Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, October 19, 2016

Cut the sticks HackerRank Solution in c


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,temp,count;
    scanf("%d",&n);
    int arr[n];
    for(int arr_i = 0; arr_i < n; arr_i++){
       scanf("%d",&arr[arr_i]);
    }
    printf("%d\n",n);
    sort:
         for(int arr_i = 0; arr_i < n; arr_i++){
       if(arr[arr_i]>temp)
           temp=arr[arr_i];
    }
    count=0;
    for(int arr_i = 0; arr_i < n; arr_i++){
       if(arr[arr_i]<temp && arr[arr_i]>0)
           temp=arr[arr_i];
    }
         // printf("\t %d\n",temp);
     for(int arr_i = 0; arr_i < n; arr_i++){
     arr[arr_i]-=temp;
         if(arr[arr_i]>0)
             count++;
     }
    if(count!=0)
      printf("%d\n",count);
    if(count>=2)
        goto sort;
     //for(int arr_i = 0; arr_i < n; arr_i++){
       //printf("%d",arr[arr_i]);
    //}
    return 0;
}

0 Comments:

Post a Comment

Stats