DSA Factory
Free lessonsProgramming basics · Stage 1 · Numbers · Step 1

Dividing and what's left

Share things out evenly and find out how many are left over. About 8 minutes.

Sharing sweets

You have 14 sweets to share among 4 friends, and nobody may get a broken sweet. Each friend gets 3, which uses up 12 sweets. Two sweets are left over.

Computers call the first part whole-number division: how many full groups fit. The leftover is called the remainder. Python writes whole-number division with two slashes, and the other languages use a single slash when both numbers are whole.

Whole-number division: counts the full groups.
The remainder: is what is left over.
In code
full = 14 // 4      # 3
left = 14 % 4       # 2
14 sweets, groups of 4
1
0
2
1
3
2
4
3
5
4
6
5
7
6
8
7
9
8
10
9
11
10
12
11
13
12
14
13
count
groups
1
left over
10

The first group of 4 sweets is handed out.

Move 1 of 4

Hours and minutes

A clock does this all day long. You have 135 minutes, and an hour is 60 minutes. Divide 135 by 60 and you get 2 full hours. The remainder is 15, so there are 15 minutes left.

Whenever a question says "how many full ones" and "how many are left", reach for these two tools. Use the same number for both: the size of one group.

Both use the same size: the number of things in one group.
In code
total = 135
hours = total // 60
minutes = total % 60
Quick check

17 sweets are shared among 5 friends, and nobody gets a broken sweet. How many does each friend get, and how many are left over?

  1. A3 each, 2 left over
  2. B2 each, 3 left over
  3. C4 each, none left
Show the answer

3 each, 2 left over. 5 friends times 3 is 15, and 17 minus 15 leaves 2.

Your problem

Hours and minutes

A video lasts a given number of minutes in total. Return how many full hours it lasts and how many minutes are left, as a list: hours first, then minutes.

Example
total = 135 → [2, 15]

0 ≤ total ≤ 1,000,000

Solve it in your browserPython, C++, Java or JavaScript. Hints if you get stuck. No sign-up needed.
Start coding