Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:03:24+00:00 2026-06-15T08:03:24+00:00

I am trying to solve Ball Removal problem on topcoder , pasting the problem

  • 0

I am trying to solve Ball Removal problem on topcoder, pasting the problem statement here as login is required to access this link.


Problem Statement

You have N balls, where N is odd. The balls are numbered from 0 to N-1. In that order, they are arranged into a row going from the left to the right.

In addition to the number, each ball has either the word “left” or the word “right” written on it. For simplicity, we will use the character ‘<‘ instead of “left”, and the character ‘>’ instead of “right”. You are given the labels on all balls as the String label. For each i, character i of label represents the word on ball i.

You will now repeat the following procedure:

  • Choose a ball that is not at either end of the row of balls.
  • If the chosen ball has the label ‘<‘, remove the chosen ball and also the ball immediately to the left of it. Otherwise, remove the chosen ball and also the ball to the right of it.
  • Without reordering the remaining balls, push them together to get rid of the gap created in the previous step.

The process ends when only one ball remains in the row. That ball is called the survivor. Note that the numbers on the balls do not change during the process.

Find all possible survivors. Your method must return a String containing exactly N characters. If ball i can be the survivor, character i of the return value must be ‘o’ (lowercase oh). Otherwise, the corresponding character must be ‘.’ (a period).

Constraints

  • label will contain between 3 and 49 characters, inclusive.
  • label will contain an odd number of characters.
  • Each character of label will be either ‘>’ or ‘<‘.

Examples
“<<>”
Returns: “..o”

Initially, you have three balls. Since you cannot choose balls at the ends of the row, you have to choose ball 1. As its label is ‘<‘, you remove balls 0 and 1. Hence the only possible survivor is ball 2.
1)
“>>><<”
Returns: “o…o”

If you choose ball 2 or ball 3 first, you have to choose ball 1 next, and the survivor will be ball 0. If you choose ball 1 first, you have to choose ball 3 next, and the survivor will be ball 4.

2)
“<<><<”
Returns: “….o”

3)
“<><<><>”
Returns: “o…..o”

4)
“>>><<<>>>>><<<>”
Returns: “o…..o.o…..o”


I am thinking of a dynamic programming approach to this problem, I am thinking of having an boolean array to mark which of the characters have been deleted and then find which is next left and next right but that makes the approach quite inefficient and I have to write a recursive method. For implementing a dynamic programming approach I need to maintain a state. But I am not able to figure out what I should keep as state, in my thinking a state is combination of both current string and current index, but maintaining a string for state doesn’t seem correct to me.

One more problem I am facing is that in this case I don’t have a particular direction if I change direction result changes also if I move left to right I might need to move right to left also.
Please help me in finding a proper approach to this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:03:26+00:00Added an answer on June 15, 2026 at 8:03 am

    The state can be boolean – DP[left][right][isLeftBoundary][isRightBoundary].

    This means if the substring starting at left and finishing at right can be fully eliminated.

    isLeftBoundary is just a boolean flag if the left symbol is the leftmost of the string.

    isRightBoundary is just a boolean flag if the right symbol is the rightmost of the string.

    if DP[0][i - 1][1][0] and DP[i + 1][N][0][1] are true, it means the ball at position i can remain.

        int canDelete(int l, int r, int st, int en)
        {
            if (l > r) return 1; //we succeeded in removing the whole string
    
            if (DP[l][r][st][en] != -1)
               return DP[l][r][st][en];
    
            int ans = 0;
    
            //i is the last removed ball, which will eliminate the whole string[l, r]
            for (int i = l + st; i <= r - en; i++) 
            {
                if (inp[i] == '<') //it will remove a ball to the left, but which one?
                {
                    for (int j = l; j < i; j++) //ball i will remove ball j
                    {       
                         if (canDelete(l, j - 1, st, 0) 
                          && canDelete(j + 1, i - 1, 0, 0) 
                          && canDelete(i + 1, r, 0, en))
                             ans = 1;       
                    }
                }
                else
                if (inp[i] == '>') //it will remove a ball to the right, but which one?
                {
                    for (int j = i + 1; j <= r; j++) //ball i will remove ball j
                    {       
                         if (canDelete(l, i - 1, st, 0) 
                          && canDelete(i + 1, j - 1, 0, 0) 
                          && canDelete(j + 1, r, 0, en))
                             ans = 1;       
                    }       
                }
            }
    
            return ans;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
Still trying to solve this puzzle. I have a more complete example here. The
I have been trying to solve this problem for a while, could not find
Trying to solve this problem: I have the following set of divs that when
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve the following problem with Puppet: I have multiple nodes.
Been trying to solve this error for ages now, hoping that someone here can
Trying to solve a problem of preventing duplicate images to be uploaded. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.