Buses Origin Problem Statement

You have been provided with an array where each element specifies the number of buses that can be boarded at each respective bus stop. Buses will only stop at locations that are multiples of the bus stop they originate from. Your task is to determine how many buses originate from each bus stop in the range from 1 to N.

Input:

The first input line contains the integer 'T', the number of test cases to process. 
For each test case, there are two lines of input:
The first line contains the integer 'N', the length of the array.
The second line contains 'N' space-separated integers representing the number of buses that can be boarded at each bus stop.

Output:

For each test case, output 'N' integers indicating the number of buses originating from each bus stop from 1 to N.

Example:

If 'N' = 4 and the array is: [1, 3, 4, 3] 
The result would be: [1, 2, 3, 0]

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 104
  • 1 ≤ Ai ≤ 106

Note:

The bus stops and the array use 1-based indexing.

Sai Iyer
2y

vector<int> countBuses(vector<int> arr, int n){ arr.insert(arr.begin(), -1); for(int i=1; i<=n; i++) for(int j=i*2; j<=n; j=j+i) arr[j]=arr[j]-arr[i]; arr.erase(arr.begin()); return arr; }

This is C++

Anonymous
2y

The language is c++

vector<int> countBuses(vector<int> arr, int n){ arr.insert(arr.begin(), -1); for(int i=1; i<=n; i++) for(int j=i*2; j<=n; j=j+i) arr[j]=arr[j]-arr[i]; arr.erase(arr.begin()); retur...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter