Morty's Array Challenge
Rick has provided Morty with an array 'Arr' of length 'N' and an integer 'K'. Morty needs to split the array into non-empty sub-arrays to achieve the minimum possible cost, subject to the following rules:
- If every element in the sub-array is unique, the cost is 'K'.
- If some elements are not unique, the cost is 'K' + the number of repeated elements.
Note: A sub-array 'C' is derived from the array 'D' by potentially removing elements from its beginning or end.
Example:
Input:
Arr[] = {1, 1, 1, 4, 1, 2, 4}, K=2
Output:
7
Explanation:
The array can be split into sub-arrays {1, 1, 1, 4}, {1, 2, 4}. The cost for the first sub-array is 2 + 3 (three 1's), and for the second, it is 2, leading to a total cost of 7.
Input:
The first line contains an integer 'T', the number of test cases. For each test case:
First line: two integers 'N' and 'K', the array length and cost.
Second line: 'Arr' consisting of 'N' integers.
Output:
A single integer 'ANS' representing the minimum cost for each test case, printed on a new line.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 500
- 1 <= K <= 500
- 1 <= Arr[i] <= 500
- Time limit: 1 sec
Note:
You just need to implement the function; printing is managed elsewhere.
Be the first one to answer
Add answer anonymously...
Top Adobe Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app