Thursday 11 February 2016

Closest Numbers

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.


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;
}

32 comments:

  1. after sorting the diff array, can you explain what to do clearly ?

    ReplyDelete
    Replies
    1. After 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..
      If 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

      Delete
    2. thank you nikhil. Very good.You got the logic

      Delete
  2. what it the meaning of closest number? and "Num" and "N" what they are?
    please explain.

    ReplyDelete
    Replies
    1. it is simply name of the problem.
      "num" and "N"are the name of variables.acoording to this the problem is formed

      Delete
  3. solution is

    int 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]);
    }
    }

    ReplyDelete
    Replies
    1. can u please refine the first for loop and there is 'if'statement missing....thanks

      Delete
  4. for (i=0;inum)


    what is that in line number 4

    ReplyDelete
    Replies
    1. for (i=0;inum)


      what is that in line number 4

      Delete
  5. int 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]);
    }
    }



    GOT IT!!!!!!!!!!

    ReplyDelete
  6. its still the same... first for loop is not proper :(

    ReplyDelete
  7. please after getting differnce and sorting of differences what we have to do

    ReplyDelete
    Replies
    1. look while sorting the difference array also sort the main array so that you will get the desired array elements

      Delete
    2. Dear Sir,

      Kindly send me whole program to mirtunjay4u@gmail.com.
      I am unable to solve this problem.Kindly help me if possible at your end.

      Delete
  8. sir please give full solution to understand better

    ReplyDelete
  9. i 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?
    e.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?

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. i don't get it.

      Delete
  10. that is the whole solution
    #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;
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  11. Please give the solution for this...

    ReplyDelete
  12. These are all the errors in the closest program. Can anyone help me with this?


    Program: 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

    ReplyDelete
    Replies
    1. why are you running the code given by one of the user .try coding yourself

      Delete
  13. yes please give the solution for this program

    ReplyDelete
  14. cant get the solution.please give the solution

    ReplyDelete
  15. Please correct the first loop condition and mention what is in if() statement there

    ReplyDelete
  16. I am getting this error,help me to solve this
    Test 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

    ReplyDelete
    Replies
    1. you've to print last output without space.
      so write this:-
      for(i=0;i<5;i++)
      {
      if(i==4)
      printf("%d",arr[i]);
      else
      printf("%d ",arr[i]);
      }

      Delete