Thursday, July 18, 2019

counting rock samples

Counting Rock Samples

Juan Marquinho is a geologist and he needs to count rock samples in order to send it to a chemical laboratory. He has a problem: The laboratory only accepts rock samples by a range of its size in ppm (parts per million).
Juan Marquinho receives the rock samples one by one and he classifies the rock samples according to the range of the laboratory. This process is very hard because the number of rock samples may be in millions.
Juan Marquinho needs your help, your task is to develop a program to get the number of rocks in each of the ranges accepted by the laboratory.
Input Format:
An positive integer S (the number of rock samples) separated by a blank space, and a positive integer R (the number of ranges of the laboratory); A list of the sizes of S samples (in ppm), as positive integers separated by space R lines where the ith line containing two positive integers, space-separated, indicating the minimum size and maximum size respectively of the ith range.
Output Format:
R lines where the ith line containing a single non-negative integer indicating the number of the samples which lie in the ith range.
Constraints: 10 ≤ S ≤ 10000 1 ≤ R ≤ 1000000 1≤size of each sample (in ppm) ≤ 1000
Example 1
Input: 10 2
           345 604 321 433 704 470 808 718 517 811
           300 350
           400 700
Output: 2 4


Program in C:

#include <stdio.h>

int main()
{
     int m,n,a[50],b[10][1],c[10];               // m= number of rocks collected
   char ch;                                                 //n= number of labs
   scanf("%d%c%d",&m,&ch,&n);         //ch to read space
   printf("\n");                                          //a[50] = diff sizes of rocks collected
   for(int i=0;i<m;i++)                            //b[10][1]= to store min and max sizes of
      scanf("%d",&a[i]);                                           //labs in two dimentional array
    for (int j=0;j<n;j++)                           //c[10]= to sore the number of rocks that
                                                                                //are suitable to labs
scanf("%d%c%d",&b[j][0],&ch,&b[j][1]);         
      for (int k=0;k<n;k++)
          c[k]=0;
    for(int l=0;l<n;l++)
    {
        for (int x=0;x<m;x++)
        {
        if(a[x]>=b[l][0] && a[x]<=b[l][1])
            c[l]++;
        }
    }
   for (int y=0;y<n;y++)
         printf("%d ",c[y]);
    return 0;

}

1 comment:

  1. Even though the question is more than a year old the other given answer doesnt satisfy all the testcases.So below is code written by me which satisfies all testcases

    #include

    using namespace std;

    int main()
    {
    int s,r;
    cin>>s>>r;
    int arr[s]={0};
    int range[2*r]={0};
    for(int i=0;i>arr[i];
    }
    for(int i=0;i<2*r;i++)
    {
    cin>>range[i];
    }
    for(int i=0;i<2*r;i+=2)
    {
    int c=0;
    for(int j=0;j=range[i]&&arr[j]<=range[i+1])
    {
    c++;
    }

    }
    cout<<c<<" ";
    }
    }

    ReplyDelete