-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpattern.cpp
More file actions
57 lines (57 loc) Β· 799 Bytes
/
Copy pathpattern.cpp
File metadata and controls
57 lines (57 loc) Β· 799 Bytes
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
#include<iostream>
using namespace std;
void twosum(int arr[],int target)
{
for(int i=0;i<6;i++)
{
for(int j=1;j<6;j++)
{
if(arr[i]+arr[j]==target){
cout<<j<<" "<<i<<endl;
}
}
}
}
void sort(int arr[])
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void optimal(int arr[],int target)
{
sort(arr);
int left=0,right=6-1;
while(left<right)
{
if(arr[left]+arr[right]==target)
{
cout<<left<<" "<<right<<endl;
break;
}
else if(arr[left]+arr[right]>target)
{
--right;
}
else
{
++left;
}
}
}
int main()
{
int target=21;
int arr[6]={2,4,5,3,20,1};
twosum(arr,target);
optimal(arr,target);
}