forked from manishaparuthi/manisha_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargetsum.cpp
More file actions
40 lines (40 loc) · 820 Bytes
/
targetsum.cpp
File metadata and controls
40 lines (40 loc) · 820 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
/* Given an array of integers, return indices of the two numbers such that
they add up to a specific target.You may assume that each input would have
exactly one solution, and you may not use the same element twice.
*/
using namespace std;
#include<iostream>
int main()
{
int size;
cout<<"Enter the size of the Array: ";
cin>>size;
int target;
cout<<endl;
cout<<"Enter the Target value: ";
cin>>target;
cout<<endl;
int array[size];
int result[2];
cout<<"Enter Array: ";
for(int i=0;i<size;i++)
{
cin>>array[i];
}
for (int i=0;i<size;i++)
{
for(int j=i;j<size;j++)
{
if(array[i]+array[j]==target)
{
result[0]=i;
result[1]=j;
break;
}
}
}
cout<<endl;
cout<<"The Resultant values are: ";
cout<<result[0]<<" "<<result[1];
return 0;
}