Array ( [0] => [1] => questions [2] => Basic [3] => Dishes-Problem ) HackwithInfy Previous Questions | Dishes Problem | THE INQUISITIVE





Dishes Problem

LEVEL:Intermediate

Description

Ramu has N dishes of different types arranged in a row: A1,A2,....,AN where Ai denotes the type of the ith dish. He wants to choose as many dishes as possible from the given list but while satisfying two conditions:
He can choose only one type of dish.
No two chosen dishes should be adjacent to each other.
Ramu wants to know which type of dish he should choose from, so that he can pick the maximum number of dishes.
Example :
Given N= 9 and A= [1,2,2,1,2,1,1,1,1]
For type 1, Ramu can choose at most four dishes. One of the ways to choose four dishes of type 1 is A1,A4, A7 and A9.
For type 2, Ramu can choose at most two dishes. One way is to choose A3 and A5.
So in this case, Ramu should go for type 1, in which he can pick more dishes.

Input Format

The first line contains T, the number of test cases. Then the test cases follow.
For each test case, the first line contains a single integer N. The second line contains N integers A1,A2,...,AN.
CONSTRAINTS : 1 <= T <= 10^3 1 <= N <= 10^3 1 <= Ai <= 10^3

Output Format

For each test case, print a single line containing one integer ? the type of the dish that Ramu should choose from. If there are multiple answers, print the smallest one.


Example 1:

Input
3
5
1 2 2 1 2
6
1 1 1 1 1 1
8
1 2 2 2 3 4 2 1
Output
1
1
2
Example 2:

Input
2
4
1 2 3 4 
2 
1 4
Output
1
1
Example 3:

Input
1
10
1 2 3 4 5 6 7 8 2 5
Output
2

oops

Login to see Discussion




Approach

Approach:
Step-1: Initialize an array with size 1001, and values as 0. (since, from constraints, maximum value is 1000)
Step-2: Find the frequency for all the elements. Frequency should be calculated as 1, if the adjacent elements are same.
Step-3:Initialize, max_value as 0
Step-4: Initialize for loop with condition 1001
Step-5: If frequency of i is greater than max_value, then update max_value as frequency of i and result as i
Step-6: print result


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution