You are given a sorted list of N numbers and a number Num. Write a program to find the five numbers that are closest (numerically) to Num. A number x in the array is closest to Num if |Num-x| is the smallest among all such x possible.
Note: If the Num is present in given list, then it should be in the output.
Constraints:
5 < N <20
All numbers in list are between -10000 to 10000
-10000 <Num< 10000
There could be numbers that are repeated
Input:
N Num
m1 m2 m3 .. mN
where mi's are N numbers in sorted order.
Output:
p1 p2 p3 p4 p5
where pi's are closest numbers in increasing order of absolute difference from Num. If there are two numbers with same difference, print the larger one first.
Note: If the Num is present in given list, then it should be in the output.
Constraints:
5 < N <20
All numbers in list are between -10000 to 10000
-10000 <Num< 10000
There could be numbers that are repeated
Input:
N Num
m1 m2 m3 .. mN
where mi's are N numbers in sorted order.
Output:
p1 p2 p3 p4 p5
where pi's are closest numbers in increasing order of absolute difference from Num. If there are two numbers with same difference, print the larger one first.
Solution-
#include<stdio.h>
#define MAXSIZE 20
/*
Write a function for printing 5 closest numbers
*/
void printclosest(int arr[], int num, int n){
int b[n],i,j,c[n],temp;
for(i=0;i<n;i++)
{b[i]=i;
if(num>=arr[i])
c[i]=num-arr[i];
else
c[i]=arr[i]-num;
}
for(i=0;i<5;i++)
{
for(j=i;j<n;j++)
{if(c[i]>=c[j])
{
temp=c[i];c[i]=c[j];
c[j]=temp;temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}}
for(i=0;i<4;i++)
{
printf("%d ",arr[b[i]]);
}
printf("%d",arr[b[4]]);
}
int main()
{
int arr[MAXSIZE];
int i,n,num;
scanf("%d %d",&n,&num);
for (i = 0; i < n; ++i)
{
scanf("%d",&arr[i]);
}
printclosest(arr, num, n);
return 0;
}
#define MAXSIZE 20
/*
Write a function for printing 5 closest numbers
*/
void printclosest(int arr[], int num, int n){
int b[n],i,j,c[n],temp;
for(i=0;i<n;i++)
{b[i]=i;
if(num>=arr[i])
c[i]=num-arr[i];
else
c[i]=arr[i]-num;
}
for(i=0;i<5;i++)
{
for(j=i;j<n;j++)
{if(c[i]>=c[j])
{
temp=c[i];c[i]=c[j];
c[j]=temp;temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}}
for(i=0;i<4;i++)
{
printf("%d ",arr[b[i]]);
}
printf("%d",arr[b[4]]);
}
int main()
{
int arr[MAXSIZE];
int i,n,num;
scanf("%d %d",&n,&num);
for (i = 0; i < n; ++i)
{
scanf("%d",&arr[i]);
}
printclosest(arr, num, n);
return 0;
}
after sorting the diff array, can you explain what to do clearly ?
ReplyDeleteAfter finding the N closest number to given number "Num",you have to sort the numbers in increasing order of absolute difference with respect to that Num..
DeleteIf Num==6 and N=5 so,5 closest number are 6,11,9,7,2 the sort them 6,7,9,2,11 as (6-6)=0,(6-7)=1,(6-9)=3,(6-2)=4,(6-11)=5 where (x-y) is the absolute difference btwn x and y
thank you nikhil. Very good.You got the logic
Deletewhat it the meaning of closest number? and "Num" and "N" what they are?
ReplyDeleteplease explain.
it is simply name of the problem.
Delete"num" and "N"are the name of variables.acoording to this the problem is formed
solution is
ReplyDeleteint b[MAXSIZE],j,temp,m,i;
/*
Write a function for printing 5 closest numbers
*/
void printclosest(int arr[], int num, int n){
for (i=0;inum)
b[i]=arr[i]-num;
else
b[i]=num-arr[i];
}
for (i=n-1;i>=0;i--){
for(j=n-1;j>=i+1;j--){
if(b[i]>=b[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
if(i==4)
printf("%d",arr[i]);
else
printf("%d ",arr[i]);
}
}
can u please refine the first for loop and there is 'if'statement missing....thanks
Deletefor (i=0;inum)
ReplyDeletewhat is that in line number 4
for (i=0;inum)
Deletewhat is that in line number 4
which if statement
Deleteint b[MAXSIZE],j,temp,m,i;
ReplyDelete/*
Write a function for printing 5 closest numbers
*/
void printclosest(int arr[], int num, int n){
for (i=0;inum)
b[i]=arr[i]-num;
else
b[i]=num-arr[i];
}
for (i=n-1;i>=0;i--){
for(j=n-1;j>=i+1;j--){
if(b[i]>=b[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
if(i==4)
printf("%d",arr[i]);
else
printf("%d ",arr[i]);
}
}
GOT IT!!!!!!!!!!
its still the same... first for loop is not proper :(
ReplyDeleteplease after getting differnce and sorting of differences what we have to do
ReplyDeletelook while sorting the difference array also sort the main array so that you will get the desired array elements
DeleteDear Sir,
DeleteKindly send me whole program to mirtunjay4u@gmail.com.
I am unable to solve this problem.Kindly help me if possible at your end.
sir please give full solution to understand better
ReplyDeletei did sort the array of differences along with the given array,but the point is if there are numbers with same difference, how to preserve their order?
ReplyDeletee.g. if num is 6 and in array we've numbers 5 & 7,so 7 must come first and then 5.
Can you just give a hint Killer?
no ,when you sort it ,it arranges in a manner by itself that the order is preserved. first make the program and run it you can check yourself.
Deletei don't get it.
Deletethat is the whole solution
ReplyDelete#include
#define MAXSIZE 20
/*
Write a function for printing 5 closest numbers
*/
void printclosest(int arr[], int num, int n){
int b[20],j,temp,m,i;
for(i=0;inum)
b[i]=arr[i]-num;
else
b[i]=num-arr[i];
}
for (i=n-1;i>=0;i--){
for(j=n-1;j>=i+1;j--){
if(b[i]>=b[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}}
}
for(i=0;i<5;i++)
{
if(i==4)
printf("%d",arr[i]);
else
printf("%d ",arr[i]);
}
}int main()
{
int arr[MAXSIZE];
int i,n,num;
scanf("%d %d",&n,&num);
for (i = 0; i < n; ++i)
{
scanf("%d",&arr[i]);
}
printclosest(arr, num, n);
return 0;
}
This comment has been removed by the author.
DeleteThis comment has been removed by the author.
DeletePlease give the solution for this...
ReplyDeleteThese are all the errors in the closest program. Can anyone help me with this?
ReplyDeleteProgram: In function 'printclosest':
Program:12:9: error: 'inum' undeclared (first use in this function)
Program:12:9: note: each undeclared identifier is reported only once for each function it appears in
Program:12:13: error: expected ';' before ')' token
Program:14:1: error: 'else' without a previous 'if'
Program: At top level:
Program:18:1: error: expected identifier or '(' before 'for'
Program:18:13: error: expected '=', ',', ';', 'asm' or '__attribute__' before '>=' token
Program:18:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before '--' token
Program:29:1: error: expected identifier or '(' before 'for'
Program:29:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
Program:29:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '++' token
Program:36:1: error: expected identifier or '(' before '}' token
Program:48:5: error: redefinition of 'main'
Program:36:6: note: previous definition of 'main' was here
why are you running the code given by one of the user .try coding yourself
Deleteyes please give the solution for this program
ReplyDeleteplease try yourself first
Deletecant get the solution.please give the solution
ReplyDeletePlease correct the first loop condition and mention what is in if() statement there
ReplyDeleteI am getting this error,help me to solve this
ReplyDeleteTest Case 1 Presentation Error
Input Expected Output Actual Output
10 6
1 2 3 4 5 6 7 8 9 10 6 7 5 8 4 6 7 5 8 4
you've to print last output without space.
Deleteso write this:-
for(i=0;i<5;i++)
{
if(i==4)
printf("%d",arr[i]);
else
printf("%d ",arr[i]);
}
good . thank you.
Delete