Found 7197 Articles for C++

Elimination Game in C++

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

881 Views

Suppose we have a list of sorted integers from 1 to n. That is starting from left and ending at right, we have to remove the first number and every other number afterward until we reach the end of the list. We will repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. We will repeat the steps again, alternating left to right and right to left, until one single number remains. We have to find the last number that remains starting with a list ... Read More

Linked List Random Node in C++

Arnab Chakraborty
Updated on 02-May-2020 08:17:15

395 Views

Suppose we have a singly linked list, we have to find a random node's value from the linked list. Here each node must have the same probability of being chosen. So for example, if the list is [1, 2, 3], then it can return random node in range 1, 2, and 3.To solve this, we will follow these steps −In the getRandom() method, do the following −ret := -1, len := 1, v := xwhile v is not nullif rand() is divisible by len, then ret := val of vincrease len by 1v := next of vreturn retExample(C++)Let us see ... Read More

Wiggle Subsequence in C++

Arnab Chakraborty
Updated on 02-May-2020 08:14:05

304 Views

Suppose we have a sequence of numbers that is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference may be either positive or negative. A sequence with less than two elements is trivially a wiggle sequence. So for example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because if you see, the differences (6, -3, 5, -7, 3) are alternately positive and negative. But, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences, the first one because its first two differences are ... Read More

Super Pow in C++

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

538 Views

Suppose we have to calculate a^b mod 1337 where a is one positive integer and b is an extremely large positive integer given in the form of an array. So if a = 2 and b = [1, 0] then the output will be 1024To solve this, we will follow these steps −Define powerMod() method this takes base and powerm := 1337, ret := 1while power is not 0if power is odd, then ret := ret * base mod mbase := base^2 mod mpower := power / 2return retDefine superPower(), this takes a and bif size of b = 0, ... Read More

Largest Divisible Subset in C++

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

126 Views

Suppose we have a set of distinct positive integers, we have to find the largest subset such that every pair like (Si, Sj) of elements in this subset satisfies: Si mod Sj = 0 or Sj mod Si = 0.So if the input is like [1, 2, 3], the possible result may come like [1, 2] or [1, 3]To solve this, we will follow these steps −Create an array ret, set endpoint := 0, retLen := 1, n := size of numsif n is 0, then return empty setsort nums arraycreate two arrays len and par of size n, initialize ... Read More

Water and Jug Problem in C++

Arnab Chakraborty
Updated on 02-May-2020 08:04:37

2K+ Views

Suppose we have two jugs with capacities x and y liters. There is an infinite amount of water supply available to us. Now we need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water are measurable, we must have z liters of water contained within one or both buckets by the end.We can do these few operations −Fill any of the jugs fully with water.Empty any of the jugs.Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.So ... Read More

Count Numbers with Unique Digits in C++

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

1K+ Views

Suppose we have a non-negative integer n. We have to count all numbers with unique digits x, where x is in range 0 to 10^n. So if the number n is 2, then the result will be 91, as we want to find numbers from 0 to 100 without 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −if n is 0, then return 1n := min of 10 and nif n is 1, then return 10ans := 9 and ret := 10for i in range 2 to nans := ans * (9 ... Read More

Integer Break in C++

Arnab Chakraborty
Updated on 02-May-2020 07:56:49

294 Views

Suppose we have a positive integer n, we have to break it into the sum of at least two positive numbers and maximize the product of those integers. We have to find the maximum product we can get. So if the number is 10, then the answer will be 36, as 10 = 3 + 3 + 4, 3 * 3 * 4 = 36To solve this, we will follow these steps −Define a method solve(), this will take n, array dp and flagif n is 0, then return 1if dp[n] is not -1, then return dp[n]end := n – ... Read More

Reconstruct Itinerary in C++

Arnab Chakraborty
Updated on 02-May-2020 07:53:32

259 Views

Suppose we have a list of airline tickets represented by pairs of departure and arrival airports like [from, to], we have to reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. So, the itinerary must begin with JFK.So if the input is like [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], then the output will be ["JFK", "MUC", "LHR", "SFO", "SJC"].To solve this, we will follow these steps −Define array ret and a map called graph.Define a method called visit. This will take airport name as inputwhile size of the graph[airport] is ... Read More

Maximum Product of Word Lengths in C++

Arnab Chakraborty
Updated on 02-May-2020 07:50:19

487 Views

Suppose we have a string array called words, find the maximum value of length(word[i]) * length(word[j]) where the two words will not share the common letters. We can assume that each word will contain only lower case letters. If no such two words exist, then return 0. So if the input is like [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”], then the output will be 16, as two words can be “abcw”, “xtfn”.To solve this, we will follow these steps −Define a method called getRev(), this will take x as inputret := 0for i in range 0 to 25if x / ... Read More

Advertisements