Found 7347 Articles for C++

Count Pairs from two arrays with even sum in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:59:58

230 Views

We are given two arrays of integer type elements let’s say, arr_1[] and arr_2[] and the task is to pick one element from arr_1[] and another element from arr_[] to form a pair then calculate the sum of elements in the pair and check whether the resultant sum is even or not.Input int arr_1[] = {2, 3, 7, 1, 4} int arr_2[] = { 2, 4, 1, 3}Output Count Pairs from two arrays with even sum are: 10Explanation We will form the pairs using both the arrays and the pairs so formed are-: (2, 2) = 4(valid), (2, 4) = 6(valid), (2, 1) ... Read More

Count pairs whose products exist in array in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:58:02

100 Views

We are given an array of integer type elements and the task is to form the pairs from the given array and calculate the product of elements in the pair and check whether the given product is present in the given array or not.Input − int arr[] = {6, 2, 3, 1, 5, 10}Output − Count of pairs whose products exist in same array are − 7Explanation − The pairs that can be formed from the given array are: (6, 2), (6, 3), (6, 1), (6, 5), (6, 10), (2, 3), (2, 1), (2, 5), (2, 10), (3, 1), (3, ... Read More

Count numbers which are divisible by all the numbers from 2 to 10 in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:55:38

321 Views

We are given a number let’s say, num and the task is to calculate the count of numbers in the range 1 to num that are divisible by 2, 3, 4, 5, 6, 7, 8, 9 and 10.Input − int num = 10000Output − Count numbers which are divisible by all the numbers from 2 to 10 are: 3Explanation − There are 3 numbers from 1 to 10000 that are divisible by all the numbers starting from 2 till 10 and those are −2520-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, ... Read More

Count unset bits of a number in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:52:23

568 Views

We are given an integer number let’s say, num and the task is to firstly calculate the binary digit of a number and then calculate the total unset bits of a number.Unset bits in a binary number is represented by 0. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 0 is known as unset bit in the terms of the computer.Input − int number = 50Output − Count of total unset bits in a number are − 3Explanation − Binary representation of a number ... Read More

Count unset bits in a range in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:49:42

178 Views

We are given an integer number let’s say, num and the range with left and right values. The task is to firstly calculate the binary digit of a number and then set the loop from the left digit till the right digit and then in the given range calculate the unset bits.Unset bits in a binary number is represented by 0. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 0 is known as unset bit in the terms of the computer.Input − int number ... Read More

Count of lines required to write the given String in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:47:38

323 Views

We are given a string Str of alphabets and an array widths[]containing width of all English alphabets. The goal is to find the number of lines required to print this string on a page which has a width of 10 characters. Also print remaining characters.We will traverse the string check width of the current character and add it, if this sum >=10 increment number of lines.Let’s understand with examples.Input Str = "ababababab" widths[] = {2, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1};Output Count of ... Read More

Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:44:50

117 Views

We are given two arrays containing prime and non prime numbers. The goal is to find the count of distinct sums of pairs of prime numbers in each array.We will do this by making a pair of two primes from each array, take their sum and add them to set sums. In the end the size of the set is the number of distinct sums of primes.Let’s understand with examples.Input Arr1[] = { 1, 2, 3 } Arr2[] = { 2, 3, 4}Output Distinct Sums of primes :3Explanation Prime pairs (2, 2), (2, 3), (3, 2), (3, 3). Unique sums are 4, 5, ... Read More

Count ways to spell a number with repeated digits in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:42:33

115 Views

We are given a number containing many repeated digits as a string. The goal is to find the number of ways to spell it. For example 112233 can be spelled as double one, double two double three or one one two two three three.We will do this by checking continuous numbers. If the number is “13” there is only one way to spell it as “one three” (20). If digits are “113” ways “double one three”, “one one three” (21). So, the approach is to count the one continuous digit in string and multiply 2(count-1) with the previous result.Let’s understand ... Read More

Count ways to reach the nth stair using step 1, 2 or 3 in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:40:34

381 Views

We are given a total number of steps in a staircase that is n. A person can reach the next floor by skipping 1, 2 or 3 steps at a time. The goal is to find the number of ways in which the next floor can be reached by doing so.We will use the recursive method by keeping in mind that to reach any i’th step, a person has to jump from i-1th step ( skip 1 step) , i-2th step (skip 2 steps ) or i-3th step ( skip 3 steps ).Let’s understand with examples.Input N=3 stepsOutput Count of ways to ... Read More

Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:38:48

226 Views

We are given an input of N points on a 2-D space. The goal is to find the count of triplets of points from the input such that one point is the mid-point on the line between the other two. i.e if triplet is (A, B, C) then B is midpoint of A and C ( or any other combination of A, B, C ).We will do this by inserting all points as pairs into a vector. Then add all pairs from this vector in set. By taking two points from the set check if the sum of (x, ... Read More

Advertisements