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.
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.
Attend our free webinar to amp up your career and get the salary you deserve.