-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path2_Power_K.cpp
More file actions
39 lines (37 loc) · 673 Bytes
/
2_Power_K.cpp
File metadata and controls
39 lines (37 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cmath>
using namespace std;
int powerOfTwo(int arr[],int k)
{
int value=0;
int low=0, high=10, mid=0;
while(low<high)
{
mid = low + (high-low)/2;
if((((int)k/mid)*mid)>1)
{
int i = (k/mid)*mid;
while(i--)
{
value = value + pow(2,mid);
}
k = k - (k/mid)*mid;
}
if(k<mid)
high = mid-1;
}
return value;
}
int main()
{
int k;
cin>>k;
int arr[11];
for(int i=0; i<11; i++)
{
arr[i] = pow(2,i);
}
int ans = powerOfTwo(arr,k);
cout<<ans<<endl;
return 0;
}