Thursday, 26 March 2015

Hackerrank Bigger is Greater Solution

Problem Statement 

Given a word w, rearrange the letters of w to construct another word s in such a way that, s is lexicographically greater than w. In case of multiple possible answers, find the lexicographically smallest one.

Source Code:

This program uses STL, if you have solved it without using STL comment the source code below.

 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <algorithm>  
 #include <string.h>  
 using namespace std;  
 int main() {  
   int t;  
   cin>>t;  
   while(t--){  
     char a[1000];  
     int aa[1000]={0};  
     cin>>a;  
     int n = strlen(a);  
     if(next_permutation(a,a+n)){  
       cout<<a<<endl;  
     }else{  
       cout<<"no answer"<<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..... :) :) :)

Hackerrank Manasa and Stones Solution

Problem Statement
Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of either aor b. Legend has it that there is a treasure trove at the end of the trail and if Manasa can guess the value of the last stone, the treasure would be hers. Given that the number on the first stone was 0, find all the possible values for the number on the last stone.
Note: The numbers on the stones are in increasing order.

Source Code:

 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <algorithm>  
 #include <string.h>  
 using namespace std;  
 int main() {  
   /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
   int t;  
   cin>>t;  
   while(t--){  
     int n,a,b,gt,lw;  
     cin>>n>>a>>b;  
     if(a>b){  
       gt =a ;  
       lw = b;  
     }else{  
       lw = a;  
       gt = b;  
     }  
     int ans = lw*(n-1);  
     n--;  
     int dif = gt-lw;  
     cout<<ans<<" ";  
     if(dif>0){  
       while(n--){  
         ans += dif;  
         cout<<ans<<" ";  
       }  
     }  
     cout<<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..... :) :) :)

Sunday, 15 March 2015

Hackerearth Balance strings Solution

PROBLEM STATEMENT:

Balance strings, by definition, are the strings that contain all the characters of the alphabet, from a to z, equal no of times. eg- abcdefghijklmnopqrstuvwxyz is a balanced string, aabb is not.
Input:
First line contains number of test cases T. Each test case contains a string S which made up of only lowercase characters.
Output:
For each test case print Yes if the string is balanced string else print No.
Constraints:
1<=T<=10
1<=|S|<=1000

