You are not logged in. Please login at www.codechef.com to post your questions!

×

Giving Wrong Answer in Chef and Rainbow Array?

I have been trying to solve the Rainbow array problem, I wrote a code in Java, I think it's correct but the compiler is giving wrong answer, can someone please help in finding out the error..

here is the code

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner obj = new Scanner(System.in);
        int n = obj.nextInt();
        while (n--  > 0) {
            int a=0,b=0,c=0,d=0,e=0,f=0,g=0;
            List<Integer> numberList = new ArrayList<Integer>();
            int counter =  obj.nextInt();int total = counter;;
            while(counter>0){
            int curr = obj.nextInt();
            numberList.add(curr);
            counter--;
            }
            Iterator<Integer> iter = numberList.iterator();
            while (iter.hasNext()) {
                int num = iter.next();
                if(num==1)
                    a+=1;
                else if(num==2)
                    b+=1;
                else if(num==3)
                    c+=1;
                else if(num==4)
                    d+=1;
                else if(num==5)
                    e+=1;
                else if(num==6)
                    f+=1;
                else if(num==7)
                    g+=1;
                }
            if(a!=0&&b!=0&&c!=0&&d!=0&&e!=0&&f!=0&&g!=0){
                    if(a+b+c+d+e+f+g==total){
                        System.out.println("yes");
                        continue;}

        }
            System.out.println("no");

        }
        obj.close();
    }
}

asked 23 Aug '17, 21:54

x_blitz_x's gravatar image

1★x_blitz_x
205
accept rate: 0%

edited 23 Aug '17, 22:03


alright so most of your code is correct, although code can be shortened, the main problem with your program is that after checking that numbers till 1 to 7 exists in the array, you should also check that the numbers from 1 to 6 are even in the given array, this will be the case as

if there is 1 one in the array then for it to be a rainbow array there should be a total of 2 ones

if there are 2 ones in the array then for it to be a rainbow array there should be a total of 4 one's

so after your

if(a!=0&&b!=0&&c!=0&&d!=0&&e!=0&&f!=0&&g!=0)

condition you should also check for this condition

if(a%2==0&&b%2==0&&c%2==0&&d%2==0&&e%2==0&&f%2==0)

hope this helps. Keep coding

link

answered 23 Aug '17, 21:59

shubham0812's gravatar image

1★shubham0812
927
accept rate: 10%

edited 23 Aug '17, 22:00

alright, I was able to successfully submit my code, thanks a lot!

(23 Aug '17, 22:12) x_blitz_x1★

your welcome! :)

(23 Aug '17, 22:16) shubham08121★

bro...I think your code doesn't bother about the order of numbers...where to get the correct answer you must check for the order of appearance of numbers...they must be strictly in increasing order at first and the they must decrease in the reverse order. Hope it helps!!!

link

answered 23 Aug '17, 22:02

karunakar404's gravatar image

2★karunakar404
1
accept rate: 0%

2 3 1 4 5 6 7 6 5 4 1 3 2

is this rainbow array or not??

(23 Aug '17, 22:09) x_blitz_x1★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,510
×1,180
×28

question asked: 23 Aug '17, 21:54

question was seen: 235 times

last updated: 23 Aug '17, 22:16