Skip to content

Commit

Permalink
Create Pair of Roses.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyathisside authored Jul 30, 2020
1 parent cfc6b64 commit 5e1ebc5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Coding Blocks/Pair of Roses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int nth;
cin>>nth;
while(nth--){
int sum=0;
int temp[100] = {0};
bool test=true;
int a[1000000];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int no;
cin>>no;
sort(a,a+n); // Sort this so it will take less time
for(int i=0;i<n;i++){
for( int j=1+i;j<n;j++){
if(a[i] + a[j] == no){
if(test){
sum = a[j] - a[i];
test = false;
}
int min = a[j] - a[i];
if(min <= sum){
sum = min;
}
}
}
}
for(int i=0;i<n;i++){
for( int j=1+i;j<n;j++){
if(a[j] - a[i] == sum && a[i] + a[j] == no){

cout<<"Deepak should buy roses whose prices are "<<a[i]<<" and "<<a[j]<<"."<<endl;
goto label;
}
// else if(a[j] - a[i] == sum && a[i] + a[j] == no && a[j] >= a[i]){
// cout<<"Deepak should buy roses whose prices are "<<a[j]<<" and "<<a[i]<<"."<<endl;
// }
}
}
label:;
}
}

0 comments on commit 5e1ebc5

Please sign in to comment.