Found 7347 Articles for C++

3Sum Smaller in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:36:55

247 Views

Suppose we have an array of n integers called nums and we also have a target, we have to find the number of index triplets (i, j, k) here i, j, k all are in range 0 to n - 1 and that satisfy the condition nums[i] + nums[j] + nums[k] < target.So, if the input is like nums = [-2, 0, 1, 3], and target = 2, then the output will be 2, as there are two triplets which sums are less than 2: [-2, 0, 1] and [-2, 0, 3].To solve this, we will follow these steps −ret ... Read More

Verify Preorder Sequence in Binary Search Tree in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:35:02

98 Views

Suppose we have a sequence of numbers; we have to check whether it is the correct preorder traversal sequence of a binary search tree. We can assume that each number in the sequence is unique. Consider the following binary search tree −So, if the input is like [5, 2, 1, 3, 6], then the output will be trueTo solve this, we will follow these steps −itr := -1low := -infinityfor initialize i := 0, when i < size of preorder, update (increase i by 1), do −x := preorder[i]if x < low, then −return falsewhile (itr >= 0 and preorder[itr] ... Read More

Factor Combinations in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:33:01

198 Views

Suppose we have a number. The numbers can be regarded as a product of its factors. So, 8 = 2 x 2 x 2; = 2 x 4. We have to make one function that takes an integer n and return all possible combinations of its factors.So, if the input is like 12, then the output will be [[2, 6], [2, 2, 3], [3, 4]]To solve this, we will follow these steps −Define a function solve(), this will take n, target, start, define one list of lists called retif n is same as 1, then −return retif n is not ... Read More

Meeting Rooms II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:30:42

843 Views

Suppose there is an array of meeting time intervals. There are two times start and end times [[s1,e1],[s2,e2],...] and each pair satisfies the rule (si < ei), We have to find the minimum number of conference rooms required.So, if the input is like [[0, 30], [5, 10], [15, 20]], then the output will be 2.To solve this, we will follow these steps −define one priority queue pqsort the intervals arrayret := 0for initialize i := 0, when i < size of intervals, update (increase i by 1), do −while (not pq is empty and top element of pq

Flatten 2D Vector in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:28:57

431 Views

Suppose we have a 2D vector, we have to design and implement an iterator to flatten that 2d vector. There will be different methods as follows −next() − This will return the next element of the current elementhasNext() − This will check whether next element is present or notSo, if the input is like [[1, 2], [3], [4]] then if we call the functions as follows −iterator.next();iterator.next();iterator.next();iterator.hasNext();iterator.hasNext();iterator.next();iterator.hasNext();then the output will be [1, 2, 3, true, true, 4, false]To solve this, we will follow these steps −Define one 2D array vDefine initializer this will take one 2D array v, rowPointer := ... Read More

Count Univalue Subtrees in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:26:39

199 Views

Suppose we have a binary tree; we have to count the number of uni-value subtrees. Here the Uni-value subtree indicates all nodes of the subtree have the same value.So, if the input is like root = [5, 1, 5, 5, 5, null, 5], then the output will be 4To solve this, we will follow these steps −Define a function solve(), this will take node, if node is empty, then −return trueleft := solve(left of node)right := solve(right of node)if left is false or right is false, then −return falseif left of node is present and val of node is not ... Read More

Group Shifted Strings in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:23:44

116 Views

Suppose we have a string, we can "shift" each of its letter to its successive letter, so: "abc" can be changed to "bcd". We can keep doing this operation which forms the sequence: "abc" -> "bcd" -> ... -> "xyz". If we have a list of non-empty strings that contains only lowercase alphabets, we have to group all strings that belong to the same shifting sequence.So, if the input is like ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], then the output will be [ ["abc", "bcd", "xyz"], ["az", "ba"], ["acef"], ["a", "z"] ]To solve this, we will follow these ... Read More

Strobogrammatic Number II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:21:46

399 Views

Suppose we have a length n. We have to find all strobogrammatic numbers that are of length n.As we know that a strobogrammatic number is a number that looks the same when rotated 180 degrees.So, if the input is like n = 2, then the output will be ["11", "69", "88", "96"]To solve this, we will follow these steps −Define an array retif n is odd, then −insert "0" at the end of retinsert "1" at the end of retinsert "8" at the end of retOtherwiseinsert blank string at the end of retfor n > 1, update n := n ... Read More

Shortest Word Distance III in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:19:46

141 Views

Suppose we have a list of words and another two words called word1 and word2, we have to find the shortest distance between these two words in the list. Here word1 and word2 may be the same and they represent two individual words in the list. Let us assume that words = ["practice", "makes", "perfect", "skill", "makes"].So, if the input is like word1 = “makes”, word2 = “skill”, then the output will be 1To solve this, we will follow these steps −ret := 10^9, l1 := 10^9, l2 := -10^9n := size of wordsfor initialize i := 0, when i ... Read More

Shortest Word Distance II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:17:13

285 Views

Suppose there is a class that receives a list of words in the constructor, there will be a method that takes two words word1 and word2 and find the shortest distance between these two words in the list. That method will be called repeatedly many times with different parameters.Let us assume that words = ["practice", "makes", "perfect", "skill", "makes"].So, if the input is like word1 = “skill”, word2 = “practice”, then the output will be 3To solve this, we will follow these steps −Define one map mThe initializer takes an array of wordsfor initialize i := 0, when i < ... Read More

Advertisements