×

# Giving Wrong Answer in Chef and Rainbow Array?

 0 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 numberList = new ArrayList(); int counter = obj.nextInt();int total = counter;; while(counter>0){ int curr = obj.nextInt(); numberList.add(curr); counter--; } Iterator 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 20●5 accept rate: 0%

 0 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 answered 23 Aug '17, 21:59 92●7 accept rate: 10% alright, I was able to successfully submit my code, thanks a lot! (23 Aug '17, 22:12) your welcome! :) (23 Aug '17, 22:16)
 0 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!!! answered 23 Aug '17, 22:02 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)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,359
×1,125
×25

question asked: 23 Aug '17, 21:54

question was seen: 198 times

last updated: 23 Aug '17, 22:16