Skip to content

Commit 3706a55

Browse files
committed
Added few more problems
1 parent 2d56000 commit 3706a55

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Given an array of integers of size n which contains numbers from 0 to n - 2. Each number is present at least once. That is, if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice. You need to find and return that duplicate number present in the array.
11+
Assume, duplicate number is always present in the array.
12+
Input format :
13+
Line 1 : Size of input array
14+
Line 2 : Array elements (separated by space)
15+
Output Format :
16+
Duplicate element
17+
18+
*/
19+
20+
21+
#include <bits/stdc++.h>
22+
23+
using namespace std;
24+
int MissingNumber(int arr[], int size){
25+
unordered_map<int, int> m1;
26+
for (int i = 0; i < size; ++i)
27+
{
28+
m1[arr[i]]++;
29+
}
30+
31+
for (int i = 0; i < size; ++i)
32+
{
33+
if (m1[arr[i]] == 2)
34+
{
35+
return arr[i];
36+
}
37+
}
38+
39+
return -1;
40+
}
41+
42+
43+
int main( int argc , char ** argv )
44+
{
45+
ios_base::sync_with_stdio(false) ;
46+
cin.tie(NULL) ;
47+
int size;
48+
cin >> size;
49+
int *input = new int[1 + size];
50+
51+
for(int i = 0; i < size; i++)
52+
cin >> input[i];
53+
54+
cout << MissingNumber(input, size);
55+
56+
delete [] input;
57+
58+
return 0;
59+
60+
61+
62+
return 0 ;
63+
64+
65+
66+
}

Language-Tools/a.out

-112 Bytes
Binary file not shown.

Language-Tools/test.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
7
2-
2 3 1 6 3 6 2
1+
9
2+
0 7 2 5 4 7 1 3 6

0 commit comments

Comments
 (0)