-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairSubaray_sum.cpp
85 lines (83 loc) · 1.57 KB
/
PairSubaray_sum.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*Check if there exists two elements in an
array such that their sum is equal to given k
*/
// Time Complexity: O(n)
#include <bits/stdc++.h>
using namespace std;
bool subarray_sum(int arr[], int n, int k)
{
int low = 0;
int high = n - 1;
int flag = 0;
while (low < high)
{
if (arr[low] + arr[high] == k)
{
cout << "Index " << low + 1 << " and " << high + 1 << endl;
return true;
}
else if (arr[low] + arr[high] < k)
{
low++;
}
else if (arr[low] + arr[high] > k)
{
high--;
}
}
return false;
}
int main()
{
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
// return bool fuction value(store to x)
int x = subarray_sum(arr, n, k);
if (x == 1)
{
cout << "Subarray Found" << endl;
}
else
{
cout << "Subarray not Found" << endl;
}
return 0;
}
/*
// Bruteforce Approach
//Time Complexity: O(n^2)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, k;
int flag = 0;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] + arr[j] == k)
{
cout << "Yes! Found index: " << i+1 << " and " << j+1 << endl;
flag = 1;
}
}
}
if (flag == 0)
{
cout <<"Not Found!"<< endl;
}
return 0;
}
*/