Count Number of Ordered Pairs with Even and Odd Sums in C++

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

379 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

Count Number of Ordered Pairs with Even and Odd Product in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:59:47

226 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the product 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 product, if it is even increment count by 2 for even products else increment count by 2 for odd products.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output − Count of even product pairs: 6 Count of odd product pairs: ... Read More

Count Numbers with Same First and Last Digits in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:57:28

491 Views

We are given an interval [first, last]. The goal is to find the count of numbers that have the same first and last digit within this interval. For example, 232 has the same first and last digit as 2.We will do this by traversing from i=first to i=last. For each number I compare its first digit with the last digit, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40Output − Count of numbers with same first and last digits − 5Explanation − Numbers between 8 and 40 with same first and last digit −8, 9, ... Read More

Count Operations to Reduce a Number in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:53:16

320 Views

We are given with a positive integer K and an array Ops[] which contains integers. The goal is to find the number of operations required to reduce K such that it becomes less than 0. Operations are −First operation is K + Ops[0], first element added to KAfter 1. Add Ops[i] to K until K

Count Pairs with Column Sum Greater than Row Sum in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:51:02

197 Views

We are given a matrix of size NXN. The goal is to find the count of all valid pairs of indexes (i, j) such that the sum elements of column j is greater than the sum of elements of row i.We will do this by traversing the matrix and calculate sums of elements of each row and column.Store sum of elements of each in rowsum[N] and sum of elements of each column in colsum[N].Now make pairs of rowsum[i] and colsum[j] and check if colsum[j]>rowsum[i]. If true increment count for such pairs.Let’s understand with examples.Input-: matrix= {    { 1, 2, ... Read More

Count Divisible Pairs in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:49:03

1K+ Views

We are given with an array arr[] of N elements. The goal is to find the count of all valid pairs of indexes (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] and i!=j.We will do this by traversing the array arr[] using two for loops for each number of pair and check if arr[i]%arr[j]==0 or arr[j]%arr[i]==0 when i!=j. If true increment count of pairs.Let’s understand with examples.Input − Arr[]= { 2, 4, 3, 6 } N=4Output − Count of valid pairs − 3Explanation − Valid pairs are −Arr[0] & Arr[1] → (2, ... Read More

Count the Number of Possible Triangles in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:47:14

854 Views

We are given an array which contains the length of sides of triangles. The goal is to find the number of possible triangles that can be made by taking any three sides from that array.We will do this by checking if the sum of any two is always > third side. If yes these three sides can make a triangle. Increment count of possible triangles that can be made.Let’s understand with examples.Input − arr[]= {1, 2, 4, 5}Output − Count of possible triangles − 1Explanation − Sides (2, 4, 5) can only make a triangle as 2+4>5 & 4+5>2 & ... Read More

Count Numbers n with Equal Number of Divisors as k in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:45:21

213 Views

We are given two numbers N and K. The goal is to find the count of numbers between 1 and N that have divisors equal to the divisors of K in range [1, N].We will first count the divisors of K in range [1, N] and store in variable count.Now we will start from i=1 to i=N. Now for each number num=i (such that i!=K), count divisors of num in range[1, N]. And store their occurrences in variable divisors.If divisors=count means num has the same divisors as K in range [1, N]. increment count of such numbers.Let’s understand with examples.Input ... Read More

Counting Cross Lines in an Array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:34:55

264 Views

We are given with an array of distinct elements that are unsorted. The goal is to find the cross lines after the array is sorted. Cross lines are counted as shown below −Arr[]={ 1, 2, 4, 3, 5 } There are 3 cross lines as shown belowArr[]= { 1, 2, 3, 4, 5 }. There are no cross lines as the array is already sorted.We will count the cross lines using insertion sort in which an element from right is added to sorted elements on its left. Each time the element is added in the sorted part, increment count as ... Read More

Count Total Divisors of A or B in a Given Range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:26:38

397 Views

We are given four integers L, R, A and B. The goal is to find the count of numbers in range [L, R] that fully divide either A or B or both.We will do this by traversing from L to R and for each number if number%A==0 or number%B==0 then increment count of divisors.Let’s understand with examples.Input − L=10, R=15, A=4, B=3Output − Count of divisors of A or B − 2Explanation −Number 12 is fully divisible by 3 and 4. Number 15 is fully divisible by 3 only. Total divisors=2Input − L=20, R=30, A=17, B=19Output − Count of divisors ... Read More

Advertisements