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

  • Home
  • SEARCH
  • 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 8022211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:12:10+00:00 2026-06-04T22:12:10+00:00

This is the question. I came up with an algorithm but I kept on

  • 0

This is the question. I came up with an algorithm but I kept on getting a Wrong Answer status. I need to know what’s wrong with my approach.

Here’s my algorithm:

   Traverse the char array s: for i in range(0,len(s))
1. If c = '?' start from p=0 and check if p is present in left or right. If yes then increment it and repeat this step until p != left and p != right.
2. Check if p >= k. If yes then print NO and continue to the next test case.
3. Put the value of p in s[i] and continue
4. If c != '?', then check if c = its left and right digits. If it is, then print NO and continue to the next test case.

There’s a special case I’ve had to handle, when k=2 and s[0] = ‘?’ (do a dry run for the input k = 2, s = ???0 on my algo, the output will turn out to be NO, whereas it should be 1010, so it’s easy to figure out why that’s a special case). For k=2, the digits will alternate. Hence, if the first character is 1, the whole string can be determined. If s[0] is ‘?’, then in the answer s[0] maybe 0 or 1. That’s the special case I’ve considered.

Here’s a little bit of theory on why my program (according to me) would always work correctly.

I’ve handled the cases when k = {1,2} correctly, and for all k >= 3, answer can never be NO, provided the input test case is not already incorrect (having at least 1 pair of same adjacent digits). This is because, any digit (in the circle) will have exactly 2 neighbours, and I will have at least 3 colors to put, hence all cases k>=3 are also handled. Now, according to me my logic isn’t wrong in any way, but when I submit, I’m getting a Wrong Answer.

Just for more details, here’s the C code:

#include <stdio.h>
#include <string.h>
int main()
{
    int t; scanf("%d\n",&t);
    while(t--)
    {
        int a=0,k,len; scanf("%d\n",&k);
        char s[101]; scanf("%s\n",&s);
        len = strlen(s);
        if(k==2 && s[0] == '?') // the special test case I was talking about
        {
            while(s[++a] == '?');
            if(a < len && ((a%2 == 0 && s[a] == 49) || (a%2 == 1 && s[a] == 48))) s[0] = 49;
        }
        for(a=0;a<len;a++)
        {
            int l = a==0 ? len-1 : a-1, r = a==len-1 ? 0 : a+1, p=0;
            if(s[a] == '?')
            {
                while(s[l]-48 == p || s[r]-48 == p) p++;
                if(p >= k) goto NP;
                s[a] = p+48;
            }
            else // checking the validity of input string
            {
                if(s[a] == s[l] || s[a] == s[r] || s[a] >= k+48) goto NP;
            }
        }
        printf("%s\n",s); continue;
        NP:
        printf("NO\n");
    }
}
  • 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-04T22:12:12+00:00Added an answer on June 4, 2026 at 10:12 pm

    From the problem statement:

    In the case N = 1 any arrangement is valid as long as the color used for the only cherry of this arrangement is less than K.

    Run your code with the following input:

    1
    5
    2
    

    That is T=1, K=5, and one piece of cake with one cherry color “2”

    What is it supposed to do instead?

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

Sidebar

Related Questions

I came across this question on an interview questions thread. Here is the question:
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
I came up with this question. There is an encryption algorithm which uses bitwise
Here is a link to a similar question with a good answer: Java Algorithm
In trying to answer this question I came up with the following code: #include
I don't know much about color composition, so I came up with this algorithm
I came across this question; "A lossless compression algorithm claims to guarantee to make
I came across this problem,I have got an algorithm that I need to implement
This question came to my mind when I learned C++ with a background of
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given

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.