-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathnumber-is-sparceor-not.cpp
More file actions
41 lines (37 loc) · 914 Bytes
/
Copy pathnumber-is-sparceor-not.cpp
File metadata and controls
41 lines (37 loc) · 914 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
40
41
// https://practice.geeksforgeeks.org/problems/number-is-sparse-or-not/0/?track=md-bm&batchId=144
#include <iostream>
#include <string>
using namespace std;
bool isSparse(int number)
{
// Test case 1:
if(number <=2)
return true;
// Convert number to binary representation
string binary_representation = "";
while(number > 0)
{
binary_representation.push_back(char(number % 2));
number /= 2;
}
// Check if it is parse or not
for(int i =0; i < binary_representation.size()-1; i++)
{
if(binary_representation[i] == 1 && binary_representation[i] == binary_representation[i+1])
return false;
}
return true;
}
int main()
{
int t, number;
cin >> t;
while(t--)
{
cin >> number;
if(isSparse(number))
cout << 1<<endl;
else
cout << 0 << endl;
}
}