Hackerearth | OA | The Prom
Anonymous User
289

The other question asked in the same OA - https://leetcode.com/discuss/interview-question/5856794/Hackerearth-or-OA-or-Happy-Neighbourhood

The Prom
N people stand in a line with IDs from 1 to N to enter their high school's prom.

Given a string S, where '0' indicates girls and '1' indicates a boy. When a boy arrives, he pairs up with the girl standing immediately ahead him in line. They then leave the line to go to the dance floor.

To ensure that everyone at the prom gets one dance, each boy finds the ID of the girl who he danced with. If a boy cannot go on a date, the answer is -1.

Function description

Complete the function solve. This function takes the following 2 parameters:

  • N: Represents the number of people
  • S: Represents the binary string denoting the genders

Input format for custom testing

Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code.

  • The first line contains a single integer N denoting the number of people.
  • The second line contains a binary string S of size N denoting the genders of the people.

Output Format

Print space-separated integers denoting the IDs of the girls.

Constraints
1 ≤ N ≤ 10^5
S[i] ∈ {0,1} ∀ i ∈[1,N]

Sample Input          Sample Output
3                     1 -1
0 1 1

Explanation

Given:

  • N = 3
  • S = "011"

Approach:

  • Person 1(girl) joins the line
  • Person 2 (boy) looks for a girl standing ahead (person 1) and both of them go on a date.
  • Person 3 (boy) looks for a girl, but as there is no girl standing, the answer is -1.

So answer = [1, -1]

Note:
Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.

Limits
Time Limit: 1.0 sec(s) for each input file
Memory Limit: 256 MB
Source Limit: 1024 KB

Scoring
Score is assigned if any testcase passes

Test case 1

Input                       Expected Output
11                          -1 4 3 2 -1 -1 -1
10001111110

Test case 2

Input                      Expected Output
7                          4
0000100

Working Solution

public static String solve(int N, String S) {
	// Stack to keep track of the indices of girls
    Stack<Integer> stack = new Stack<>();
	// StringBuilder to build the result
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < N; i++) {
        char ch = S.charAt(i);

        if (ch == '0') {
			// It's a girl, push her index (1-based) onto the stack
            stack.push(i + 1);
        } else if (ch == '1') {
			// It's a boy, find the nearest girl
            if (!stack.isEmpty()) {
				// Pop the nearest girl's index
                result.append(stack.pop()).append(" ");
            } else {
                // No girl available
				result.append("-1").append(" ");
            }
        }
    }

	return result.toString().trim();
}
Comments (2)