Skip to content

Commit 5a6708a

Browse files
committed
2864. Maximum Odd Binary Number
1 parent 4e5bbf4 commit 5a6708a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Maximum Odd Binary Number/kata.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package kata
2+
3+
func maximumOddBinaryNumber(s string) string {
4+
var countOnes int
5+
var countAll int
6+
for _, digit := range s {
7+
if digit == '1' {
8+
countOnes++
9+
}
10+
countAll++
11+
}
12+
13+
if countOnes == 0 {
14+
return s
15+
}
16+
17+
maximumOddBinary := make([]byte, countAll)
18+
countOnes--
19+
maximumOddBinary[countAll-1] = '1'
20+
for i := 0; i < countAll-1; i++ {
21+
if countOnes > 0 {
22+
maximumOddBinary[i] = '1'
23+
countOnes--
24+
} else {
25+
maximumOddBinary[i] = '0'
26+
}
27+
}
28+
return string(maximumOddBinary)
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy