Hii friends,here find the optimize version of the above problem.If u have any doubt let me know
#include<bits/stdc++.h> using namespace std; int main(){ int n,i,j; cout << "Enter the size of array:\n"; cin >> n; int arr[n]; cout << "Enter the elements of array:\n"; for(i=0;i<n;i++){ cin >> arr[i]; } int sum; cout << "Enter the value of sum:\n"; cin>> sum; //Using hash technique map<int,int> hash; for(i=0;i<n;i++){ hash[arr[i]]=1; } for(i=0;i<n;i++){ if(hash.find(sum-arr[i])!=hash.end()){ cout << arr[i] << " " << sum-arr[i] << endl; hash.erase(arr[i]);//to remove duplicates pair } } return 0; }
No comments:
Post a Comment