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.
AnswerBot
4mo

Find the minimum number of increments needed to make all elements of the array distinct by increasing any element by 1 in each operation.

  • Iterate through the array and keep track of the frequency of ea...read more

Help your peers!
Select
Add answer anonymously...

Walmart Software Developer interview questions & answers

A Software Developer was asked Q. How many languages do you know?
A Software Developer was asked Q. What are your strengths?
A Software Developer was asked Q. Can you explain Source Code Control (SCC)?

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. Tell me about yourself.
A Software Developer was asked Q2. How many languages do you know?
A Software Developer was asked Q3. What are your strengths?
Walmart 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