0% found this document useful (0 votes)
225 views2 pages

Top-20 Training Program (Data Structures) Array Example: 1) First Solution (Bruteforce)

The document describes four solutions to find a duplicated integer in an array where each element is between 1 and n-1 without using additional space. The first uses a nested for loop with O(n^2) time and O(1) space. The second uses an auxiliary array with O(n) time and O(n) space. The third uses a hash set with similar time and space complexity. The fourth is an in-place solution with O(n) time and O(1) space.

Uploaded by

ramesh158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views2 pages

Top-20 Training Program (Data Structures) Array Example: 1) First Solution (Bruteforce)

The document describes four solutions to find a duplicated integer in an array where each element is between 1 and n-1 without using additional space. The first uses a nested for loop with O(n^2) time and O(1) space. The second uses an auxiliary array with O(n) time and O(n) space. The third uses a hash set with similar time and space complexity. The fourth is an in-place solution with O(n) time and O(1) space.

Uploaded by

ramesh158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Top-20 Training Program

(Data Structures)
Array Example
Given an array of n integers in which each element is between 1 and n-1,
write an efficient function to determine any duplicated integer. You may
destroy the array. What are the time and space complexities of your
solution?
Function Prototype:
int findDuplicate(int a[], int n)
1) First Solution(BruteForce)
int findDuplicate(int a[ ], int n) {
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
if(i!=j && a[i] == a[j]) return a[i];
}
return -1; //never be executed
}
2) Second Solution(Using Auxiliary Array)
int findDuplicate(int a[ ], int n) {
int i, *aux = calloc(n,sizeof(int)); //create bit array for more efficient storage
for(i=0; i<n; i++) {
if(aux[a[i]]) return a[i];
else aux[i] = 1;
}
return -1; //never be executed
}

Copyright 2012 by Algorithmica

www.algorithmica.co.in
Ph: +91-9246582537

Top-20 Training Program


(Data Structures)
Array Example
3)Third Solution(Using HashSet)
//Create your own hashset with the required API since C doesnot support any such
//libraries
int findDuplicate(int a[ ], int n) {
int i;
HashSet aux = CreatHashSet();
for(i=0; i<n; i++) {
if(find(aux, a[i])) return a[i];
else insert(aux, a[i]);
}
return -1; //never be executed
}
4) Aha! Solution(In-place)
int findDuplicate(int a[ ], int n) {
int i, tmp;
for(i=0; i<n; i++) {
int tmp = abs(a[i]);
if(a[tmp] < 0 ) return tmp;
else a[tmp] *= -1;
}
return -1; //never be executed
}

Note: Download complete working example at download link of this topic

Copyright 2012 by Algorithmica

www.algorithmica.co.in
Ph: +91-9246582537

You might also like