

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of numbers which can be made power of 2 by given operation in C++
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: 3
Explanation − 1+1=2 → 21 , 3+1=4 → 22 , 2=21 others will become 5+1=6, 6+1=7
Input − arr[]= {2,4,8,16 },
Output− Count of numbers that can become power of 2: 4
Explanation − All 4 numbers are already power of 2.
Approach used in the below program is as follows
We take an integer array arr[] initialized with random positive numbers.
Function powofTwo(int arr[],int n) takes an array and its length as input and returns a count of numbers that are or can be made power of two.
Take the initial count as 0.
Traverse array from i=0 to i<n.
For each element check if floor(log2(arr[i]))==ceil((log2(arr[i])) or floor(log2(arr[i]+1))==ceil((log2(arr[i]+1)) , if true increment count in both cases.
Return count as final result.
Example
#include <bits/stdc++.h> #include <math.h> using namespace std; int powofTwo(int arr[],int n){ int count=0; for(int i=0;i<n;i++){ if( floor(log2(arr[i])) == ceil(log2(arr[i])) ) { count++; } else{ ++arr[i]; if( floor(log2(arr[i])) == ceil(log2(arr[i])) ) { count++; } } } return count; } int main(){ int Arr[]={ 5,6,9,3,1 }; int len=sizeof(Arr)/sizeof(Arr[0]); cout<<endl<<"Count of numbers with power of 2 possible: "<<powofTwo(Arr,len); return 0; }
Output
If we run the above code it will generate the following output −
Count of numbers with power of 2 possible: 2
- Related Questions & Answers
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Count numbers which can be constructed using two numbers in C++
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Count numbers which can be represented as sum of same parity primes in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Program to count indices pairs for which elements sum is power of 2 in Python
- C++ code to count maximum groups can be made
- Program to count number of horizontal brick pattern can be made from set of bricks in Python
- C++ program to count maximum possible division can be made in a graph with given condition
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Reordered Power of 2 in C++
- C++ code to count number of times stones can be given
- Compute modulus division by a power-of-2-number in C#
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++