#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 Two pointer concept //first of all sort the array sort(arr,arr+n); i=0;//1st element j=n-1;//last element while(i<j){ int x=arr[i]+arr[j]; if(x==sum){ cout << arr[i] << " " << arr[j] << endl; i++; j--; } else if(x>sum){ j--; } else{ i++; } } return 0; }
Translate
Saturday, 6 February 2016
FIND THE PAIR IN AN ARRAY FOR A GIVEN SUM(USING TWO POINTER CONCEPT) VERY GOOD METHOD
Hi,guys here is the best solution for this problem. Time Complexity:o(nlogn) Space Complexity:o(1) If u have any doubt plz comment below.
Subscribe to:
Post Comments (Atom)
-
HII guys this is totally geometry based problem there is nothing to code just use formula LOGIC::how to find centroid of a polygon u c...
No comments:
Post a Comment