Programming Articles - Page 1974 of 3366

Delete and Earn in C++

Arnab Chakraborty
Updated on 02-May-2020 09:02:50

265 Views

Suppose we have an array nums of integers, we can perform some operations on the array. Here in each operation, we pick any nums[i] and delete it to earn nums[i] amount of points. We must delete every element equal to nums[i] - 1 or nums[i] + 1. Initially the point is 0. We have to find the maximum number of points we can earn by applying such operations. So if the input is like [3, 4, 2], then the output will be 6. So this is because, if we delete 4, we will get 4 points, consequently 3 will also ... Read More

Asteroid Collision in C++

Arnab Chakraborty
Updated on 02-May-2020 09:00:25

1K+ Views

Suppose we have an array asteroids of integers representing asteroids in a row. Now for each asteroid, the absolute value represents its size, and the sign represents its direction that can be positive or negative for the right and left respectively. Each asteroid moves at the same speed.We have to find the state of the asteroids after all collisions. When two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.So if the input is like [5, 10, -5], then output will be [5, ... Read More

Maximum Length of Pair Chain in C++

Arnab Chakraborty
Updated on 02-May-2020 08:57:28

167 Views

Suppose in the world of Dota2, there are two parties − The Radiant and also the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to form a choice a few change within the Dota2 game. The voting for this alteration may be a round-based procedure. In each round, each senator can exercise one among the two rights −Ban one senator's right − A senator can make another senator lose all his rights during this and every one the subsequent rounds.Announce the victory − If this senator found the senators who still have rights ... Read More

Shopping Offers in C++

Arnab Chakraborty
Updated on 02-May-2020 08:53:56

292 Views

Suppose there is a store, there are some items to sell. Each item has some price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. So we have the list of prices, a set of special offers, and the number we need to buy for each item. The task is to find the lowest price we have to pay for exactly certain items as given, where we could make optimal use of the special offers.Here each special offer is represented in the form of an array, ... Read More

Exclusive Time of Functions in C++

Arnab Chakraborty
Updated on 02-May-2020 08:48:11

315 Views

Suppose on a single threaded CPU, we execute some functions. Now each function has a unique id between 0 and N-1. We will store logs in timestamp order that describe when a function is entered or exited.Here each log is a string written this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, if the string is like "0:start:3" this means that the function with id 0 started at the beginning of timestamp 3. "1:end:2" means the function with id 1 ended at the end of timestamp 2. A function's exclusive time is the number of units of time spent in this function.So ... Read More

Add Two Numbers II in C++

Nishu Kumari
Updated on 23-Jul-2025 13:21:11

385 Views

We're given two singly linked lists, where each node stores one digit of a number. The digits are arranged from left to right, just like how we normally write numbers. For example: 7 -> 2 -> 4 -> 3 represents the number 7243. Our task is to add these two numbers and return the sum as a new linked list in the same (left-to-right) order. The input number can contain zeroes at the start, but in the output, there should not be any leading zeros. Let's understand this with a diagram given below - Scenario 1 Input: List 1 ... Read More

Maximum XOR of Two Numbers in an Array in C++

Arnab Chakraborty
Updated on 02-May-2020 08:40:02

232 Views

Suppose we have a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. We have to find the maximum result of ai XOR aj, where 0 ≤ i, j < n. So if the input is like [3, 10, 5, 15, 2, 8], then the output will be 28. The max result will be 5 XOR 25 = 28.To solve this, we will follow these steps −Define insertNode(), this will take val and headcurr := headfor i in range 31 to 0bit := val / (2^i) AND 1if child[bit] of curr is null, ... Read More

Random Pick Index in C++

Arnab Chakraborty
Updated on 02-May-2020 08:36:35

837 Views

Suppose we have an array of integers with possible duplicates, we have to pick the index randomly of a given target number. We can assume that the given target number must exist in the array. So if the array is like [1, 2, 3, 3, 3], then pick(3), may return 2, 3, 4 randomly.To solve this, we will follow these steps −ret := - 1, cnt := 1for i in range 0 to size of vif v[i] = target, thenif random number mod cnt = 0, then ret = icnt := cnt + 1return retExample (C++)Let us see the following ... Read More

Integer Replacement in C++

Arnab Chakraborty
Updated on 02-May-2020 08:31:38

721 Views

Suppose we have a positive integer n and we can do these operations as follow −If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.We have to find the minimum number of replacements needed for n to become 1?So if the number is 7, then the answer will be 4, as 7 → 8 → 4 → 2 → 1 or 7 → 6 → 3 → 2 → 1To solve this, we will follow these steps −ret := 0, n := xwhile n > 1if ... Read More

Rotate Function in C++

Arnab Chakraborty
Updated on 02-May-2020 08:29:25

337 Views

Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].Now find the maximum value of F(0), F(1), ..., F(n-1).So if the input is like A = [4, 3, 2, 6], then −F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 ... Read More

Advertisements