Make All Elements of the Array Distinct

Given an array/list ARR of integers with size 'N', your task is to determine the minimum number of increments needed to make all elements of the array distinct. You are allowed to increase any element by 1 in each operation.

Example:

Input:
[1,4,4,5]
Output:
2
Explanation:

The third and fourth elements can be increased by 1 each, turning the array into [1,4,5,6], where all numbers are distinct. This requires 2 operations.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^3
  • 1 <= ARR[i] <= 10^4
  • Time Limit: 1 sec

Input:

The first line contains the integer ‘T’, representing the number of test cases. 
The first line of each test case contains the integer ‘N’.
The following line contains ‘N’ integers as the elements of the array/list ‘ARR’.

Output:

Return the minimum number of operations for each test case to make the elements of the array distinct.
Note:
No need to print anything; just implement the function to return the answer.
Be the first one to answer
Add answer anonymously...
Walmart Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter