Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
This is a Codeforces problem: read standard input, print to standard output.
The first line has n (1 ≤ n ≤ 1000), the number of problems in a contest. Each of the next n lines has three numbers, one per friend. Each number is 1 if that friend is sure how to solve the problem, and 0 if not.
The friends only write a solution when at least two of them are sure. Print how many problems they will solve.
Example:
3
1 1 0
1 1 1
1 0 0The first problem has two friends sure, the second has three, the third only one. The answer is 2.
Building the intuition
For each line you need to know: how many of the three numbers are 1?
You could write out the cases (a == 1 and b == 1, or a == 1 and c == 1, or …), but there's a neater trick. Every value is either 0 or 1, so adding them up counts the 1s. 1 + 1 + 0 = 2 friends sure. 1 + 0 + 0 = 1 friend sure.
So the test for one problem is a + b + c >= 2. Do that for every line, and count how many lines pass. It's the same "loop over rows, compute something per row" shape as Richest Customer Wealth, but now you count instead of taking the max.
The optimal approach
- Read
nand setsolved = 0. - Repeat
ntimes: reada,b,c. Ifa + b + c >= 2, add 1 tosolved. - Print
solved.
Reading the input
This time there are many numbers spread over many lines. Two ways to think about it:
- Line by line: read a line, then split it into three numbers.
- Token by token: ignore lines and read numbers one at a time. Newlines are just spaces.
Token by token is usually simpler:
data = list(map(int, sys.stdin.read().split())) # [3, 1, 1, 0, 1, 1, 1, 1, 0, 0]
n = data[0]
# problem i's three numbers are data[1 + 3*i], data[2 + 3*i], data[3 + 3*i]int n;
cin >> n;
int a, b, c;
cin >> a >> b >> c; // cin doesn't care which line a number is onBufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine().trim());
StringTokenizer st = new StringTokenizer(in.readLine()); // splits one line on spaces
int a = Integer.parseInt(st.nextToken());const data = require("fs").readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
import sys
def main():
data = list(map(int, sys.stdin.read().split())) # all numbers, ignoring line breaks
n = data[0]
solved = 0
for i in range(n):
a, b, c = data[1 + 3 * i], data[2 + 3 * i], data[3 + 3 * i]
if a + b + c >= 2: # values are 0/1, so the sum counts who is sure
solved += 1
print(solved)
main()#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int solved = 0;
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a + b + c >= 2) { // values are 0/1, so the sum counts who is sure
solved++;
}
}
cout << solved << '\n';
return 0;
}import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine().trim());
int solved = 0;
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(in.readLine()); // one problem per line
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (a + b + c >= 2) { // values are 0/1, so the sum counts who is sure
solved++;
}
}
System.out.println(solved);
}
}// Codeforces: choose "Node.js" as the language
const data = require("fs").readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);
const n = data[0];
let solved = 0;
for (let i = 0; i < n; i++) {
const a = data[1 + 3 * i], b = data[2 + 3 * i], c = data[3 + 3 * i];
if (a + b + c >= 2) { // values are 0/1, so the sum counts who is sure
solved++;
}
}
console.log(solved);Dry run
| Line | a b c | Sum | ≥ 2? | solved |
|---|---|---|---|---|
| 1 | 1 1 0 | 2 | Yes | 1 |
| 2 | 1 1 1 | 3 | Yes | 2 |
| 3 | 1 0 0 | 1 | No | 2 |
Print 2.
Complexity
- Time: O(n). One check per problem, at most 1000.
- Space: O(1). We never store the lines; each one is checked and forgotten.
Edge cases and traps
> 2instead of>= 2. "At least two" includes exactly two. With> 2you only count problems all three are sure about.- Reading only the first line. In Java,
readLine()returns one line. Call it again for every problem. - Off-by-one in the token index. In the token approach, the first number is
n, so problem 0's numbers start at index 1. - All zeros or all ones. The answer is
0orn. Your loop handles both.
Say it in the interview
"Each value is 0 or 1, so the sum of a line is the number of friends who are sure. I loop over the n lines, add 1 to a counter when the sum is at least 2, and print the counter. Linear time, constant space."
Follow-ups interviewers ask
- "What if there were k friends and you needed a majority?" Sum the k values and check
sum > k / 2. - "What if the values were confidence scores, not 0/1?" Summing no longer counts people. Count the values above a threshold instead.
Practise the same idea
This is Day 7 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free