Tuesday, 18 November 2014

CUT THE STICKS



If there are N sticks, A cut operation is performed such that length of all of them are reduced by the length of the smallest stick.
Suppose we have 6 sticks of length
5 4 4 2 2 8
then in one cut operation we make a cut of length 2 from each of the 6 sticks. For next cut operation 4 sticks are left (of non-zero length), whose length are
3 2 2 6
Above step is repeated till no sticks are left.
Input Format 
The first line contains a single integer N. 
The next line contains N integers: a
0, a1,...aN-1 separated by space, where ai represents the length of ith stick.
Output Format 
 output will be the number of sticks that are cut in separate lines

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
int findmin(int * b,int n);
int check(int *a,int n);
int main() {
int n,i,min;
 scanf("%d",&n);
    int arr[n],count=0;
    for(i=0;i<n;i++)
        {
        scanf ("%d",&arr[i]);
        }
     while(check(arr,n))
    {
         min = findmin(arr,n);
         for(i=0;i<n;i++)
             { if(arr[i]>0){
              arr[i]=arr[i]-min;
                 count++;}
              }
         printf("%d\n",count);
         count=0;
        
    }
   
    return 0;
}

  int  check(int *a,int n)
    {
    int i;
    for(i=0;i<n;i++)
        {
        if(a[i] > 0)
            {return 1;
            }
        }
       return 0;
   }
   
  int findmin(int * b,int n) 
      { int i, min=1000;
      for(i=0;i<n;i++)
          {
          if((b[i]>0) && b[i] < min)
              min=b[i];
          }
       return min;
      

      }

No comments:

Post a Comment