QUESTION
QUESTION DESCRIPTION
Kapildev marketting a mobile phone like, if anyone answered this question, the mobile phone will be given for 50% flat offer. The task is to be find three closest elements from given three sorted arrays. So get three input aray from the user and these arrays should be in sorted mannar. Take the three sorted arrays and their sizes as input. Final answer should be the closest element from three arrays. Method name has to be used like void findClosest()
Kapildev marketting a mobile phone like, if anyone answered this question, the mobile phone will be given for 50% flat offer. The task is to be find three closest elements from given three sorted arrays. So get three input aray from the user and these arrays should be in sorted mannar. Take the three sorted arrays and their sizes as input. Final answer should be the closest element from three arrays. Method name has to be used like void findClosest()
TEST CASE 2
INPUT
INPUT
3
20 24 100
5
2 19 22 79 800
5
10 12 23 24 119
OUTPUT24 22 23
answer------
#include<bits/stdc++.h>
using namespace std;
void findClosest(int A[], int B[], int C[], int p, int q, int r)
{
int diff = INT_MAX;
int res_i =0, res_j = 0, res_k = 0;
int i=0,j=0,k=0;
while (i < p && j < q && k < r)
{
int minimum = min(A[i], min(B[j], C[k]));
int maximum = max(A[i], max(B[j], C[k]));
if (maximum-minimum < diff)
{
res_i = i, res_j = j, res_k = k;
diff = maximum - minimum;
}
if (diff == 0) break;
if (A[i] == minimum) i++;
else if (B[j] == minimum) j++;
else k++;
}
cout << A[res_i] << " " << B[res_j] << " " << C[res_k];
}
int main()
{
int A[3] ;
int B[3] ;
int C[3] ;
int i,a,b,c;
cin>>a;
for(i=0;i<a;i++)
{
cin>>A[i];
}
cin>>b;
for(i=0;i<b;i++)
{
cin>>B[i];
}
cin>>c;
for(i=0;i<c;i++)
{
cin>>C[i];
}
int p = sizeof A / sizeof A[0];
int q = sizeof B / sizeof B[0];
int r = sizeof C / sizeof C[0];
findClosest(A, B, C, p, q, r);
return 0;
}
Comments
Post a Comment