Maximum Ones After K Operations
Updated
Problem statement
You are given a binary string s containing only '0' and '1'. You may perform at most k operations.
In one operation:
- Choose an index
isuch that0 ≤ i < s.length - 1. - Set
s[i] = max(s[i], s[i + 1]).
Each operation modifies only one position.
Return the maximum possible number of '1' characters in the final string.
Function
maximumOnes(s: String, k: int) → intExamples
Example 1
s = "10110"k = 1return = 4Choose i = 1. The string changes from "10110" to "11110", which contains 4 ones.
Example 2
s = "00011"k = 2return = 4Use the two operations to propagate a '1' leftward: "00011" becomes "00111" and then "01111". The final string contains 4 ones.
Constraints
1 ≤ s.length ≤ 2 * 10^50 ≤ k ≤ s.lengthscontains only'0'and'1'.
More Goldman Sachs Online Assessment questions
- Palindrome CounterOA · Seen Oct 2026
- Longest Palindromic SubsequenceOA · Seen Oct 2026
- Count Redundant File BackupsOA · Seen Oct 2026
- Minimum Distinct Prefix CostOA · Seen Oct 2026
- Merge Overlapping IntervalsOA · Seen Sep 2026
- Data ReorganizationOA · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsOA · Seen Jul 2026
- Find the Winner of an Array GameOA · Seen Jun 2026
- Minimum Number of Taps to Water a GardenOA · Seen Jun 2026
Similar questions at other companies
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Prepare NotificationMicrosoft · OAMicrosoft interview questions
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
- Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
- Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
- Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase interview questions
- Missing WordsVanguard · OAVanguard interview questions
- Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
- Smallest String After at Most One DeletionLSEG · OALSEG interview questions