forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2sum_by_Vishal.cpp
More file actions
71 lines (64 loc) · 1.95 KB
/
2sum_by_Vishal.cpp
File metadata and controls
71 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Code in cpp to tell if there exists a pair in array whose
// sum results in x.
#include <iostream>
using namespace std;
// Function to print pairs
void printPairs(int a[], int n, int x)
{
int i;
int rem[x];
// initializing the rem values with 0's.
for (i = 0; i < x; i++)
rem[i] = 0;
// Perform the remainder operation only if the element
// is x, as numbers greater than x can't be used to get
// a sum x. Updating the count of remainders.
for (i = 0; i < n; i++)
if (a[i] < x)
rem[a[i] % x]++;
// Traversing the remainder list from start to middle to
// find pairs
for (i = 1; i < x / 2; i++) {
if (rem[i] > 0 && rem[x - i] > 0) {
// The elements with remainders i and x-i will
// result to a sum of x. Once we get two
// elements which add up to x , we print x and
// break.
cout << "Yes\n";
break;
}
}
// Once we reach middle of remainder array, we have to
// do operations based on x.
if (i >= x / 2) {
if (x % 2 == 0) {
// if x is even and we have more than 1 elements
// with remainder x/2, then we will have two
// distinct elements which add up to x. if we
// dont have more than 1 element, print "No".
if (rem[x / 2] > 1)
cout << "Yes\n";
else
cout << "No\n";
}
else {
// When x is odd we continue the same process
// which we did in previous loop.
if (rem[x / 2] > 0 && rem[x - x / 2] > 0)
cout << "Yes\n";
else
cout << "No\n";
}
}
}
int main()
{
int A[] = { 1, 4, 45, 6, 10, 8 };
int n = 16;
int arr_size = sizeof(A) / sizeof(A[0]);
// Function calling
printPairs(A, arr_size, n);
return 0;
}
//Time Complexity== O(N+X);
//Auxiliary Space: O(X)