Given an array of bird sightings where every element represents a
bird type id, determine the id of the most frequently sighted type. If
more than 1 type has been spotted that maximum amount, return the
smallest of their ids.
Example
There are two each of types and , and one sighting of type . Pick the lower of the two types seen twice: type
.
Function Description
Complete the migratoryBirds function in the editor below.
migratoryBirds has the following parameter(s):
int arr[n]: the types of birds sighted
Returns
int: the lowest type id of the most frequently sighted birds
Input Format
The first line contains an integer,
, the size of .
The second line describes as
space-separated integers, each a type number of the bird sighted.
Constraints
It is guaranteed that each type is , , , , or
.
Sample Input 0
61 4 4 4 5 3
Sample Output 0
4
Explanation 0
The different types of birds occur in the following frequencies:
Type
:
bird
Type
:
birds
Type
:
bird
Type
:
birds
Type
:
bird
The type number that occurs at the highest frequency is type
, so we print
as our answer.
Sample Input 1
111 2 3 4 5 4 3 2 1 3 4
Sample Output 1
3
Explanation 1
The different types of birds occur in the following frequencies:
Type
: Type : Type : Type : Type : Two types have a frequency of , and the lower of those is type .
importjava.io.*;
importjava.math.*;
importjava.security.*;
importjava.text.*;
importjava.util.*;
importjava.util.concurrent.*;
importjava.util.function.*;
importjava.util.regex.*;
importjava.util.stream.*;
importstaticjava.util.stream.Collectors.joining;
importstaticjava.util.stream.Collectors.toList;
classResult{
publicstaticintmigratoryBirds(List<Integer>arr){
List<Integer> countList =newArrayList<>();
Integer[] initialCount ={0,0,0,0,0};
countList.addAll(Arrays.asList(initialCount));
int maxCount =0;
for(int i =0; i <arr.size(); i++){
int currentBirdCount = countList.get(arr.get(i)-1);
Consider a non-empty array Inarr of unique elements such as satisfies the following condition 0 Starting from the leftmost element in inarr, for each element Form all possible numbers by concatenating pair of elements in inarr such that the second element of the pair appears after the first element in inarr. From the numbers formed, find and add the unique Armstrong numbers) outarr in the order of occurrence of elements in inarr. If two pairs have the same first number, then consider the order of occurrence of second elements in the pairs An Armstrong number is equal the sum of its own digits each raised to the power of the number of digits in the number If there is no Armstrong number found, print-1 as output Input Format First line contains inarr, with the elements separated by ‘,’ (comma) Constraints Read the inputs from the standard input stream Output Format Print outarr, with the elements separated by ',' (comma) to the standard output stream Sample Input 0 15,3,1,70,53,7...
Problem Submissions Leaderboard Discussions In the previous challenge, you wrote a partition method to split an array into two sub-arrays, one containing smaller elements and one containing larger elements than a given number. This means you 'sorted' half the array with respect to the other half. Can you repeatedly use partition to sort an entire array? Guideline In Insertion Sort, you simply went through each element in order and inserted it into a sorted sub-array. In this challenge, you cannot focus on one element at a time, but instead must deal with whole sub-arrays, with a strategy known as "divide and conquer". When partition is called on an array, two parts of the array get 'sorted' with respect to each other. If partition is then called on each sub-array, the array will now be split into four parts. This process can be repeated until the sub-arrays are small. Notice that when partition is called on just one of the numbers, the...
Comments
Post a Comment