ARRAYS-TARGET SUM PAIRS
Take as input N, the size of array. Take N more inputs and store that in an array. Take as input “target”, a number. Write a function which prints all pairs of numbers which sum to target.
#include<bits/stdc++.h>
using namespace std;
void func(int **arr, int row, int colum) {
for (int i = 0; i < colum; ) {
if (i % 2 == 0) {
for (int j = 0; j < row ; j++) {
cout << arr[j][i] << ‘,’ << " ";
}
}
else {
for (int j = 0; j < row; j++) {
cout << arr[row - j - 1][i] << ‘,’ << " ";
}
}
i++;
// for
}
}
void target_pairS(int *a, int n, int pair) {
// unordered_set ss;
unordered_map<int, int> m;
for (int i = 0; i < n; i++) {
m[a[i]]++;
}
for (int i = 0; i < n; i++) {
// cout << "inside";
if (a[i] < pair) {
if (m.find(pair - a[i]) != m.end() && m[a[i]] > 0 && m[pair - a[i]] > 0) {
if((pair-a[i])>a[i])
cout << a[i] << " and " << pair - a[i];
else
cout << pair-a[i] << " and " << a[i];
m[a[i]]--;
m[ pair - a[i]]--;
}
}
cout<<"\n";
}
}
int main()
{
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int target;
cin >> target;
target_pairS(a, n, target);
// (arr, row, colum);
return 0;
}