Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please let me know about the answer! Maximize equal numbers You are given the following: An array a consisting of n elements Integer k For
Please let me know about the answer!
Maximize equal numbers You are given the following: An array a consisting of n elements Integer k For each (1in), perform the following operation exactly one time: - Replace ai by ai+x, where x[k,k] which denotes x should lie in the range of k and k, both inclusive. Task Determine the maximum length of the subsequence of array a, such that all numbers in that subsequence are equal after applying the given operation. Notes - A subsequence of array a is an array that can be derived from array a by removing some elements from it. (maybe none or all of them) - Assuming 1 based indexing. Assumptions - n=4 - k=1 - Array a=[2,5,1,2] Approach Applying one operation on indices 1,2 , and 4 , with x=0 to get array a as [2,5,1,2]. Applying one operation on index 3, with x=1 to get array a as [2. 5,2,2] Therefore, the maximum length of the subsequence with equal numbers is 3. Function description Complete the MaximizeEqualNumbers function provided in the editor. This function takes the following 3 parameters and returns the maximum length of the subsequence: - n : Represents the size of array a - k. Represents the integer k - a: Represents the array a of length n Input format Note: This is the input format that you must use to provide custom input (available above the Compile and Test button). - The first line contains an integer T denoting the number of test cases. T also denotes the number of times you have to run the MaximizeEqualNumbers function on a different set of inputs. - For each test case: - The first line contains two integers n and k. - The second line contains n space-separated integers denoting the elements of the array a. Output format For each test case, print the maximum length of the subsequence in a new line. Constraints 1T51n51050k1091ai109 Explanation The first line represents the number of test cases, T=2. For test case 1: Applying one operation with x=0 on index 1,2,3, and 4 to get array a as [2,2,5,6]. Therefore, the maximum length of the subsequence with equal numbers is For test case 2 : Applying one operation with x=0 on index 1 and 2 to get array a as [1,5, 6]. Applying one operation on index 3 , with x=1 to get array a as [1,5,5], Therefore, the maximum length of the subsequence with equal numbers is 2. Sample input 1E Sample output 1 1431335 4 Sample input 2 Sample output 2 1431531 Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started