Found 7347 Articles for C++

Number of Longest Increasing Subsequence in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:38:00

312 Views

Suppose we have one unsorted array of integers. we have to find the number of longest increasing subsequence, so if the input is like [1, 3, 5, 4, 7], then the output will be 2, as increasing subsequence are [1, 3, 5, 7] and [1, 3, 4, 7]To solve this, we will follow these steps −n := size of the num array, create two arrays len and cnt of size n, and fill them with value 1.lis := 1for i in range 1 to nfor j in range 0 to i – 1if nums[i] > nums[j], thenif len[j] + 1 ... Read More

Find K Closest Elements in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:29:19

351 Views

Suppose we have a sorted array, two integers k and x are also given, we have to find the k closest elements to x in that array. The result should be sorted in increasing order. If there is a tie, the smaller elements are always preferred. So if the input is like [1, 2, 3, 4, 5] and k = 4, x = 3, then output will be [1, 2, 3, 4]To solve this, we will follow these steps −Make an array called ansset low := 0, high := size of the array – kwhile low < highmid := low ... Read More

Delete Operations for Two Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:09:36

213 Views

Suppose we have two words w1 and w2, we have to find the minimum number of steps required to make w1 and w2 the same, where in each step we can delete one character in either string. So if the input is like “sea” and “eat”, then the output will be 2, because we need to delete ‘s’ from w1, this will be “ea” and delete “t” from “eat” from w2. Then they are the same.To solve this, we will follow these stepsn := size of s1, m := size of s2add one blank space before the strings s1 and ... Read More

Permutation in String in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:00:06

540 Views

Suppose we have two strings s1 and s2, we have to write a function to return true if s2 contains the permutation of s1. So we can say that one of the first string's permutations is the substring of the second string. So if the string s1 = “abc”, and second string s2 is “findcab”, then the result will be true, as the permutation of “abc” is true. That is “cab”.To solve this, we will follow these steps −create two vectors cnt1 and cnt2 of size 26for i in range 0 to s1increase the value of cnt1[s1[i] – ‘a’] by ... Read More

Subarray Sum Equals K in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:56:41

665 Views

Suppose we have an array of integers and an integer k, we need to find the total number of continuous subarrays whose sum same as k. So if nums array is [1, 1, 1] and k is 2, then the output will be 2.To solve this, we will follow these steps −define one map called sums, temp := 0, sums[0] := 1 and ans := 0for i in range 0 to size of the arraytemp := temp + n[i]if sums has k – temp, thenans := ans + sums[k - temp]increase the value of sums[-temp] by 1return ansExample(C++)Let us see ... Read More

Single Element in a Sorted Array in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:54:10

260 Views

Suppose we have a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. we have to find this single element that appears only once. So if the array is like [1, 1, 2, 3, 3, 4, 4, 8, 8], then the output will be 2To solve this, we will follow these steps −ans := 0for i in range 0 to nums array sizeans := ans XOR nums[i]return ansExample(C++)Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; class Solution { public:   ... Read More

Coin Change 2 in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:51:30

383 Views

Suppose we have coins of different denominations and a total amount of money. we have to Write a module to compute the number of combinations that make up that amount. we can assume that we have infinite number of each kind of coin. So if the amount is 5 and coins are [1, 2, 5], then there are four combinations. (1+1+1+1+1), (1+1+1+2), (1+2+2), (5)To solve this, we will follow these steps −create one array dp of size amount + 1dp[0] := 1n := size of coins arrayfor i in range 0 to n – 1for j in range coins[i] to ... Read More

Next Greater Element II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:46:18

160 Views

Suppose we have a circular array (the next element of the last element is the first element of the array), we have to display the Next Greater Number for every element. Here the Next Greater Number of a number x is the first greater number to its traversing-order next in the array, this means we could search circularly to find its next greater number. If it is not present, then it will be -1. So if the numbers are [1, 2, 1, 3, 2, 1], then output will be: [2, 3, 3, -1, 3, 2]To solve this, we will follow ... Read More

Target Sum in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:41:25

566 Views

Suppose we have a list of non-negative integers, a1, a2, ..., an, and another value, that is target, S. Now we have 2 symbols + and -. For each integer, we should choose one from + and - as its new symbol. we have to find out how many ways to assign symbols to make sum of integers same as the target value S. So if the numbers are [1, 1, 1, 1, 1], and S = 3, then the output will be 5, as the combinations are – 1 + 1 + 1 + 1 + 1 = 3, ... Read More

Sort Characters By Frequency in C++

Arnab Chakraborty
Updated on 28-Apr-2020 13:01:13

2K+ Views

Suppose we have a string, we have to sort the characters based on the frequency. So if the string is like “abbbacbcc”, then the output will be “bbbbcccaa”To solve this, we will follow these steps −create an array of pairs called v, create one map mfor all characters in string, increase value of m[character] by 1i := first element of mapwhile map has elementsinsert (i.second, i.first) into vand increase i to point to the next elementsort the vector vans := an empty stringfor i := 0 to size of vt := first element of v[i]while t is not 0ans := ... Read More

Advertisements