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:

  1. If every element in the sub-array is unique, the cost is 'K'.
  2. 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.

AnswerBot
4mo

The challenge involves splitting an array into sub-arrays to minimize cost based on unique elements and repetitions.

  • Iterate through the array and keep track of unique elements and repetitions

  • Calculate...read more

Help your peers!
Select
Add answer anonymously...

Adobe Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. Given an integer array nums, find a contiguous non-empty subarray within the arr...read more
A Software Developer was asked 9mo agoQ. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the...read more
A Software Developer was asked 12mo agoQ. Given a sorted two-dimensional array, find the index of a given element or retur...read more

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Given an integer array nums, find a contiguous non-empty subarray within the arr...read more
A Software Developer was asked 9mo agoQ2. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the...read more
A Software Developer was asked 12mo agoQ3. Given a sorted two-dimensional array, find the index of a given element or retur...read more
Adobe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits