#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin>>n;
ll ar[n];
for(ll i=0;i<n;i++)
{
cin>>ar[i];
}
ll target;
cin>>target;
map<int,int> mp;
for(ll j=0;j<n;j++)
{
ll rem=target-ar[j];
if(mp.find(rem)!=mp.end())
{
cout<<ar[j]<<" and "<<rem<<endl;
}
mp[ar[j]++];
}
}
How to print in sequence, This code is working to find the pair but how to print in sequence
Instead of using the map try this.
Sort the given array, and then run two loops to print the sequence.
O(N^2) works because its N<=1000
I hope Iβve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n]={0}; for(int i=0;i<n;i++) { cin>>a[i]; cin.get(); } int k=0; cin>>k; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if((a[i]+a[j])==k) { cout<<a[i]<<" β<<βandβ<<β "<<a[j]<<endl; } } } return 0; } this doesnt work
you need to sort the array as well, it was mentioned in the approach.