xxxxxxxxxx
int two_sum(){
int target = 19;
int n = 10;
int arr[n] = {1,2,3,4,5,6,7,8,9,10};
int l = 0;
int r = n-1;
while (l<r) {
// If we find a pair
if(arr[l] + arr[r] == target)
return 1;
else if (arr[l] + arr[r] < target)
l++;
else
r--;
}
return -1;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout<<"Enter the first number.\n";
cin>>num1;
cout<<"Enter the second number.\n";
cin>>num2;
sum = num1 + num2;
cout<<"The sum is \n"<<sum;
return 0;
}
xxxxxxxxxx
bool twosum(int A[], int N, int X) {
sort(A, A+N);
int i = 0, j = N-1;
while (i < j) {
if (A[i] + A[j] == X) return true;
else if (A[i] + A[j] > X) j--;
else i++;
}
return false;
}
xxxxxxxxxx
#include <iostream>
int main(){
float sum=0;
int array [3][5] =
{
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};
for ( i = 0; i < 3; i++ ){
for ( j = 0; j < 5; j++ ){
sum+=array[i][j];
}
}
std::cout << sum << std::endl;
}