We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 588acbf commit f0e7ac8Copy full SHA for f0e7ac8
GuessTheNumberUsingBitwiseQuestionsI/solution.cpp
@@ -0,0 +1,26 @@
1
+/**
2
+ * Definition of commonSetBits API.
3
+ * int commonSetBits(int num);
4
+ */
5
+
6
+class Solution {
7
+public:
8
+ int findNumber() {
9
+ int result = 0;
10
11
+ // Iterate over each bit in a 30bit int (1 <= n <= 2^30 - 1)
12
+ for (int i=0; i<30; i++)
13
+ {
14
+ int num = (1 << i);
15
16
+ if (commonSetBits(num))
17
18
+ // if bit is set in num then it must be set in the number we are finding too
19
+ // So set it in the result
20
+ result |= num;
21
+ }
22
23
24
+ return result;
25
26
+};
0 commit comments