Move Bits to Right
Learn this problemProblem statement
An engineer is tasked with developing an integrated binary circuit that processes an input binary string ( s ). This binary string, which consists solely of 0s and 1s, needs to be rearranged so that all 1s are moved to the end of the string. For example, the string "01010" should be transformed into "00011".
To achieve this, the engineer can perform a series of operations where any "1" from the string can be moved to the right. The operation cost is defined as ( 1 + ) the number of positions the "1" is moved. For instance, in the string "100010", moving the first "1" three positions to the right costs ( 1 + 3 = 4 ). Each "1" must be moved as far to the right as possible during the operation.
Given a binary string ( s ), determine the maximum total cost of operations required to segregate the string so that all 1s are at the end.
Function
getMaxOperationCost(s: String) → intExamples
Example 1
s = "110100"return = 13The target string should be "000111". To maximize the number of operations, the process would be:
- Move the second character to the third position at a cost of 2. The string becomes "101100".
- Move the first character to the second position at a cost of 2. The string becomes "011100".
- Move each "1" two positions to the right, each at a cost of 3.
The total cost is ( 2 + 2 + 3*3 = 13 ). Therefore, the maximum number of operations required to segregate the string is 13.
Constraints
🐹More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026