0

"Birthday Cake Candles" is the name of this problem in HackerRank This is the working code and if I replace "int" with "Integer" it is not working. It is working for all test cases except for the cases where "limit" is 100000 and all values are in lakhs or similar scenerios. Can anyone explain! please.

Scanner scanner = new Scanner(System.in);
int limit = scanner.nextInt();

int max = 0;
int count = 0;

for(int i = 0; i < limit; i++ ){
    int value = scanner.nextInt();

   if(value > max) {
       max = value;
       count = 1;
   }
   else if(value == max){
       count++;
   }
}

System.out.print(count);

scanner.close();

}

HelpMe
  • 1
  • 1
  • 5
    `if(value == max)` is likely your problem. [`Integer`s are objects, not primitives](https://stackoverflow.com/questions/10002037/comparing-integer-values-in-java-strange-behavior). But you should probably stick with `int` – ernest_k Apr 28 '22 at 13:08
  • Yep. `value.equals(max)` – Robert Harvey Apr 28 '22 at 13:09
  • I don't know if that corresponds to your actual code, but you also forgot a sc.nextLine() after every nextInt(). https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Dan Apr 28 '22 at 13:10
  • Super small nitpick: SO is an international platform and not everybody knows what a "lakh" is. – Sıddık Açıl Apr 28 '22 at 13:11
  • It helps when you write a question to include what doesn't work about it. – matt Apr 28 '22 at 13:51
  • Thanks everyone for your replies and suggestions. – HelpMe Apr 28 '22 at 17:07

0 Answers0