CHEAT - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: shubham0105
Tester: raysh07
Editorialist: iceknight1093

DIFFICULTY:

763

PREREQUISITES:

None

PROBLEM:

Given N, compute the number of Tuesdays in the next N days if the first day is a Monday.

EXPLANATION:

A week has seven days, and the first Tuesday is on day 2.
So, the Tuesdays occur at days 2, 9, 16, 23, \ldots

Since N \leq 1000, it suffices to run a loop and iterate through all possibilities till you reach N.
Alternately, some simple math says that the answer is

\left\lceil \frac{N-1}{7} \right\rceil

where \left\lceil \ \ \right\rceil denotes the ceiling function.

TIME COMPLEXITY

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (Python, brute force)
for _ in range(int(input())):
    n = int(input())
    ans = 0
    for x in range(2, n+1, 7): ans += 1
    print(ans)
Editorialist's code (Python, formula)
for _ in range(int(input())):
    n = int(input())
    print((n-1+6)//7) # ceil(a/b) = (a+b-1)//b