-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairs in array.cpp
More file actions
77 lines (65 loc) · 1.18 KB
/
Copy pathPairs in array.cpp
File metadata and controls
77 lines (65 loc) · 1.18 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
72
73
74
75
76
77
// Problem Statement Link - https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1
// Check or find all pairs on integer array whosesum is equal to given numberint
// Time: O(n^2); Space : O(1)
class solution{
public:
bool sumpair(vector<int>& nums)
{
int n = nums.size();
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n-1; j++)
{
if(nums[i] + nums[j] == k)
{
return 1;
}
}
}
return -1;
}
};
// Sorting & Two Pointer
// Time : O(nlogn)
class solution{
public:
bool sumpair(vector<int>& nums)
{
int n = nums.size();
sort(nums.begin(), nums.end());
int low=0;
int high = n-1;
while(low<high)
{
if(nums[low] + nums[high] == sum)
{
return 1;
}
if(nums[low] + nums[high] > sum)
{
high--;
}
else
{
low++;
}
}
return -1;
}
};
// Hash Table
// Time : O(n); Space: O(n)
bool checkpair(vector<int>& nums, int k)
{
unordered_map<int, int> map;
for(i=0; i<nums.size(); i++)
{
int x = k = nums[i];
if(map.find(x) =! map.end())
{
return 1;
}
map.insert(nums[i]);
}
return -1;
}