Programming Articles - Page 1746 of 3366

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

370 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

5K+ 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

559 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

284 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

722 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

377 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

221 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

490 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 02-Dec-2024 00:23:52

2K+ 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) will display the count of objects as roll no.s of students ... Read More

Advertisements