RAINBOWA - Editorial

Better :slight_smile:

It now fails on the following test input:

1
13
2 2 3 4 5 6 7 6 5 4 3 2 2
2 Likes

got it
Thanks

1 Like

My code: Rainbowa

I’m not able to find the test case that is getting failed. Someone please help me with this code.

Will the list contain elements valued only upto 7 or can it take up values like 8 9 also? Like is
1 2 3 4 4 5 6 7 7 8 7 7 6 5 4 4 3 2 1 valid??

1 2 3 4 4 5 6 7 7 8 7 7 6 5 4 4 3 2 1 is not valid. The array can contain elements only upto 7

1 Like

Hello everyone,
I am unable to find out why my code gives a WA. Can someone please help me.
https://www.codechef.com/viewsolution/36204637

@ssjgz

Thank you so much @ssjgz for helping me when I get stuck. It gave an AC now :blush:

1 Like

Here is a solution using the regex. We have to ensure following checks:

  1. The numbers should only be from 1-7 only.
  2. Also all the numbers(from 1-7) should come. You cannot skip any of the number.
    Eg: 1,2,3,2,1 is not a valid series.
  3. Numbers should come in this specific manner. 1-7 then 7-1.
    Eg: 1,2,3,4,5,6,7,6,5,4,3,2,1.
  4. Numbers count should be equal at start and end. Like a palindrome.
    Eg: 1,1,2,3,3,3,4,5,6,7,7,6,5,4,3,3,3,2,1,1.

First 3 cases can be checked using regex:
^[1]+[2]+[3]+[4]+[5]+[6]+[7]+[6]+[5]+[4]+[3]+[2]+[1]+$

Now we need to check if the numbers appear like a palindrome.

Below is my code to check RAINBOW array using regex:

import java.io.IOException;
import java.util.Scanner;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 
import java.util.StringTokenizer; 
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void isRainbow(int[] a, int N){
         /*convert given integer array into string and then match with the regex;*/

		String s1 = Arrays.toString(a).replaceAll("\\[","").replaceAll("\\]","").replaceAll(",","").replaceAll(" ","");
		String regex = "^[1]+[2]+[3]+[4]+[5]+[6]+[7]+[6]+[5]+[4]+[3]+[2]+[1]+$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(s1);
		if(!matcher.matches()) {
			System.out.println("no");
			return;
		}
        /* Now check for the palindromic nature of the series */
	    int l = 0;
	    int r = N - 1;
	    while(l <= r){
	        if(a[l] != a[r]){
	            System.out.println("no");
	            return;
	        }
	        l++; 
	        r--;
	    }
        /* If the series passes all the checks then it is a rainbow array */

	    System.out.println("yes");
	    return;
	}
		public static void main (String[] args) throws java.lang.Exception{
		    Scanner sc = new Scanner(System.in);
		    int t = 0;
		    if(sc.hasNextInt())
		        t = sc.nextInt();
		    while(t-- > 0){
		        int N = sc.nextInt();
		        int[] a = new int[N];
		        for(int i = 0; i < N; i++){
		            if(sc.hasNextInt())
		                a[i] = sc.nextInt();
		        }
		        isRainbow(a, N);
		        
		    }
		}
	
}

Thanks!! :star_struck:

Thank you… i was really very confused.

can anyone please figure out what is the wrong with this code sample input is showing positive result

t=int(input())
for i in range(t):
    flag=True
    array=[]
    n=int(input())
    array=list(map(int,input().split()))
    m=0;
    g=n-1-m
    c=array[m]
    while m<=g and array[0]==1 and array[-1]==1:
        if array[m]==array[g] and (array[m]==array[m-1] or array[m]==array[m-1]+1):
            if m==g and array[m]==7:
                break
            m=m+1
            g=n-1-m
        else:
            flag=False
            break;
    if flag :
        if array[m]==7:
            print('yes')
    else:
        print('no') 
2 Likes

Yes the o/p is wrong it should come ‘no’ but it is coming ‘yes’.i got it why my algo is wrong Thank u very much @ssjgz

1 Like

Gives no output for this
1
11
2 3 4 5 6 7 6 5 4 3 2

2 Likes

Yes because the first and last elements must be 1

@snehalmahima means that it doesn’t give any output at all for that test input :slight_smile:

3 Likes

Yeah! It doesnt even print “no”. That’s what I meant :slightly_smiling_face:

3 Likes

@snehalmahima thank u it should print no

1 Like

Rememer to use ‘yes’ or ‘no’ instead of ‘Yes’ or ‘No’

What’s wrong in this solution https://www.codechef.com/viewsolution/41956567. It works fine in my code blocks IDE, but here it shows WA.