-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkhush.cpp
More file actions
77 lines (70 loc) · 1.79 KB
/
khush.cpp
File metadata and controls
77 lines (70 loc) · 1.79 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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int partition(int arr[],int s, int e)
{
int pivot = arr[s];
int count = 0;
for (int i = s + 1; i <= e; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = s + count;
swap(arr[pivotIndex], arr[s]);
int i = s, j = e;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i < pivotIndex && j > pivotIndex) {
int temp = arr[i];
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[],int s, int e)
{
if (s >= e)
return;
int p = partition(arr, s, e);
quickSort(arr, s, p - 1);
quickSort(arr, p + 1, e);
}
vector<int> twoSum(vector<int>& arr, int target) {
std::vector<int> ans;
int n = arr.size();
int* arrArray = arr.data();
quickSort(arrArray, 0, n - 1);
for(int i = 0; i < n; i++) {
cout<<arrArray[i]<<endl;
}
int s = 0;
int e = n - 1;
while (s <= e) {
int sum = arrArray[s] + arrArray[e];
if (sum == target) {
ans.push_back(s);
ans.push_back(e);
return ans;
} else if (sum < target) {
s++;
} else {
e--;
}
}
return ans;
}
};
int main() {
Solution s;
std::vector<int> arr = { 2, 11, 7, 15 };
int target = 9;
std::vector<int> ans = s.twoSum(arr, target);
return 0;
}