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 9139461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:23:27+00:00 2026-06-17T09:23:27+00:00

This is an algoritm problem from Topcoder SRM 566 Div2. The problem can viewed

  • 0

This is an algoritm problem from Topcoder SRM 566 Div2.

The problem can viewed here.

For those not having topcoder account the problem is described below:

Penguin Pals is a match making service that matches penguins to new friends, using the following procedure:

  1. Each penguin is asked a single question: “Do you prefer the color blue, or the color red?”
  2. All penguins are arranged so that they stand on a circle, equally spaced.
  3. The organizers draw some straight lines, connecting some pairs of penguins. Each penguin may only be connected to at most one other penguin. Two penguins cannot be connected if they prefer a different color.
  4. Each penguin who is connected to some other penguin follows the line to find their match.

The only problem with the above system was that it allowed penguins to collide if two lines crossed each other. Therefore, a new additional rule was adopted: no two lines may cross. Penguin Pals now has some penguins arranged on a circle (after step 2 of the above procedure). They need to know the maximum number of pairs of penguins they can create.

You are given a String colors whose i-th character represents the prefered color of the i-th penguin (0-based index) in the circular arrangement. The i-th character is ‘R’ if the i-th penguin prefers red and ‘B’ if the i-th penguin prefers blue. Return the maximum number of matched pairs that can be formed.

Example:

“RRBRBRBB”

Returns: 3

“BBBBB”

Returns: 2

“RRRBRBRBRBRB”

Returns: 5

My Approach:

Call the string s of length n. (Note that 0th and n-1th index are consecutive).

I used a recursive function recurse(string s,int i,int j)
which is as follows:

int recurse(string s,int i,int j)
{
     if(i>=j)
           return 0;
     if(s[i]==s[j])
        return(1+recurse(s,i+1,j-1));
     else return max(recurse(s,i,j-1),recurse(s,i+1,j));
}

I start from i=0 and j=n-1, as they both will be consecutive if they are equal, call the the function with (i+1,j-1) and if not take both the possibilities and call the function recurse(s,i,j-1) and recurse(s,i+1,j) and will take the maximum of these two.

I called this function for every possible starting pair i.e.

for input “RRRBRRBB”.

I called the function recurse() with inputs:

  1. s=”RRRBRRBB” i=0 j=n-1
  2. s=”RRBRRBBR” i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
  3. s=”RBRRBBRR” i=0 j=n-1 (the same operation)

and so on until all the cases are covered.

But i got WA, and couldn’t identify the flaw in my approach why it couldn’t work.

  • 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-17T09:23:28+00:00Added an answer on June 17, 2026 at 9:23 am

    To correct you solution you should do following into each recursively call:

    s="RRRBRRBB" i=0 j=n-1
    s="RRBRRBBR" i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
    s="RBRRBBRR" i=0 j=n-1 (the same operation)
    and so on until all the cases are covered.
    

    But I feels TLE on this case.

    Solution:
    This is simple problem.

    1) Remove all pairs from string where s[i] == s[(i+1) % n], and calculate count. (i from 0 to n-1).

    2) Iterate #1 till your string not converted to “RBRBRBRB…RB” or “BRBRBRBRBR…BR”, for this special casing result (length / 2) – 1;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a problem from Algorithms book by Vazirani The input to this problem
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
I'm trying to implement this algorithm description from a previous question I had here
I'm learning for the test and I can't manage with this problem: We are
I got this problem from an interview with Microsoft. Given an array of random
This is not a jquery problem But a general algorithm problem. i have mentioned
Okay this problem will be a little hard to explain but here it goes..
I am confusing with this problem from Leetscode.com, is this algorithm a Top-down or
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
This question is about a specific programming problem I am having - I want

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.