Showing posts with label Cut the sticks Hacker Rank solution in C. Show all posts
Showing posts with label Cut the sticks Hacker Rank solution in C. Show all posts

Thursday, 27 August 2020

Cut the sticks Hacker Rank solution in C- perfect example where you can use goto

 

https://www.hackerrank.com/challenges/cut-the-sticks/problem

#include<stdio.h>
#include<limits.h>
int main()
{
    int n,min=INT_MAX;
    scanf("%d",&n);
    int a[n],ans=0;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    
    //finding minimum in array
    rep:
    for(int i=0;i<n;i++)
        if(a[i] > 0 && min>a[i])
            min=a[i];
    //subtracting the minimum and counting number of cuts
    int c=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>=min)
            c++;
        a[i]=a[i]-min;
    }
     if(c == 0)
        goto print;
    printf("%d\n",c);
    // again we have to find min
    min=INT_MAX;
    goto rep;

    print:
        return 0;
}