https://hack.codingblocks.com/contests/c/522/696
https://ide.codingblocks.com/s/41950
Run error in pair of roses
you can use unordered map which will run in complexity O(TN). NOW your code is running in O(TN*logN).
Your are getting run error as ( long long int a[i] ) . Here size should be a[n]
Wrong test case
1
3
20 40 60
80
Correct Output
20 60
your output
40 40
Reason
you are doing binary search on complete array but you have to do binary search from (i+1 to n)
Hit like if u get it.
1 Like
your Binary search function is wrong cozz if I m using inbuilt function binary_search() with your code the it is showing passed.
int main() {
long long int t,i,n,sum,s1,s2,diff,min,a1,a2;
cin>>t;
while(t–){
cin>>n;
long int a[n];
for(i=0;i<n;i++)
cin>>a[i];
cin>>sum;
sort(a,a+n);
min=INT_MAX;;
for(i=0;i<n;i++){
if(a[i]<=sum-a[i]){
s1=a[i];
s2=sum-a[i];
if((binary_search(a+i+1,a+n,s2)==true)){
diff=abs(s1-s2);
if(diff<=min){
min=diff;
a1=s1;
a2=s2;
}
}}
}
printf("Deepak should buy roses whose prices are %d and %d.\n",a1,a2);
}
return 0;
}