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);
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...
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example refers to Each of the data values matches its distance from the tail. The value is at the desired position. Function Description Complete the getNode function in the editor below. getNode has the following parameters: SinglyLinkedListNode pointer head: refers to the head of the list int positionFromTail: the item to retrieve Returns int: the value at the desired position Input Format The first line contains an integer , the number of test cases. Each test case has the following format: The first line contains an integer , the number of elements in the linked list. The next lines contains an integer, the data value for an el...
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. Original alphabet: abcdefghijklmnopqrstuvwxyz Alphabet rotated +3: defghijklmnopqrstuvwxyzabc Example The alphabet is rotated by , matching the mapping above. The encrypted string is . Note: The cipher only encrypts letters; symbols, such as - , remain unencrypted. Function Description Complete the caesarCipher function in the editor below. caesarCipher has the following parameter(s): string s : cleartext int k : the alphabet rotation factor Returns string: the encrypted string Input Format The first line contains the integer, , the length of the unencrypted string. The second line cont...
Comments
Post a Comment