Castle Towers

                  Castle Towers

Eragon is a dragon that is visiting the town of Osaka. There are n towers of various heights in the city, and tower i has height of height i. Because the taller towers tower over the shorter ones, Eragon can only blow out the tallest towers.
For example assume there are four towers of heights 3,5,4 and 5.Then Eragon can blow out 2 towers as there are two towers of maximum height 5.
Given the height height i for each individual tower, find and print the number of towers that Eragon can successfully blow out.

Input Format

The first line contains a single integer, , denoting the number of towers in the city.
The second line contains space-separated integers, where each integer i describes the height of tower i.
Constraints
 
Output Format
Print the number of towers Eragon blows out on a new line.
Sample Input 0
4
3 2 1 3
Sample Output 0
2
Explanation 0
image
As the tallest towers is of height 3 and there are two occurences, the answer is 2.

Code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Collections;

public class Solution
{
    static int castleTowers(int n, Integer[] ar) 
    {
         Arrays.sort(ar, Collections.reverseOrder());
         int maxi = ar[0];
         int cnt = 1;
         for (int i = 1; i < n; i++) 
         {
              if (maxi == ar[i]) 
              {
                  cnt += 1;
              }
              else 
              {
                  break;
              }
         }
return cnt;
}

public static void main(String[] args) 
{
     Scanner in = new Scanner(System.in);
     int n = in.nextInt();
     Integer[] ar = new Integer[n];
     for(int ar_i = 0; ar_i < n; ar_i++){
     ar[ar_i] = in.nextInt();
     }
     int result = castleTowers(n, ar);
     System.out.println(result);
     in.close();
}
}

 

 

Please do comment If u have any Queries!

Add a Comment

Your email address will not be published. Required fields are marked *