Ramu has a row of N dishes of different types numbered from 1 to N. He wants to pick the maximum number of dishes possible under the conditions that he can only pick one type of dish, and no two picked dishes can be adjacent. Given the types of each dish, the problem is to determine which type Ramu should pick to get the maximum number of dishes. The algorithm works by counting the occurrences of each type except consecutive ones, and returning the type with the highest count. It runs in O(N^2) time.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
123 views10 pages
Prob 1
Ramu has a row of N dishes of different types numbered from 1 to N. He wants to pick the maximum number of dishes possible under the conditions that he can only pick one type of dish, and no two picked dishes can be adjacent. Given the types of each dish, the problem is to determine which type Ramu should pick to get the maximum number of dishes. The algorithm works by counting the occurrences of each type except consecutive ones, and returning the type with the highest count. It runs in O(N^2) time.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Problem Statement
• 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/ Output • 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. • 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. CONSTRAINTS • 1 <= T <= 10^3 • 1 <= N <= 10^3 • 1 <= Ai <= 10^3 Approach • Select 1st dish and count it’s occurrences except in the consecutive indices • Check if our count > max_count, update max_count • Repeat it for next dish in the list • Complexity O(n^2) Dry Run-1 • Sample Input : 3 5 12212 6 111111 8 12223421 • Sample Output : 1 1 2 Code #Test case t=int(input()) # loop for each test case for tc in range(t): # number of elements n=int(input()) # itemList itemList=list(map(int, input().split())) i=0 max=0 itemType=itemList[0] Code (contd…) # loop to calculate max possible type of dish while i<n: #count variable c=1 j=i+1 while j<n: if itemList[i]==itemList[j] and j!=i+1: c+=1 if j<n-1 and itemList[j]==itemList[j+1]: j+=1 j+=1 Code (contd…) # if the count is greater than max then max=c if max<c: max=c itemType = itemList[i] i+=1 #print the type of Dish print(itemType)