Count of Pairs in an Array Whose Sum is a Perfect Square in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:22:33

672 Views

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

Count Ways to Form Minimum Product Triplets in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:19:36

243 Views

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

Counting Numbers Whose Difference from Reverse is a Product of K in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:11:19

119 Views

We are given a range [l,r] and a number k. The goal is to find all the numbers between l and r (l

Counting Pairs in C++ with At Most One Pair Per Person

Sunidhi Bansal
Updated on 29-Aug-2020 09:08:11

251 Views

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

Count of Index Pairs with Equal Elements in an Array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:05:28

502 Views

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

Count Number of Triplets with Product Equal to Given Number in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:01:41

490 Views

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

Count Number of Triplets in an Array Having Sum in the Range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:58:32

292 Views

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]

Count Number of Triplets (a, b, c) in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:55:45

542 Views

We are given an integer n. The goal is to find triplets ( set of 3 numbers ) that satisfy the conditions −a2+b2=c21

Count Steps to Reduce N to 1 in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:51:32

381 Views

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

Count Solutions of x^2 ≡ 1 (mod p) in Given Range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:48:06

143 Views

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

Advertisements