Problem · Stack
EasyHAHackerearthOA

Problem statement

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

solve(N: int, S: String) → int[]

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

  1. N: Represents the number of people
  2. 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.
  • Return Value

    Return an integer array containing, for each boy in arrival order, the ID of the girl he dances with or -1 when no girl is available.

    Note:

    Your code is run against multiple hidden test cases, so the returned array must satisfy the contract for every valid input.

    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

    Examples

    Example 1

    N = 3S = "011"return = [1, -1]
    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]

    Example 2

    N = 11S = "10001111110"return = [-1, 4, 3, 2, -1, -1, -1]
    🌷 Will add once find reliable reference :)

    Example 3

    N = 7S = "0000100"return = [4]
    🌷 Will add once find reliable reference :)

    Constraints

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

    More Hackerearth problems

    drafts saved locally
    public int[] solve(int N, String S) {
      // write your code here
    }
    
    N3
    S"011"
    expected[1, -1]
    checking account