FLIPKART Coding Question – Solved

5 Live
Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to N) at some distance from each other. The player starts from any village S and travels through all villages in a circular route, returning to the starting village S. Each village provides energy drinks, which are used to travel to the next village. One energy drink allows travel of one unit distance. The player loses if they run out of energy drinks at any point. Leftover energy accumulates. At each village checkpoint, the player is given: 1) The number of energy drinks available in that village. 2) The distance from that village to the next village. Write a program to help the player choose the starting village S such that they can complete the full circular tour. If multiple starting points are valid, return the one with the smallest label (1-indexed). Assume a valid solution always exists. Input Format The first line contains an integer N, the number of villages. The next N lines each contain two integers: the number of energy drinks and the distance to the next village. Output Format Print the smallest index of the village (1-based) from which the player can start and complete the tour. Constraints - N ≥ 0 - 1 ≤ energy drinks, distance ≤ 10^9 (assumed from context) Sample Input 1 6 3 5 5 2 6 4 3 4 5 3 4 2 Sample Output 1 2 Explanation Starting at village 2: - Drinks: 5, distance: 2 → remaining: 3 - Next drinks: 6 → total: 9, distance: 4 → remaining: 5 - Next drinks: 3 → total: 8, distance: 4 → remaining: 4 - Next drinks: 5 → total: 9, distance: 3 → remaining: 6 - Next drinks: 4 → total: 10, distance: 2 → remaining: 8 - Next drinks: 3 → total: 11, distance: 5 → remaining: 6 The player successfully returns to starting point. Sample Input 2 4 5 3 2 4 3 7 8 4 Sample Output 2 4

Asked in: FLIPKART

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <iostream>
#include <vector>

using namespace std;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, start by carefully understanding the conditions and what is being asked: you have N villages arranged in a circle, each providing a certain number of energy drinks and requiring a certain distance to the next village to travel. The player consumes energy drinks equal to the distance traveled. The objective is to find the earliest village (smallest index) from which the player can start, collect energy drinks, and successfully complete the full circular journey without ever running out of energy.

Step 1: Analyze the Problem Structure
The problem is similar in nature to the classic "gas station" or "circular tour" problem often discussed in algorithmic challenges. The key idea is that you need to ensure that starting from some village, you never have negative leftover energy when moving through each village in order.

The player starts at village S with zero leftover energy. At each village, the player gains some energy drinks and then must spend some to travel to the next village. The leftover energy accumulates, and at no point should it drop below zero.

Step 2: Understand the Feasibility Condition
For the circular trip to be possible, the total amount of energy drinks across all villages must be at least the total distance that must be traveled around the circle. If the sum of all energy drinks is less than the sum of all distances, no solution exists. The problem states a solution always exists, so we can assume total drinks ≥ total distance.

Step 3: Iterate Through the Villages and Track Energy
One straightforward method is to simulate the journey from each possible starting village S, checking if the player can make the full circle without the energy dropping below zero. However, this brute force approach is inefficient, especially for large N (up to 10^5 or more).

Step 4: Leverage the Greedy Approach
A more efficient approach comes from a greedy insight:
- Keep a running total of leftover energy as you move from village to village.
- When the leftover energy becomes negative at some village, it means you cannot start from any village before or at the current one because the energy drops below zero before completing the circle. So you reset your start point to the next village.
- Continue this process until you reach the end of the list.
- The start village you end up with after this process will be the earliest valid starting point.

Why does this work? Because if you fail at village i starting from some earlier village, then no village between the earlier start and i can be a valid start (as you’d run out of energy earlier or at the same point). Thus, you can skip all these and move your start to i + 1.

Step 5: Calculate Running Sums and Identify Start
While iterating, track:
- The total net energy gain or loss (energy drinks minus distance) to confirm overall feasibility.
- The current leftover energy starting from the tentative start village.
If at any point leftover energy drops below zero, reset leftover energy to zero and move the start index to the next village.

Step 6: Final Result
After processing all villages, the chosen start index will be the smallest index (1-based) that allows the player to complete the circular tour.

Step 7: Additional Points and Edge Cases
- Since indexing is 1-based for output, remember to adjust indexes accordingly.
- Handle cases where energy drinks and distances are equal or large integers gracefully (use data types that can hold large sums).
- The circular nature means once you reach the last village, you must check the total feasibility to ensure the journey can wrap around.

Step 8: Summarize the Efficient Approach
- Verify total energy drinks ≥ total distance to ensure a solution exists.
- Initialize start index at 0, leftover energy at 0.
- Traverse the villages in order: at each village add (energy drinks - distance) to leftover energy.
- If leftover energy falls below zero, move start index to the next village and reset leftover energy to zero.
- After completing one full pass, the start index is the minimal valid starting village.

This approach runs in O(N) time, as it involves only a single pass through the array. It efficiently finds the minimal starting village to complete the circular tour without exhaustive checking of all possible starts.
```


Related Questions