Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 B
You must perform exactly 3 operations on the array [4, -2, 5, -7, 3, -1]; each operation replaces one element by its negation (the same element may be negated again later). What is the largest possible sum after all operations?
- A. 22
- B. 20
- C. 26
- D. 2
Correct answer: A. 22
Flip the most negative numbers first, turning them positive. If operations remain after all negatives are flipped, spend the parity on the smallest absolute value: flipping it twice cancels out, flipping it once costs the least. Flipping the largest positive element instead is a common error.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the length of the longest substring of "aaabbcccbbaad" in which EVERY letter that appears in the substring appears at least 2 times?
- The universe is {1, ..., 8} and the available sets are [[1, 2, 3], [3, 4, 5], [5, 6, 7], [1, 7, 8], [2, 4, 6, 8]]. What is the minimum…
- How many subsets of {1, 2, ..., 7} have a sum exactly equal to half of 1 + 2 + ... + 7? (The complementary subset is counted separately.)
- How many non-empty subsets of [2, 3, 4, 5, 6] (chosen by position) have a sum that is a prime number?
- Five bitmasks ['0b11', '0b110', '0b1100', '0b1001', '0b101'] are available. Choose the fewest of them whose bitwise OR equals 0b1111. What…
- How many index triples i < j < k of [-4, -1, 0, 2, 3, -2, 5, 1] have a[i] + a[j] + a[k] = 0?