Sunidhi Bansal

Sunidhi Bansal

809 Articles Published

Articles by Sunidhi Bansal

Page 32 of 81

Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++

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

We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their squares is N.We will do this by finding solutions to the equation a2+ b2 = N. Where a is not more than square root of N and b can be calculated as square root of (N-a2).Let’s understand with examples.Input N=100Output Count of pairs of (a, b) where a^3+b^3=N: 2Explanation Pairs will be (6, 8) and (8, 6). 62+82=36+64=100Input N=11Output Count of pairs of (a, b) where a^3+b^3=N: 0Explanation No such pairs possible.Approach used in the below program is as followsWe take integer N.Function squareSum(int ...

Read More

Count pairs (i,j) such that (i+j) is divisible by both A and B in C++

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

We are given variables N, M, A and B. The goal is to find ordered pairs of positive numbers( i, j ) such that their sum is divisible by both A and B. And 1

Read More

Count pairs from two arrays having sum equal to K in C++

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

We are given two arrays Arr1[] and Arr2[] and a number K. The goal is to find unique pairs of elements of both arrays such that their sum is K. Pairs will be of form ( Arr1[i], Arr2[j] ) where Arr1[i]+Arr2[j]==K.We will traverse using two loops for i and j. If sum (Arr1[i]+Arr2[j])==K. And the pair doesn’t exist in unordered_map. Add it to the map and increment count.Let’s understand with examples.Input Arr1[]={ 1, 3, 2, 4, 3, 2 }; Arr2[]={ 0, 2, 1, 2, 3 }; K=4Output Number of pairs with sum K : 4Explanation Pairs will be ( Arr1[0], Arr2[4] ) → ...

Read More

Count square and non-square numbers before n in C++

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

We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their cubes is N.Naive ApproachTraverse all numbers from 1 to N and check if it is a perfect square. If floor(sqrt(i))==ceil(sqrt(i)).Then the number is a perfect square.Efficient ApproachPerfect squares below N can be found using formula: floor(sqrt(N)).Let’s understand with examples.Input N=20Output Count of square numbers: 4 Count of non-square numbers: 16Explanation Square numbers are 1, 4, 9 and 16. Rest all are non-squares and less than 20.Input N=40Output Count of square numbers: 6 Count of non-square numbers: 34Explanation Square numbers are 1, 4, 9, 16, ...

Read More

Count strings that end with the given pattern in C++

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

We are given an array of strings str[] and a pattern string pat. The goal is to find the string elements of str[] that have pattern pat at the end.We will traverse each string of str and compare last characters with pat. If they match incrementLet’s understand with examples.Input str[]={ “kittens”, “hens”, “deers”, “dogs” } pat=”ens”Output Strings that end with given pattern: 2Explanation Strings “kitt-ens” and “h-ens” end with “ens”.Input str[]={ “tickets”, “wickets”, “bats”, “cricket” } pat=”et”Output Strings that end with given pattern: 1Explanation Strings “wick-et” ends with “et”.Approach used in the below program is as followsWe string array str[] and a pattern string pat.N is ...

Read More

Count Primes in Ranges in C++

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

We are given range variables START and END. The goal is to find the count of prime numbers in the range [START, END].We will check if number i in range is prime by checking if any number other than 1 fully divides it and is between 1 and i/2. If it is prime. Increment count.Let’s understand with examples.Input Start=1 End=20Output Primes in Ranges : 8Explanation Primes between 1 and 20 are: 2, 3, 5, 7, 11, 13, 17, 19.Input Start=100 End=200Output Primes in Ranges : 21Explanation Primes between 100 and 200 are: 101 103 107 109 113 127 131 137 139 149 151 157 163 167 ...

Read More

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

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

We are given four arrays A[], B[], C[] and D[]. The goal is to find all quadruples of elements of these arrays such that A[i]+B[j]+C[k]+D[l] =x. All four arrays have the same number of elements N.We will do this by traversing each array once and compare if A[i]+B[j]+C[j]+D[l]==x. If true increment count.Let’s understand with examples.Input A[]={ 1, 2, 3}; B[]={ 2, 3, 2}; C[]={ 4, 3, 1}; D[]={ 3, 1, 1 }; X=12Output Count of Quadruples: 4Explanation Quadruples such as ( A[i] B[j] C[k] D[l] ) are: (2 3 4 3) , (3 2 4 3), (3 3 3 3), (3 2 4 ...

Read More

Count numbers whose sum with x is equal to XOR with x in C++

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

We are a number X. The goal is to find numbers between 0 and X whose sum with X is equal to XOR with X.We will do this by traversing no. from i=0 to i

Read More

Count numbers whose XOR with N is equal to OR with N in C++

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

We are a number N. The goal is to find numbers between 0 and N whose OR with N is equal to XOR with N.We will do this by traversing no. from i=0 to i

Read More

Count number of ways to divide a number in parts in C++

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

We are given a positive number N. The goal is to count the number of ways in which the number N can be divided into 3 parts. The parts may or may not be equal. N lies in range [1, 5000].We will do this by using three for loops for 3 parts of the number. Check at the innermost loop that the sum of all three is equal to N. If true, then increment the count of ways.Let’s understand with examples.Input − N=5Output − Number of ways to divide N in 3 parts: 2Explanation − 5 can be shown as ...

Read More
Showing 311–320 of 809 articles
« Prev 1 30 31 32 33 34 81 Next »
Advertisements