Drill

ProblemsC++ › events

How many openings in the coming weeks

mediumeventsArraysMathC++

A recurring weekly pattern marks which of the seven weekdays are open. Count the total openings over a number of full weeks.

weeklyOpenings(pattern: list<bool>, weeks: int) → int

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int weeklyOpenings(std::vector<bool> pattern, int weeks) {
    
}

Worked examples

CallResult
weeklyOpenings(std::vector<bool>{true, false, true, false, true, false, false}, 4)12
weeklyOpenings(std::vector<bool>{false, false, false, false, false, false, false}, 10)0
weeklyOpenings(std::vector<bool>{true, true, true, true, true, true, true}, 2)14
weeklyOpenings(std::vector<bool>{true, false, false, false, false, false, false}, 0)0

Hint

Count the true entries in the pattern once, then multiply.

Reference solution in C++
int weeklyOpenings(std::vector<bool> pattern, int weeks) {
    int perWeek = 0;
    for (bool d : pattern) if (d) perWeek++;
    return perWeek * weeks;
}

The same problem in another language

More events problems in C++