Found 7347 Articles for C++

Count of sub-strings that contain character X at least once in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:17:55

236 Views

We are given a string str[] and a character X. The goal is to find the substrings of str[] such that all the substrings contain X at least once. For str[]=”abc '' and X=’a’, the substrings containing ‘a’ at-least once are “a”, “ab”, “abc”. The count is 3.Let us understand with examples.Input − str[] = “aabccd” X=’c’Output − Count of sub-strings that contain character X at least once are − 14Explanation − Substrings containing at-least one ‘c’ will be : “c”, “c”, “bc”, “cc”, “cd”, “abc”, “bcc”, “ccd”, “aabc”, “abcc”, “bccd”, “aabcc”, “abccd”, “aabccd”.Input − str[] = “settings” X=’s’Output − ... Read More

Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:21:18

196 Views

We are given a string str[] containing ‘a’, ‘b’ and ‘c’ only. The goal is to find the substrings of str[] such that all the three characters are not part of that substring. For any string str, substrings could be “a”, “b”, “c”, “abb”, “bba”, “bc”, “ca”, “ccc” but not “abc”, “bcca” , “cab” as these have ‘a’, ‘b’ and ‘c’, all three.Let us understand with examples.Input − str[] = “aabc”Output − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 8Explanation − Substrings will be : ... Read More

Count of subarrays whose maximum element is greater than k in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:10:35

248 Views

We are given an array arr[] containing integer elements and a variable k . The goal is to find the count of subarrays of arr[] that have greatest/maximum element more that k. If the array is [1, 2, 3] and k is 1. Then possible subarrays are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]. The subarrays with maximum element > 1 are [2], [3], [1, 2], [2, 3], [1, 2, 3]. So the count is 5.Let us understand with examplesInput − arr[] = {1, 2, 5, 3 } k=3Output − Count of subarrays whose maximum element is ... Read More

Count of values of x <= n for which (n XOR x) = (n – x) in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:09:07

281 Views

We are given a number n as input. The goal is to find values x such that condition (n xor x)=(nx) holds. Also x lies in range [0,n].Let us understand with examplesInput − n=10Output − Count of values of x

Count of unique pairs (arr[i], arr[j]) such that i < j in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:07:12

685 Views

We are given an array containing integer elements. The goal is to find unique pairs of elements of array such that pairs of type (arr[i],arr[j]) have indexes such that iLet us understand with examplesInput − arr[] = {1,2,3};Output − Count of unique pairs (arr[i], arr[j]) such that i < j are − 3Explanation − As all elements are unique. Pairs would be −(1,2) - ( arr[0],arr[1] ) 0

Count of subsequences having maximum distinct elements in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:03:07

231 Views

We are given an array arr[] containing integers only. The goal is to find the number of subsequences of arr[] such that they have maximum number distinct elements. If the array is [ 4, 1, 2, 3, 4 ] then two subsequences will be [ 4, 1, 2, 3 ] and [ 1, 2, 3, 4 ].Let us understand with examplesInput − arr[]= { 1, 3, 5, 4, 2, 3, 1 }Output − Count of subsequences having maximum distinct elements are − 4Explanation − The maximum distinct elements are 1, 2, 3, 4 and 5. Count is 5. Subsequences will ... Read More

Count operations of the given type required to reduce N to 0 in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:00:00

658 Views

We are given a positive integer N. The goal is to find the number of operations required to reduce N to 0. Operation applied is N=N-P where P is the smallest prime divisor of P.Let us understand with examplesInput − N=17Output − Count of operations of the given type required to reduce N to 0 are − 1Explanation − The smallest prime divisor of 17 is 17 itself. So the operation is applied only once 17-17=0.Input − N=20Output− Count of operations of the given type required to reduce N to 0 are − 10Explanation − The smallest prime divisor of ... Read More

Count pairs from two BSTs whose sum is equal to a given value x in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:58:03

191 Views

We are given two binary search trees as input and a variable x. The goal is to find pairs of nodes from each tree such that the sum of value of nodes is equal to x. Take node 1 from BST_1 and node 2 from BST_2 and add data part of both. If sum=x. Increment count.Let us understand with examples.Input Output − Count of pairs from two BSTs whose sum is equal to a given value x are − 1Explanation − The pair is (8, 6)Input Output −Count of pairs from two BSTs whose sum is equal to a given value x ... Read More

Count pairs from two arrays whose modulo operation yields K in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:54:32

145 Views

We are given two arrays containing positive numbers and a value K. The goal is to find unique pairs of elements of arrays such that pairs of type (A, B) has A%B=K or B%A=K and A belongs to the first array and B belongs to the second array.Let us understand with examplesInput − arr_1[] = {1, 2, 5, 3, 4}; arr_2[] = {7, 1, 3}; k=2Output − Count of pairs from two arrays whose modulo operation yields K are − 2Explanation − The pairs are (5, 7) - (arr_1[2], arr_2[1]) 7%5=2 and (5, 3) - (arr_1[2], arr_2[2]) 5%3=2Input − arr_1[] ... Read More

Count pairs from two sorted arrays whose sum is equal to a given value x in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:50:09

364 Views

We are given two arrays containing positive numbers and a value x. The goal is to find pairs of elements of arrays such that pairs of type (A, B) has A+B=x and A belongs to the first array and B belongs to the second array.Let us understand with examplesInput − arr_1[] = {1, 2, 5, 3, 4}; arr_2[] = {7, 0, 1, 3}; x=6Output −Count of pairs from two sorted arrays whose sum is equal to a given value x are − 2Explanation − The pairs are (5, 1) - (arr_1[2], arr_2[2]) and (3, 3) - (arr_1[3], arr_2[3])Input − arr_1[] ... Read More

Advertisements