SOURCE CODE:

 #include <iostream>  
 using namespace std;  
 int main()  
 {  
   int t;  
   cin>>t;  
   while(t--){  
        char arr[10000];  
        int a[500]={0},mx=0;  
        cin>>arr;  
        for(int i=0;arr[i]!='\0';i++){  
             int temp = arr[i]-97;  
             a[temp]++;  
             if(a[temp]>mx){  
                  mx = a[temp];  
             }  
        }  
        int flag = 1;  
        for(int i=0;i<26;i++){  
             if(a[i]!=mx){  
                  flag = 0;  
             }  
        }  
        if(flag)  
        {  
             cout<<"Yes"<<endl;  
        }  
        else{  
             cout<<"No"<<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 Hackerearth. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)


Saturday, 14 March 2015

Hackerearth What is the string made of? solution

PROBLEM STATEMENT:

You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.
digits made of dashes
You have to write a function that takes this string message as an input and returns a corresponding value in terms of a number. This number is the count of dashes in the string message.
Note:
0 consists of 6 dashes, 1 consists of 2 dashes, 2 consists of 5 dashes, 3 consists of 5 dashes, 4 consists of 4 dashes, 5 consists of 5 dashes, 6 consists of 6 dashes, 7 consists of 3 dashes [though the figure shows that 7 consists of 4 dashes but due to minor mistake in the problem please write your solution assuming 7 consists of 3 dashes], 8 consists of 7 dashes, 9 consists of 6 dashes.
Constraints
  • String message will contain at least one digit, but not more than 100
  • Each character in code will be a digit ('0'-'9').

SOURCE CODE:

#include <iostream>
#include <string.h>
using namespace std;  
 int main() {  
      int arr[100]={6,2,5,5,4,5,6,3,7,6},ans = 0;  
      char str[10000] ;  
      cin>>str;  
      for(int i=0;str[i]!='\0';i++)  
   {  
     int temp = str[i] -48 ;  
     ans += arr[temp];  
   }  
      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 Hackerearth. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

Monday, 9 March 2015

Hackerrank Pangrams Solution

Problem Statement
Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly because it is a pangram. ( pangrams are sentences constructed by using every letter of the alphabet at least once. )
After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams.
Given a sentence s, tell Roy if it is a pangram or not.

(Explanation for the code is available in the first comment)


Source Code:
 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <algorithm>  
 #include <stdio.h>  
 using namespace std;  
 int main() {  
   /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
   char arr[10000];  
   gets(arr);  
   int a[150]={0};  
   for(int i =0;arr[i]!='\0';i++){  
     char t;  
     t = tolower(arr[i]);  
     int n = t;  
     if(n>0)  
     {  
       a[n]++;  
       //cout<<n<<endl;  
     }  
   }  
   int flag = 1;  
   for(int i = 97;i<123;i++){  
     if(a[i]==0){  
       flag = 0;  
     }  
   }  
   if(flag == 1)  
     cout<<"pangram"<<endl;  
   else  
     cout<<"not pangram"<<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..... :) :) :)

Sunday, 8 March 2015

Hackerrank Valid PAN format Solution

Problem Statement
The equivalent of SSN in India is a PAN number, which is unique to each of its citizens. In any of the country's official documents, the PAN number is listed as follows
<char><char><char><char><char><digit><digit><digit><digit><char>
Your task is to figure out if the PAN number is valid or not. A valid PAN number will have all its letters in uppercase and digits in the same order as listed above.
Source Code:


 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #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 a[100];  
     int flag = 1;  
     cin>>a;  
     for(int i=0;a[i]!='\0';i++){  
       int temp = a[i];  
       if(i<5){  
         //cout<<a[i]<<" ";  
         //cout<<temp<<" ";  
         if(temp<65 || temp>90){  
           flag = 0;  
         }  
       }else if(i<9){  
         if(temp<48 || temp>57){  
           flag = 0;  
         }  
       }else{  
         if(temp<65 || temp>90){  
           flag = 0;  
         }  
       }  
     }  
     if(flag){  
       cout<<"YES"<<endl;  
     }else{  
       cout<<"NO"<<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..... :) :) :)

Hackerrank Angry Professor Solution

Problem Statement
The professor is conducting a course on Discrete Mathematics to a class of N students. He is angry at the lack of their discipline, and he decides to cancel the class if there are less than Kstudents present after the class starts.
Given the arrival time of each student, your task is to find out if the class gets cancelled or not.
Source Code:
 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #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--){  
     int n,k,temp,cnt=0;  
     cin>>n>>k;  
     for(int i=0;i<n;i++){  
       cin>>temp;  
       if(temp<=0){    
         cnt++;  
       }  
     }  
     if(cnt>=k){  
       cout<<"NO"<<endl;  
     }else{  
       cout<<"YES"<<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..... :) :) :)

Tuesday, 3 March 2015

Hackerrank Sherlock and Array Solution

Problem Statement
Русский \| 中文
Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum is considered to be zero.
Formally, find an i, such that, A1+A2...Ai-1 = Ai+1+Ai+2...AN.

Source Code:

 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #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--){  
     int arr[1000000],n,flag = 0;  
     unsigned long long lsum=0,rsum = 0;  
     cin>>n;  
     for(int i=0;i<n;i++){  
       cin>>arr[i];  
       rsum += arr[i];       
     }  
     for(int i=0;i<n;i++)   
     {  
       rsum -= arr[i];  
       if(i>0){  
         lsum += arr[i-1];  
       }  
       if(lsum == rsum){  
         flag = 1;  
       }  
     }  
     if(flag == 1){  
       cout<<"YES"<<endl;  
     }else{  
       cout<<"NO"<<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..... :) :) :)