File tree
Expand file treeCollapse file tree1 file changed
+41
-0
lines changed Array/Geeksforgeeks problem
Expand file treeCollapse file tree1 file changed
+41
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +class solution |
| 2 | +{ |
| 3 | +public: |
| 4 | +int *findTwoElement(int *arr, int n) { |
| 5 | +int *res= new int(2); |
| 6 | +int freq[n+1]={0}; |
| 7 | +for (int i=0;i<n;i++) |
| 8 | +{ |
| 9 | +freq[arr[i]]++; // go to that particular index and increment it by 1 |
| 10 | +} |
| 11 | +for (int i=0;i<n;i++) |
| 12 | +{ |
| 13 | +if (freq[arr[i]]>1) // if the index is greater then 1 that means it is a duplicate element |
| 14 | +res[0]=arr[i]; |
| 15 | +if (freq[i]==0) // the index which will be having 0 will be the missing number |
| 16 | +res[1]=i; |
| 17 | +} |
| 18 | +// this is edge case when result's 1st index will be zero i.e last number is missing |
| 19 | +/* For example |
| 20 | +Possibly your code doesn't work correctly for multiple test-cases (TCs). |
| 21 | +
|
| 22 | +The first test case where your code failed: |
| 23 | +
|
| 24 | +Input: |
| 25 | +14 |
| 26 | +12 7 5 1 13 1 10 8 11 9 2 4 3 6 |
| 27 | +
|
| 28 | +Its Correct output is: |
| 29 | +1 14 |
| 30 | +
|
| 31 | +And Your Code's output is: |
| 32 | +1 0 |
| 33 | +*/ |
| 34 | +if (res[1]==0) |
| 35 | +res[1]=n; |
| 36 | +return res; |
| 37 | +} |
| 38 | +}; |
| 39 | + |
| 40 | +// O(2n)tc |
| 41 | +// o(n)sc |
You can’t perform that action at this time.
0 commit comments