Find a duplicated number in a loose permutation of numbers. A permutation is an array that is size n
, and also has positive numbers from 1 to n
. A loose permutation is a permutation where some numbers are missing and some are duplicated, but the total number is still n
.
{
"arr": [1, 2, 7, 3, 4, 4, 5]
}
Output:
4
We can see that 4 is a duplicate number here as it is present 2 times in the array.
{
"arr": [1, 2, 3]
}
Output:
-1
Constraints:
n
<= 106n
We provided one solution.
Throughout this editorial, we will refer to the length of the input array as n
.
abs(integer) -1
, here abs(5) - 1 = 4
, in the input array.abs(integer)
because over the subsequent iterations, we may start getting negative numbers at certain array positions, hence we use the absolute values of array integers.abs(-3) - 1
, which is value at index 2, and that is already negated. So we identify 3 as a duplicate.O(n).
We iterate all the elements of the array only once.
O(1).
O(n).
Space used for input: O(n).
Auxiliary space used: O(1).
Space used for output: O(1).
So, total space complexity: O(n).
/*
Asymptotic complexity in terms of the length of the input array `n`:
* Time: O(n).
* Auxiliary space: O(1).
* Total space: O(n).
*/
int find_duplicate(vector<int> &arr) {
for (int i = 0; i < arr.size(); i++) {
int value = abs(arr[i]) - 1;
if (arr[value] < 0) {
return abs(arr[i]);
}
else {
arr[value] = -arr[value];
}
}
// If no duplicate is present, we return -1.
return -1;
}
We hope that these solutions to finding duplicate in a loose permutation problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.
If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.
Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.
We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:
â€To learn more, register for the FREE webinar.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: