
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Count numbers which can be constructed using two numbers in C++
- Count numbers which can be represented as sum of same parity primes in C++
- C++ program to count maximum possible division can be made in a graph with given condition
- C++ code to count maximum groups can be made
- List some ways by which water can be made safe for drinking.
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Program to count number of horizontal brick pattern can be made from set of bricks in Python
- C++ code to count number of times stones can be given
- Program to count indices pairs for which elements sum is power of 2 in Python
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Name five objects which can be made from wood.
