Number Distribution
Counting + Conditionals
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven N numbers, count how many are positive, how many are negative, and how many are zero.ReadHide
Problem Statement
Given N numbers, count how many are positive, how many are negative, and how many are zero.
Input / Output
Input:
Line 1: Integer N — count of numbers
Line 2: N space-separated integers
Output:
Three integers on separate lines:
count of positives
count of negatives
count of zeros
Example:
Input: 6
3 -1 0 7 -4 0
Output: 2
2
2
Constraints
1 ≤ N ≤ 10^5
-10^9 ≤ arr[i] ≤ 10^9
Understanding the Problem
- Walk through every number once
- Bucket each number into one of three categories: positive, negative, or zero
- Three counters, one loop — that's the entire solution
Edge Cases
- All zeros → pos=0, neg=0, zero=N
- All positive → neg=0, zero=0
- All negative → pos=0, zero=0
- Single element → still works fine
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Counting + Conditionals — Maintain separate counters and increment the right one based on a condition. This pattern appears constantly in array problems.
Approach
Optimized — O(n)
Initialize pos=0, neg=0, zero=0
For each number in array:
if number > 0 → pos++
if number < 0 → neg++
if number == 0 → zero++
Print pos, neg, zero
No brute force needed — this is already optimal.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Optimized | O(n) | O(1) | Single pass, three variables |
Video Resources
| Topic | Link |
|---|---|
| Arrays Basics — Striver | https://www.youtube.com/watch?v=37E9ckMDdTk |
Online Compiler
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(n) time | O(1) space
# Single pass — bucket each number into positive, negative, or zero
# ─────────────────────────────────────────────────────────────────────────
def distribute(arr):
pos = 0 # count of positive numbers
neg = 0 # count of negative numbers
zero = 0 # count of zeros
for num in arr:
if num > 0: # positive bucket
pos += 1
elif num < 0: # negative bucket
neg += 1
else: # zero bucket
zero += 1
return pos, neg, zero
# ── Pythonic one-liner alternative (same O(n)) ────────────────────────────
def distribute_pythonic(arr):
pos = sum(1 for x in arr if x > 0) # count positives
neg = sum(1 for x in arr if x < 0) # count negatives
zero = sum(1 for x in arr if x == 0) # count zeros
return pos, neg, zero
# ── Input ─────────────────────────────────────────────────────────────────
n = int(input())
arr = list(map(int, input().split()))
pos, neg, zero = distribute(arr)
print(pos) # positives
print(neg) # negatives
print(zero) # zerosOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language