i know i have to print only maximum and it’s printing 3 outputs…how can i resolve the problem can someone modify my code please
Pair of roses problem
you can store arr[i] and arr[j] as
int p,q;
p=arr[i]; q=arr[j];
and then after the loop you can use p and q.
Reference Code:
#include<bits/stdc++.h>
using namespace std;
void PairOfRoses(int arr[], int n, int target){
int i = 0;
int j = n-1;
int p,q;
while(i<j){
if(arr[i] + arr[j]>target){
j--;
} else if (arr[i] + arr[j]< target){
i++;
} else {
p=arr[i];
q=arr[j];
i++;
j--;
}
}
cout<<"Deepak should buy roses whose prices are "<<p<<" and "<<q<<"."<<endl;
}
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i =0;i<n;i++){
cin>>arr[i];
}
sort(arr, arr+n);
int target;
cin>>target;
PairOfRoses(arr, n, target);
}
return 0;
}