Data Structures & AlgorithmsVery hardCodeforces Div2 C · TCS NQT
Each number in [1, 2, 3, 4, 5, 6, 7] gets a sign '+' or '-', and all are then added. How many sign choices give a total of exactly 4?
- A. 8
- B. 128
- C. 2
- D. 4
Correct answer: A. 8
Signing numbers so the total is T means choosing a subset P with sum (total + T)/2 to be positive. So the answer is the number of subsets with that sum, which is a subset-sum count rather than a choice of 2ⁿ sign patterns.
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
- How many NON-EMPTY subsets of [4, 7, 2, 9, 5, 3, 8, 6] have a sum divisible by 3? Subsets are chosen by position.
- Items as (weight, value) are [(3, 5), (4, 6), (5, 9), (7, 13), (2, 8)]. Each item can be used at most once. What is the maximum value of a…
- From the numbers [17, 22, 9, 35, 41, 13, 28], choose a subset (each number used at most once) whose sum is as large as possible without…
- Four workers (rows) and four tasks (columns) have cost matrix [[9, 2, 7, 8], [6, 4, 3, 7], [5, 8, 1, 8], [7, 6, 9, 4]], where cost[i][j]…
- Four cities have symmetric travel costs D = [[0, 27, 24, 21], [27, 0, 30, 17], [24, 30, 0, 1], [21, 17, 1, 0]]. Starting at city 0, you…
- A directed graph on vertices 0 to 3 has adjacency matrix [[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 0]] (adj[i][j] = 1 means an…