We are given with an array of N elements. The goal is to find the count of all pairs (Arr[i], Arr[j]) which have a sum which is a perfect square such that i!=j. That is Arr[i]+Arr[j] is a perfect square.We will do this by calculating the sum of pairs and check if the square root of that sum is equal to the floor value of the square root. sqrt(Arr[i]+Arr[j])-floor( sqrt(Arr[i]+Arr[j] )==0.Let’s understand with examples.Input − Arr[]= { 4, 3, 2, 1, 2, 4 } N=6Output − Count of pairs with sum as perfect square − 2Explanation −Arr[1]+Arr[3]=4, sqrt(4)-floor(4)=0 4 is ... Read More
We are given with an array of numbers Arr[]. The goal is count the number of triplets whose product is equal to the smallest product of all possible triplets.Count triplets if (i
We are given a range [l,r] and a number k. The goal is to find all the numbers between l and r (l
We are given with N no. of participants in a coding competition. The goal is to find the no. of pairs that are possible when a person can pair with at most one other person. So a pair has at most 2 participants. The participants are allowed to take part alone also.We can solve this using recurrence where pairs=count=1 when n=0 or 1 ( only one person left )if person remains single n reduced to n-1now for remaining pairing people left = n-2count=makePairs(p-1) + (p-1)*makePairs(p-2);Let’s understand with examples.Input − persons=3Output − Count of ways to make pair − 4Explanation −If ... Read More
We are given with an array of N elements. The goal is to find the index pairs (i, j) which have the same element value such that i!=j. i.e, Arr[i]=Arr[j] and i!=j. This is used to make pairs of gloves of equal size. Out of N gloves only paired gloves are useful to sell.We will do this by running two loops with 0 -1. Total pairs=2Approach used in the below program is as followsWe take an integer array Arr[] initialized with random numbers for size of gloves > 0.Take a variable n which stores the length of Arr[].Function countPairs(int arr[], ... Read More
We are given with an array of numbers Arr[]. The goal is to count the number of triplets whose product is equal to the given number p. There can be more than one triplet with the same values but different elements. For example, (1, 2, 3) and (3, 1, 2) in array [1, 2, 3, 1, 2] will be counted as different if elements are different but values are the same.Let’s understand with examples.Input − arr[]= { 1, 2, 3, 2, 4, 1, 5 }, p=4Output − Number of triplets: 3Explanation −Triplet 1[ 1, 2, 3, 2, 4, 1, 5 ... Read More
We are given an array of integers, Arr[] and two variables a and b to define a range [a,b]. The goal is to find the number of triplets whose sum lies in between this range [a,b].We will do this by using three for loops. Increment count if arr[i]+arr[j]+arr[k]>=a and arr[i]+arr[j]+arr[k]
We are given an integer n. The goal is to find triplets ( set of 3 numbers ) that satisfy the conditions −a2+b2=c21
We are given a number N. The goal is to count the number of steps required to reduce the number to 1 by following rules −If the number is power of 2, reduce it to its half.Else reduce it to the N-(nearest power of 2 which is less than N).For step 1, we will check if N is power of 2, by checking if ceil(log2(N)), floor(log2(N)) return the same result. If yes then N=N/3, increment count of operation.If the result of step 1 is false then we will perform step 2 and subtract the nearest power of 2 less than ... Read More
We are given with integers x and p. The goal is to find the number of solutions of the equation −x2=1 ( mod p ) such that x lies in range [1, N].We will do this by traversing from 1 to N and take each number as x check if (x*x)%p==1. If yes then increment the count.Let’s understand with examples.Input − n=5, p=2Output − Number of Solutions − 3Explanation − Between the range 1 to 5.12=1%2=1, count=1 22=4%2=0, count=1 32=9%2=1, count=2 42=16%2=0, count=2 52=25%2=1, count=3 Total number of solutions=3.Input − n=3, p=4Output − Number of Solutions − 2Explanation − Between ... Read More