Sunidhi Bansal

Sunidhi Bansal

809 Articles Published

Articles by Sunidhi Bansal

Page 3 of 81

Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 240 Views

We are given a tree which can have endless levels, a variable child which will store the number of children a node can have, a variable weight which will store the weight associated with the path and a variable path that will store the path and task is to calculate the count of number of paths which has weights equals to the X and there must be an at least one edge with the given weight.For ExampleInput - int child = 4, weight = 4, path = 4; Output - Count of number of paths whose weight is exactly X and ...

Read More

Count of AP (Arithmetic Progression) Subsequences in an array in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 663 Views

Given an array arr[] containing integer elements. The goal is to count the number of Arithmetic Progression subsequences inside arr[]. The range of elements inside arr[] are [1, 1000000].Empty sequence or single element will also be counted.Let us understand with examples.For ExampleInput - arr[] = {1, 2, 3}Output - Count of AP (Arithmetic Progression) Subsequences in an array are: 8Explanation - The following subsequences will form AP:-{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}Input - arr[] = {2, 4, 5, 8}Output - Count of AP (Arithmetic Progression) Subsequences in an array are: 12Explanation - The following subsequences ...

Read More

Count palindrome words in a sentence in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 2K+ Views

We are given a string containing an English sentence. The goal is to find the number of words in the string that are palindromes. Palindrome words are those that when read from start or end have the same alphabet sequence. If the sentence is “Madam speaks good Malayalam”, then count of palindrome words is 2. (Madam and Malayalam)Note − Words can contain both upper and lowercase alphabets.Let us understand with examples.Input − str = "My Mom and Anna left at Noon";Output − Count of palindrome words in a sentence are − 3Explanation − Palindrome words in above sentence are − ...

Read More

Count Pairs of Consecutive Zeros in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 374 Views

We have a sequence generator that starts with 1. At each step 0 becomes 10 and 1 becomes 01. So following changes will occur at consecutive steps −Step 1 − 01Step 2 − 1001Step 3 − 01101001 ……The goal is to find the number of pairs of consecutive 0’s for a given number of steps.If input step is 1 pair of 0’s - 0, input step is 2 pair of 0’s - 1, input step is 3 pair of 0’s 1Step 4 − 1001011001101001Step 5 − 01101001100101101001011001101001We can observe that sequence is increasing in powers of 2 and repeats itself ...

Read More

Count pairs in an array that hold i*arr[i] > j*arr[j] in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 250 Views

We are given an array of numbers. The goal is to find the pair of elements of array such that they hold the conditionIf (i*arr[i] > j*arr[j]) then (arr[i], arr[j]) is a valid pair.If the array is [ 5, 4, 3, 2, 1 ] then pairs will be [3, 1] and [2, 1].Let us understand with examples.Input − arr[] = [ 1, 5, 4, 1, 2, 8, 3 ]Output − Count of pairs in an array that hold i*arr[i] > j*arr[j] are − 3Explanation − Pairs are (5, 1), (4, 1), (8, 3)Input − arr[] = [ -1, -2, 3, ...

Read More

Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 346 Views

We are given an array of positive integers. The goal is to find the count of pairs of elements of arr[] such that condition LCM( arr[i], arr[j] ) > minimum of ( arr[i], arr[j] ). That is, the lowest common multiple of elements in a pair is greater than the minimum of both.Note − pair ( arr[i], arr[j] ) is the same as ( arr[j], arr[i] ). Don’t count it twice.Let us understand with examples.Input − arr[] = [ 1, 5, 4, 2 ]Output − Count of pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i], arr[j]) are − ...

Read More

Count pairs in an array such that at least one element is prime in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 255 Views

We are given an array of positive integers. The goal is to find the count of distinct pairs of elements of an array that have at-least one prime member. If the array is [1, 2, 3, 4] then pairs would be (1, 2), (1, 3), (2, 3), (2, 4) and (3, 4).Let us understand with examplesInput − arr[] = { 1, 2, 4, 8, 10 };Output − Count of pairs in an array such that at least one element is prime are − 4Explanation − The only prime element is 2 and pairing it with all others will give − ...

Read More

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 529 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

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 272 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 BSTs whose sum is equal to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 322 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
Showing 21–30 of 809 articles
« Prev 1 2 3 4 5 81 Next »
Advertisements