Skip to content

Commit d3d0246

Browse files
committed
find the next sparse number
1 parent 4237b31 commit d3d0246

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
100100
- [] Count total set bits in all numbers from 1 to n
101101
- [x] [Rotate bits of a number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/rotate_bits)
102102
- [] Count number of bits to be flipped to convert A to B
103-
- [] Find Next Sparse Number
103+
- [x] [Find Next Sparse Number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/next_sparse)
104104

105105
## [String / Array](https://github.com/danrusei/algorithms_with_Go/tree/main/strings)
106106

bitwise/next_sparse/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Find Next Sparse Number
2+
3+
Source: [GeeksforGeeks](https://www.geeksforgeeks.org/given-a-number-find-next-sparse-number/amp/)
4+
5+
Given a number x, find the smallest Sparse number which greater than or equal to x.
6+
A number is Sparse if there are no two adjacent 1s in its binary representation. For example 5 (binary representation: 101) is sparse, but 6 (binary representation: 110) is not sparse.
7+
8+
## Example
9+
10+
```bash
11+
Input: x = 6
12+
Output: Next Sparse Number is 8
13+
14+
Input: x = 4
15+
Output: Next Sparse Number is 4
16+
17+
Input: x = 38
18+
Output: Next Sparse Number is 40
19+
20+
Input: x = 44
21+
Output: Next Sparse Number is 64
22+
```
23+
24+
## Algorithm
25+
26+
There is a simple solution which check if the number is sparse, if not it moves to the next decimal number and so on.
27+
Although this is a valid solution is not so efficient, a more efficient solution is described below:
28+
29+
* store the binary of a given number in a slice
30+
* initialize the last finalized bit position as 0
31+
* traverse the slice, and start with least significatn bit (from right to left)
32+
* if we find two adjacent 1's such that next (or third) bit is not 1, then:
33+
* Make all bits after this 1 to last finalized bit 0
34+
* Update last finalized bit as next bit
35+
36+
## Result
37+
38+
```bash
39+
$ go run main.go
40+
next sparse number starting from 38 is 40
41+
```

bitwise/next_sparse/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func nextSparse(x int) int {
6+
7+
// bin store the binary representation of x
8+
// bin[0] contains least significant bit
9+
bin := []int{}
10+
for x != 0 {
11+
bin = append(bin, x&1)
12+
x >>= 1
13+
}
14+
15+
last := 0
16+
17+
for i := 1; i < len(bin); i++ {
18+
19+
if bin[i] == 1 && bin[i-1] == 1 && bin[i+1] != 1 {
20+
21+
// make the next bit 1
22+
bin[i+1] = 1
23+
24+
// make all bits before current bit as 0 to make
25+
// sure that we get the smallest next number
26+
for j := i; j >= last; j-- {
27+
bin[j] = 0
28+
29+
}
30+
31+
// store position of the bit set so that this bit
32+
// and bits before it are not changed next time.
33+
last = i + 1
34+
}
35+
}
36+
37+
//create and return the decimal equivalent of modified bin[]
38+
result := 0
39+
for i := 0; i < len(bin); i++ {
40+
result += bin[i] * (1 << i)
41+
}
42+
43+
return result
44+
}
45+
46+
func main() {
47+
x := 38
48+
y := nextSparse(x)
49+
50+
fmt.Printf("next sparse number starting from %d is %d\n", x, y)
51+
52+
}

0 commit comments

Comments
 (0)