-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthdayCakeCandles_16.java
70 lines (60 loc) · 2.02 KB
/
BirthdayCakeCandles_16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package MountBlue_01;
import java.util.Scanner;
public class BirthdayCakeCandles_16 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = input.nextInt();
}
int ans = birthdayCakeCandles(arr);
System.out.println(ans);
}
static int birthdayCakeCandles(int[] arr){
// // get the count
// int[] freq = new int[10];
// for (int i = 0; i < n; i++) {
// freq[arr[i]]++;
// }
//
// int maxIndex = 1;
// for (int i = 0; i < freq.length; i++) {
// if(freq[i] > freq[maxIndex]){
// maxIndex = freq[i];
// }
// }
//
// return maxIndex;
/* Find the max element */
int max = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
/* Get the count of max element */
int count = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == max){
count++;
}
}
return count;
}
}
// SOLUTION 2 : EXCEEDING TIME LIMIT
/*
1. initialize a freq array of size 10 and store and get count of each year
2. Here the count of freq array is 10 because the tallest candle can be from 0 to 9
3. take a maxIndex to store the count of maximum height of candles which occurs at the highest frequnecy
3. take a loop iterate over freq array and check if current element has the highest value than the previous if true update the maxIndex
*/
// SOLUTION 2 :
/*
1.loop through an array and find max value from given candles
2. take count variable to count frequency of max value element
3. take another for loop and and compare the max value to every element in an array\
4. if its true, then increment the value of count by 1
5. return the count
*/