Found 7347 Articles for C++

Count of quadruplets with given Sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:35:16

135 Views

We are given with four arrays. Goal is to find quadruplets of elements from the four arrays that have sum equal to a given Sum value. The elements selected should be such that all 4 elements belong to different arrays.We will do this by traversing all arrays using for loop and check if A[i]+B[j]+C[k]+D[l]==sum. If yes increment count.Let us understand with examples −Input −A[]={ 1, 3, 1}, B[]={ 2, 4, 5 } , C[]={ 1, 1, 2 } , D[]= { 4, 4, 0} Sum=5Output − Count of quadruplets with given sum are − 2Explanation −2 quadrutplets are: (A[0], B[0], ... Read More

Maximize number of nodes which are not part of any edge in a Graph in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:31:49

59 Views

We are given a graph containing nodes and edges. The goal is to find the maximum number of possible nodes that are connected to any edge of the graph. We know that no. of nodes will always be less than or equal to the number of edges in a complete graph.We will do this by trying to make a complete graph where the number of nodes is n then there will be n(n-1)/2 edges.edge=n(n-1)/2 (here n for nodes )2*edge=n(n-1). Once n(n-1)> no. of edges then we have extra nodes. So iterate from i=1 to i=n.Till i(i-1)>2*edge. Return n-i as result.Let ... Read More

Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++

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

179 Views

We are given an array of integers. The goal is to count numbers such that the absolute difference between the sum of all elements and that element is greater than variable k.We will do this by getting the sum of elements of the array. Now for each element arr[i], check if −sum-2(arr[i])>k, as sum already includes arr[i] so subtract it twice. If true increment count.Let’s understand with examples.Input − arr[]= { 1, 2, 3, 0, 3, 2, 0, 1 }, k=10Output − Count of elements: 2Explanation − Sum of elements is 1212-1-1=10, 12-2-2=8, 12-3-3=6, 12-0-0=12.Only 12 > 10, so for ... Read More

Count of numbers which can be made power of 2 by given operation in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:27:46

231 Views

We are given an array of positive integers. The goal is to find the count of numbers that can be made power of two by adding 1 to them utmost once.We will check using log2(i) that number is power of two or can become power of two by adding 1 to it. If yes increment count.Let’s understand with examples.Input − arr[]= {1, 3, 2, 5, 6 }, Output − Count of numbers that can become power of 2: 3Explanation − 1+1=2 → 21 , 3+1=4 → 22 , 2=21 others will become 5+1=6, 6+1=7Input − arr[]= {2, 4, 8, 16 ... Read More

Count of only repeated element in a sorted array of consecutive elements in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:21:18

5K+ Views

We are given an array of consecutive numbers of length n. The array has only one number which is repeated more than once. The goal is to get the number of times that element is repeated in the array. Or we can say find the length of a repeated element in the array.We will traverse the array from i=0 to i

Count numbers upto N which are both perfect square and perfect cube in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:19:15

4K+ Views

We are given a number N. The goal is to count the numbers upto N that are perfect squares as well as perfect cubes. For example, 1, 64 are both perfect squares and perfect cubes.We will use sqrt() to calculate square root and cbrt() to calculate cube root of a number.Let’s understand with examples.Input − N=100Output − Count of numbers that are perfect squares and cubes − 2Explanation − 1 and 64 are only numbers from 1 to 100 that are both perfect squares and cubes.Input − N=5000Output −Count of numbers that are perfect squares and cubes − 3Explanation − ... Read More

Count numbers with unit digit k in given range in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:16:53

369 Views

We are given an interval [first, last]. The goal is to find the count of numbers that have a unit digit k and lie between range [first, last].We will do this by traversing from i=first to i=last. For each number i compare its unit digit with the k, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40 , k=8Output − Count of numbers with unit digit k − 4Explanation −Numbers between 8 and 40 with unit digit = 8 8, 18, 28, 38Input − first=100 last=200 , k=9Output − Count of numbers with unit digit ... Read More

Count of m digit integers that are divisible by an integer n in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:05:47

193 Views

We are given two integers m and n. The goal is to count m digit numbers that are divisible by n.If m=1, then numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and n=3 then numbers divisible by 3=0, 3, 6, 9 count=4.Let’s understand with examples.Input − m=2, n=9Output − Count of m digit numbers divisible by n − 10Explanation − between 10 and 99 numbers divisible by 9 are −18, 27, 36, 45, 54, 63, 72, 81, 90, 99Input m=3, n=300Output − Count of m digit numbers divisible by n: 3Explanation − between 100 and 999 ... Read More

Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:03:46

581 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (i,j) such that arr[i]*arr[j] > arr[i]+arr[j] and 0sum.Traverse array using two for loops for each element of the pair.Outer Loop from 0

Count number of ordered pairs with Even and Odd Sums in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:01:45

251 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the sum of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate sum, if it is even increment count by 2 for even sums else increment count by 2 for odd sums.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output− Count of even product sums − 6 Count of odd sum pairs ... Read More

Advertisements