Saturday, 7 April 2018

2 SUM Maximum or Minimun Subarray program / implementation in C++ / C

#include<bits/stdc++.h>
using namespace std;

main()
{
int a[]={1,4,5,6,7,9,9,10};
int n=sizeof(a)/sizeof(a[0]);
sort(a,a+n);
int j=n-1,i=0,num;
cout<<"Enter Number To find Sum:";
cin>>num;
for(int k=0;k<n;k++)
{
if(a[i]+a[j]>num)
j--;
else
if(a[i]+a[j]<num)
i++;
else
{
cout<<"Founded:"<<num<<"At Index:"<<"["<<i<<","<<j<<"]";
break;
}
}
}

No comments:

Post a Comment

Amazon1Ads