Found 7347 Articles for C++

Find All Duplicates in an Array in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:53:01

10K+ Views

Suppose we have an array of integers, in range 1 ≤ a[i] ≤ n (n = size of array), here some elements appear twice and others appear once. We have to find all the elements that appear twice in this array. So if the array is [4, 3, 2, 7, 8, 2, 3, 1], then the output will be [2, 3]To solve this, we will follow these steps −n := size of array, make one array called ansfor i in range 0 to n – 1x := absolute value of nums[i]decrease x by 1if nums[x] < 0, then add x ... Read More

Longest Repeating Character Replacement in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:42:36

1K+ Views

Suppose we have given a string s that consists of only uppercase letters, we can perform at most k operations on that string. In one operation, we can select any character of the string and change it to any other uppercase letters. We have to find the length of the longest sub-string containing all repeating letters we can get after performing the above operations. So if the input is like: “ABAB” and k = 2, then the output will be 4. This is because two ‘A’s with two ‘B’s or vice versa.To solve this, we will follow these steps −maxCount ... Read More

Reconstruct Original Digits from English in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:32:27

328 Views

Suppose we have a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. There are some properties −Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.Input length is less than 50, 000.So if the input is like “fviefuro”, then the output will be 45.To solve this, we will follow these steps −nums := an array that is holding the numbers in English letter from 0 to 9.make one array count of size 10ans := an empty ... Read More

Partition Equal Subset Sum in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:28:44

300 Views

Suppose we have a non-empty array containing only positive numbers, we have to find if the array can be partitioned into two subsets such that the sum of elements in both subsets is the same. So if the input is like [1, 5, 11, 5], the output will be true. As this array can be partitioned as [1, 5, 5] and [11]To solve this, we will follow these steps −n := size of the arraysum := 0for i := 0 to n – 1sum := sum + nums[i]if sum is odd, return falsesum := sum / 2create one array called ... Read More

Queue Reconstruction by Height in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:23:17

245 Views

Consider we have a random list of people standing in a queue. If each person is described by a pair of integers (h, k), where h is the height and k is the number of people in front of him, who have a height greater than or equal to h. We have to define one method to reconstruct the queue. So if the given array is like [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]], then the output will be [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]To solve this, we will ... Read More

Longest Substring with At Least K Repeating Characters in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:18:21

217 Views

Suppose we have a string s, and we have to find the length of the longest substring T of that given string (consists of lowercase letters only) such that every character in T appears no less than k times. So if the string is “ababbc” and k = 2, then the output will be 3 and longest substring will be “ababb”, as there are two a’s and three b’s.To solve this, we will follow these steps −create one recursive function called longestSubstring(), this takes string s and size kif k = 1, then return the size of the stringif size ... Read More

Decode String in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:10:27

2K+ Views

Suppose we have an encoded string; we have to return its decoded string. The rule for encoding is: k[encoded_string], this indicates where the encoded_string inside the square brackets is being repeated exactly k times. We can assume that the original data does not contain any numeric characters and that digits are only for those repeat numbers, k. So if the input is like “1[ba]2[na]”, then the output will be “banana”.To solve this, we will follow these steps −create one empty stack, set i := 0while i < size of a stringif s[i] is ‘]’res := delete element from the stack ... Read More

Array Transformation in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:23:38

469 Views

Suppose there is an initial array arr, consider every day we produce a new array using the array of the previous day. On the i-th day, we will perform the following operations on the array of day i-1 to produce the array of the day i. The conditions are as follows −If an element is smaller than both its left and its right adjacent values, then this element is incremented.If an element is bigger than both its left and its right adjacent values, then this element is decremented.The first and last elements will remain same.After some days, the array does ... Read More

Intersection of Three Sorted Arrays in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:16:52

350 Views

Suppose there are three integer arrays arr1, arr2 and arr3 and they are sorted in strictly increasing order, we have to return a sorted array of only the integers that appeared in all of these three arrays. So if arrays are [1, 2, 3, 4, 5], [1, 2, 5, 7, 9], and [1, 3, 4, 5, 8], so the output will be [1, 5]To solve this, we will follow these steps −define an array called rescreate three maps f1, f2 and f3for i in range 0 to length of arr1f1[arr1[i]] increase by 1for i in range 0 to length of ... Read More

Combination Sum IV in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:53:24

190 Views

Suppose we have an integer array with all positive numbers and all elements are unique, find the number of possible combinations, so that if we add up, we will get positive integer target.So if the array is [1, 2, 3] and the target is 4, then the possible combinations will be [[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 3], [3, 1], [2, 2]], so output will be 7.To solve this, we will follow these steps −Suppose we have one recursive function called solve(), this is taking array, target and another array for dynamic ... Read More

Advertisements