Maximize weighted subsequence pairs with wildcards
AmazonSoftware Engineer · Online AssessmentLast updated
This question evaluates combinatorial counting, string manipulation, and optimization under constraints, along with handling large-number results via modular arithmetic.
You are given a string s of length n consisting only of characters '0', '1', and '!'. Each '!' can be replaced by either '0' or '1'.
For the final binary string, define:
count10= number of index pairs(i, j)withi < j,s[i] = '1',s[j] = '0'(a subsequence pair, not necessarily adjacent).count01= number of index pairs(i, j)withi < j,s[i] = '0',s[j] = '1'.
Given integers x and y, the total “error” is:
error = x * count10 + y * count01.
Return the maximum possible error over all replacements of '!', modulo 1_000_000_007.
Example: s = "101!1" (with given x, y).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.