Skip to content

Commit 972cb8b

Browse files
Merge pull request #33 from Ln11211/birthday-cake-problem
Create birthday.java
2 parents ae01ff7 + aec5232 commit 972cb8b

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Hackerrank/birthday.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* You are in charge of the cake for a child's birthday. You have decided the cake will have one candle
2+
for each year of their total age. They will only be able to blow out the tallest of the candles.
3+
Count how many candles are tallest. A Program to input candle heights and print the total number
4+
of tallest candles present on the cake*/
5+
import java.io.*;
6+
import java.util.Arrays;
7+
class birthday
8+
{
9+
int candle_count(int [] a, int x) //a method to sort and calculate the number of candles.
10+
{
11+
Arrays.sort(a); //to sort the elements in ascending order
12+
int c=0;
13+
int lst= a[x-1];
14+
for(int i=x-1;i>=0;i--)
15+
{
16+
if(a[i]==lst)
17+
{c++;}
18+
}
19+
return c;
20+
}
21+
public static void main() throws IOException
22+
{
23+
int ln; //a variable of integer type to store the length of array(i,e number of candles)
24+
InputStreamReader inp=new InputStreamReader(System.in);
25+
BufferedReader br=new BufferedReader(inp);
26+
ln=Integer.parseInt(br.readLine());
27+
int [] cdl=new int[ln]; //an array to store the candles height
28+
for(int i=0;i<ln;i++)
29+
{
30+
cdl[i]=Integer.parseInt(br.readLine());
31+
if(cdl[i]==0) //this will ensure that the input is not 0
32+
i=i-1;
33+
}
34+
birthday obj= new birthday();
35+
System.out.println(obj.candle_count(cdl,ln));
36+
}
37+
}

0 commit comments

Comments
 (0)