Flipkart Coding Question – Solved

2 Live
Recruitment Software - Application Display Update There are N applicants who have applied for a job vacancy. Each applicant has an application ID ranging from 1 to N. During the initial round of recruitment, the company's software assigns each applicant a score value, which can be positive or negative: - A positive score indicates the applicant is qualified for the vacancy. - A negative score indicates the applicant is not qualified and is not visible to the recruiter. Recruiter's Request The recruiter wants access to all N applications, but they have a constraint of performing a maximum of K iterations to change the score values. Operations Allowed In each iteration, the recruiter can change the score value of the ith applicant from positive to negative or negative to positive. If there is an (i+1)th applicant, the software automatically changes their score as well. The goal is to ensure that all N applicants become visible to the recruiter in at most K iterations. Input Format First line: An integer application_size (N), representing the total number of applicants. Second line: N space-separated integers, representing the score values of all N applicants. Third line: An integer iterations (K), representing the maximum number of iterations allowed. Output Format Print space-separated integers, representing the application IDs whose scores must be changed by the recruiter in order to make all N applications visible.

Asked in: Flipkart

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


n = int(input())
arr = list(map(int, input().split()))
k = int(input())
changed = False
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, start by clearly understanding the problem constraints and goals. You have N applicants, each with a score that can be positive or negative. Positive means visible, negative means invisible. The recruiter wants all applicants to be visible (all scores positive) using at most K iterations. Each iteration involves flipping the score of a chosen applicant and also automatically flipping the next applicant’s score. The question is which applicants' scores to flip to achieve visibility for all within the allowed iterations.

First, analyze the nature of the flipping operation. When you flip the ith applicant’s score, you also flip the (i+1)th applicant’s score simultaneously. This means a single iteration affects two adjacent applicants at once. The cascading nature of this operation means the state of adjacent applicants is linked through each flip.

Think about the problem as transforming a sequence of binary states: positive (visible) or negative (invisible). You want to convert all negative scores to positive using the minimum or at most K flips. The sequence transformation can be viewed as a series of toggle operations on pairs of adjacent elements. Because each operation flips two adjacent elements, the problem becomes similar to "making all elements 1s in a binary array by flipping pairs of bits."

Next, consider a greedy strategy based on sequential processing. Start from the first applicant and move through the list in order. At each applicant, check if the score is positive or negative:

- If the current applicant is already positive, no flip is needed at this position, so move to the next applicant.
- If the current applicant is negative, perform a flip operation at this applicant (flip this and the next one). This operation ensures the current applicant becomes positive, but it also toggles the next applicant's state. Since you're processing sequentially, flipping here will fix the current applicant and move the problem forward.

Continue this process until you reach the second-last applicant because flipping at the last applicant affects only them and the next, which doesn’t exist. Keep track of all flip positions (applicants where flips are done) to meet the output requirement.

Now, analyze what happens at the last applicant. Because flipping at the last applicant affects only them and no next applicant, if the last applicant is still negative at the end, it might not be possible to fix them with the allowed operations. The problem constraints imply all applicants must be visible, so if the last applicant is negative after the sequential process, then the solution is not feasible under the current K limit.

Check if the number of flips used in your greedy process is within the allowed K iterations. If it is, output the indices of flipped applicants. Otherwise, indicate that the problem cannot be solved within the given iteration limit.

Also, consider the edge cases:
- When N=1, flipping affects only the single applicant, so flipping once might suffice if negative.
- When all scores are already positive, no flips are needed.
- When K=0, no flips are allowed; check if all are positive or not.

Another perspective is to think of the flipping operation as a domino effect on the array of scores. Each flip changes the current and the next, so your goal is to plan flips that cascade changes correctly. The greedy approach of scanning left to right and flipping when you encounter a negative score is intuitive and effective because it fixes the immediate problem and progresses forward without revisiting fixed elements.

In summary, the approach steps are:
1. Represent scores as positive/negative binary states.
2. Iterate over applicants from 1 to N-1.
3. When a negative score is encountered, flip at that position, affecting that applicant and the next one.
4. Record flip positions.
5. After the pass, check if the last applicant is positive.
6. Verify the total flips do not exceed K.
7. Output the flip positions if constraints are met; otherwise, indicate no solution.

By thinking in this way, you transform the problem into a systematic sequence of toggling operations with local dependencies, solved by a greedy linear scan approach. This avoids unnecessary complexity while respecting the problem constraints and achieving the desired outcome efficiently.
```


Related Questions