Monday, 27 October 2014

Wednesday, 22 October 2014

Hackerrank Filling Jars Solution


The Solution for hackerrank poblem, Filling Jars Solution using C++ program.
Passed Testcases : 13 out of 13

Source Code:
 #include<iostream>  
 #include<vector>  
 #include<math.h>  
 #include<array>  
 using namespace std;  
 int main()  
 {  
    long n,m; 
      long answer=0,t,i;  
    long a,b,k,ii,jj,kk;  
   cin>>n>>m;  
   vector<long> array(n);  
   t = m;  
   while(t--){  
     cin>>a>>b>>k;  
     answer = answer + (abs(a-b)+1)*k;  
   }  
   answer = floor(answer/n);  
   cout<<answer<<endl;  
   return 0;  
 }  

Hackerrank Angry Childrem Solution


The Solution for hackerrank poblem, Angry Childrem Solution using C++ program.
Passed Testcases : 15 out of 15
Source Code:

 #include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 #include <stdlib.h>  
 #include <algorithm>  
 using namespace std;  
 #define MAX 100000  
 #define MAX_VAL 1000000001  
 int candies[MAX];  
 int main() {  
   int N,K;  
   int i,upper,lower[MAX];  
   scanf("%d %d",&N,&K);  
   for(i = 0;i < N;i++){  
     scanf("%d",candies + i);  
   }  
   sort(candies,candies+N);  
   int unfairness =  candies[K-1]-candies[0];  
 for(i = 0;i < N-K-1;i++){  
   if(unfairness>candies[i+K-1]-candies[i])  
     unfairness = candies[i+K-1]-candies[i];  
 }  
   printf("%d\n",unfairness);  
   return 0;  
 }  

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;  
 }  

Hackerrank The Love-Letter Mystery Solution

Problem Statement
James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter intopalindromes.
To do this, he follows 2 rules:
(a) He can reduce the value of a letter, e.g. he can change 'd' to 'c', but he cannot change 'c' to 'd'.
(b) In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes 'a'. Once a letter has been changed to 'a', it can no longer be changed.
Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome. 
Source Code:
 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <cstring>  
 #include <algorithm>  
 using namespace std;  
 int main() {  
   /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
   int t;  
   cin>>t;  
   while(t--)  
   {  
     char inp[10000]  ;  
     cin>>inp;  
     int first=0,last=strlen(inp)-1,ans = 0,count;  
     do{  
       int ff = inp[first];  
       int ll = inp[last];  
       if(ff!=ll)  
         {  
         int count = abs (ff-ll);  
         ans = ans +count;  
       }  
       first++;  
       last--;  
     }while(first<last);  
     cout<<ans<<endl;  
   }  
   return 0;  
 }  
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)