Tuesday, 21 October 2014

Hackerrank Cut The Sticks Solution

The Solution for hackerrank poblem, Cut The Sticks Solution using C++ program.
Passed Testcases : 9 out of 9
Source Code:
 #include <algorithm>  
 #include <iostream>  
 #include <climits>  
 using namespace std;  
 int main()  
 {  
   int n,i,j,k,min= INT_MAX,max,st,en,arr[10000],flag ;  
   cin>>n;  
   for(i=0;i<n;i++){  
     cin>>arr[i];  
     if( min>arr[i])  
       {  
       min = arr[i];  
     }  
   }  
   //cout<<min<<"min";  
   do{  
     int count= 0,nmin=INT_MAX;  
     flag =0;  
     for(i=0;i<n;i++)  
       {  
       if(arr[i]>0){  
       arr[i] = arr[i] - min;  
         count++;  
       }  
       //cout<< arr[i]<<" ";  
       if(arr[i]>0 )  
         {  
         flag =1;  
         if(nmin>arr[i])  
           nmin = arr[i];  
       }  
     }  
     min = nmin;  
     cout<<count<<endl;  
   }while(flag>0);  
   return 0;  
 }  

0 comments:

Post a comment