Found 7347 Articles for C++

Count number of triplets with product equal to given number with duplicates allowed in C++

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

313 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 [a,b] in C++

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

216 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) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++

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

406 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 number of step required to reduce N to 1 by following certain rule in C++

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

299 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 number of solutions of x^2 = 1 (mod p) in given range in C++

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

83 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

Count number of right triangles possible with a given perimeter in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:44:24

222 Views

We are given a perimeter P of a triangle. Perimeter is the sum of all sides of the triangle. The goal is to find the number of right triangles that can be made which have the same perimeter.If the sides of the triangle are a, b and c. Then a + b + c = P and a2 + b2 = c2 ( pythagoras theorem for any combination of a, b, and c )We will check this by taking a from 1 to p/2 and b from a+1 to p/3. Then c = p-a-b (a+b+c=p)For all right triangles, apply Pythagoras ... Read More

Count number of squares in a rectangle in C++

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

585 Views

We are given with a rectangle of length L and breadth B, such that L>=B. The goal is to find the number of squares that a rectangle of size LXB can accommodate.Above figure shows a rectangle of size 3 X 2. It has 2, 2X2 squares and 6, 1X1 squares in it.Total squares= 6+2=8.Every rectangle of size LXB has L*B number of 1X1 squares.Biggest squares are of size BXB.For L=B=1, squares = 1.For L=B=2, squares = 1 + 4 = 5. ( 1 of 2X2, 4 of 1X1 )For L=B=3, squares = 1 + 4 + 9 = 14. ( ... Read More

Count number of smallest elements in given range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:37:30

210 Views

We are given an array of integers of size N. Variables L and R define a range between 1 and N. The goal is to find the number of smallest elements that lie in range L and R such that L>=1 and R

Count number of primes in an array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:33:54

9K+ Views

We are given with an array of numbers. The goal is to find the count of prime numbers in that array.A prime number is the one which is divisible by 1 and the number itself. It has only two factors. We will check if the number is prime starting from the first element till the last and increase the count of prime numbers found so far.To check if the number N is prime, check if numbers between the range [2 to N/2], fully divides N. If yes then it is non-prime. Else it is prime.Let’s understand with examples.Input − arr[]= ... Read More

Maximize profit when divisibility by two numbers have associated profits in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:30:31

69 Views

We are given with five integers N, A, B, X and Y. The goal is to maximize the profit by checking that between numbers in range [ 1 to N ] , ifA number is divisible by A, then profit increases by X.A number is divisible by B then profit increases by Y.A profit can be added once only, for a particular number in range.Let’s understand with examples.Input − N=4, A=2, B=3, X=2, Y=3Output − Maximized profit is − 7Explanation −2, 4 are divisible by A ( 2 ). Profit increases from 0 to 2, then 2 to 4 ( ... Read More

Advertisements