Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Buses
MEDIUM
15 mins
17 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Buses

Contributed by
Omkar Deshmukh
Medium
yellow-spark
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share
17 upvotes

Problem Statement

You are given a vector of 'N' integers denoting the number of buses that can be boarded from the i-th position. The bus stops only at stops whose number is a multiple of the bus stop number from which the bus originates. You need to find the number of buses originating from each bus stop from 1 to 'N'.

For example:

If 'N' = 4 and the given vector is: [1 3 4 3].

1 bus can be boarded from the first bus stop which means that 1 bus originates from the first bus stop.

3 buses can be boarded from the second bus stop which means that (3 - 1 = 2) buses originate from the second bus stop. This is because the bus originating from the first stop will stop at the second stop as well.

4 buses can be boarded from the third bus stop which means that (4-1 = 3) buses originate from the third bus stop. This is because the bus originating from the first stop will stop at the third stop as well.

3 buses can be boarded from the fourth bus stop which means that (3-3 = 0) buses originate from the fourth bus stop. This is because the buses originating from the first and second stop will stop at the fourth stop as well.

So the final vector would be: [1 2 3 0].

Note:

The given vector uses 1-based indexing.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
3
4
1 3 4 3
5
1 2 3 4 5
3
4 4 4
Sample Output 1:
1 2 3 0 
1 1 2 2 4 
4 0 0
Explanation For Sample Input 1:
For the first test case, the explanation is already given.

For the second test case, the given vector is: [1 2 3 4 5].
1 bus can be boarded from the first bus stop which means that 1 bus originates from the first bus stop.
2 buses can be boarded from the second bus stop which means that (2-1 = 1) buses originate from the second bus stop. This is because the bus originating from the first stop will stop at the second stop as well.
3 buses can be boarded from the third bus stop which means that (3-1 = 2) buses originate from the third bus stop. This is because the bus originating from the first stop will stop at the third stop as well.
4 buses can be boarded from the fourth bus stop which means that (4-1-1 = 1) buses originate from the fourth bus stop. This is because the buses originating from the first and second stop will stop at the fourth stop as well.
5 buses can be boarded from the fifth bus stop which means that (5-1 = 4) buses originate from the fifth bus stop. This is because the bus originating from the first stop will stop at the fifth stop as well.
So the final vector would be: [1 1 2 2 4]

For the third test case, the given vector is: [4 4 4].
4 buses can be boarded from the first bus stop which means that 4 buses originate from the first bus stop.
4 buses can be boarded from the second bus stop which means that (4-4 = 0) buses originate from the second bus stop. This is because the bus originating from the first stop will stop at the second stop as well.
4 buses can be boarded from the third bus stop which means that (4-4 = 0) buses originate from the third bus stop. This is because the bus originating from the first stop will stop at the third stop as well.
So the final vector would be: [4 0 0]
Sample Input 2:
3
1
5
2
5 6
4
1 3 6 8
Sample Output 2
5 
5 1 
1 2 5 5
Reset Code
Full screen
Auto
copy-code
Console