-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindingBinaryInDecimal.cpp
More file actions
82 lines (55 loc) · 1.23 KB
/
findingBinaryInDecimal.cpp
File metadata and controls
82 lines (55 loc) · 1.23 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Finding Binary in Decimal
// Level MEDIUM
// Given an integer n, the task is to find the number of integers between 1 to n whose decimal number representation contain only 0s and 1s.
// Note : You have to return the count.
// Input Format :
// Line 1 : Integer n
// Constraints :
// 1 ≤ n ≤ 10^9
// Sample Input :
// 10
// Sample Output :
// 2
// Sample Output Explanation :
// Between 1 and 10 ,there are only 2 numbers having only 0 and 1 which are 1 and 10.
#include<bits/stdc++.h>
using namespace std;
pair<int, int> noOfDigits(int n) {
int count = 0;
int index = 0;
int temp = n;
while(n > 0) {
int d = n%10;
n /= 10;
count++;
if(d != 0 && d != 1) {
index = count;
}
}
int new_n = temp/pow(10, index);
return make_pair(new_n, index);
}
int countOfNumbers(int n)
{
// Write your code here
pair<int, int> d = noOfDigits(n);
int binaryCount = 0;
int index = d.second;
while(d.first > 0) {
binaryCount += ((d.first % 10) * (1<<index));
index++;
d.first /= 10;
}
index = d.second;
for(int i = index; i > 0; i--) {
binaryCount += (1 << (i-1));
}
return binaryCount;
}
int main() {
int n;
cin >> n;
int ans = countOfNumbers(n);
cout << ans << endl;
return 0;
}