Hii friends,this method is simple brute force i think when interviewer asks you this questions then this solution must come in your mind then u can optimise this So right now i am just posting this brute force solution later i will upload its optimize version.
Optimized Version
#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; for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if((arr[i]+arr[j])==sum){ cout << arr[i] << " " << arr[j] << endl; } } } return 0; }
No comments:
Post a Comment