Skip to content

Commit 211674a

Browse files
committed
new question added
1 parent bc6cb28 commit 211674a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ TODO:
9292
- [Find the subarray with the given sum](/arrays/question20.c)
9393
- [Consider and array which contains only 0's and 1's. Find largest sub array which contains only 0's and 1's](/arrays/question21.c)
9494
- [Given an array of n-integers, construct product array such that prod[i] is equal to product of all elements except arr[i] without using division operator](/arrays/question22.c)
95+
- [Find the duplicate in O(n) time and O(1) extra space](/arrays/question23.c)
96+
9597

9698
## Some important concepts to solve algos better
9799

arrays/question23.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Find the duplicate in O(n) time and O(1) extra space
3+
Only valid for numbers in range 1 to N
4+
5+
Method: In this we visit a number lets say its 3, we go to index 3 and make the number there
6+
negative. Then if 3 is repeated we will again go to 3 and check if its already negative.
7+
Time complexity: O(n)
8+
Space complexity: O(1)
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
int main(){
15+
16+
int a[] = {1,2,3,5,8,1,1,2};
17+
int size = sizeof(a)/sizeof(a[0]);
18+
19+
for(int i=0;i<size;i++){
20+
if(a[abs(a[i])] > 0){
21+
a[abs(a[i])] = -1*a[abs(a[i])];
22+
}else{
23+
printf("%d\n", abs(a[i]));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)