lundi 13 juillet 2020

Delete last printed element from console

QUESTION Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all pairs from both arrays whose sum is equal to X.

 **INPUT**
    1
    5 5 9
    1 2 4 5 7
    5 6 3 4 8



**EXPECTED OUTPUT** 1 8, 4 5, 5 4

**MY OUTPUT**   1 8, 4 5, 5 4,

MY CODE

#include<bits/stdc++.h>
using namespace std;
void Pair(int *a, int*b, int n, int m, int sum) {
    map<int, int>mp;
    for (int i = 0; i < n; i++) {
        int x = a[i];
        for (int i = 0; i < m; i++) {
            if ((sum - x) == b[i])
                mp[x] = b[i];
        }
    }

    for (auto x : mp) {
        cout << x.first << " " << x.second << ",";
         
    }
  
}
int main() {

    int  test ;
    cin >> test;
    for (int i = 0; i < test; i++) {
        int  a[1000000];
        int  b[1000000];
        int  n, m, sum;
        cin >> n >> m >> sum;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> b[i];
        }
        Pair(a, b, n, m, sum); 
       
        cout << endl;
    }
    return 0;
}

i HAVE ALREADY TRIED /b/b but it does not works here I don't know why please help me print the correct output and also suggest me a better way of doing it I HAVE TO REMOVE THE LAST COMMA PRINTED

Aucun commentaire:

Enregistrer un commentaire