Found 7347 Articles for C++

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

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

135 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

357 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 the number of objects using Static member function in C++ Program

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

949 Views

The goal here is to count the number of objects of a class that are being created using a static member function.A static data member is shared by all objects of the class commonly. If no value is given, a static data member is always initialized with 0.A static member function can only use static data members of that class.We are using a class Student here. We will declare a static data member count which will store the count of objects. A static member function rollCall(void) which will display the count of objects as roll no.s of students in a ... Read More

Count the number of operations required to reduce the given number in C++

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

200 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 the number of pairs that have column sum greater than row sum in C++

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

117 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 the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++

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

898 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

612 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 the numbers divisible by ‘M’ in a given range in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:03:55

1K+ Views

We are given three numbers A,B and M. A and B define the range [A,B] of numbers.The goal is to count numbers between A and B that are divisible by M.We will start from i=A till first multiple of M. Increment count if i%M=0. Now increment i till i

Count Triplets such that one of the numbers can be written as sum of the other two in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:02:31

1K+ Views

We are given an array Arr[] of integers with length n. The goal is to find the number of triplets (Arr[i],Arr[j],Arr[k]) such that the sum of any two numbers is equal to the third number.a+b=c, where a,b,c are elements of Arr[] with indexes i,j,k such that 0

Counting cross lines in an array in C++

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

147 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

Advertisements