Skip to content

finding sum of elements to the given sum value of an unsorted array #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
finding sum of elements of an unsorted array
  • Loading branch information
varungoel18 authored Feb 8, 2022
commit 16550cae832079f7e9394790515d6b6f122a4e3f
27 changes: 27 additions & 0 deletions find_sum_pair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<iostream>
#include<algorithm>
using namespace std;

// in unsorted array

int main () {
int arr[] = {2,3,1,6,5,4};
int sum = 9;
int n = sizeof(arr)/sizeof(int);
int i = 0 , j=n-1;
sort(arr,arr+n);
while(i<j) {
if(arr[i] + arr[j]==sum) {
cout<<arr[i]<<" "<<arr[j]<<endl;
i++;
j--;
}
else if(arr[i]+arr[j]>sum) {
j--;
}
else if(arr[i]+ arr[j]<sum) {
i++;
}
}
return 0;
}