Monday 16 February 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.

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..... :) :) :)



Monday 12 January 2015

Hackerrank Alternating Characters Solution

Problem Statement
Shashank likes strings in which consecutive characters are different. For example, he likes ABABA, while he doesn't like ABAA. Given a string containing characters A and B only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string.
Your task is to find the minimum number of required deletions.
Input Format
The first line contains an integer T i.e. the number of test cases.
Next T lines contain a string each.
Output Format
Print minimum number of required steps for each test case.
Constraints
1T10
1lengthofString105 
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
Explanation
AAAAA, 3 deletions
BBBBBB, 4 deletions
ABABABABABABABAB, 0 deletions
BABABABABABA, 0 deletions
AAABBBAB, 4 deletions
Source Code:

#include <cmath>
#include <cstring>
#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 str[1000000];
    long  i,cnt =0,n;
    cin>>str;
    n = strlen(str);
    for(i=0;i<n-1;i++)
        {
        if(str[i]==str[i+1])
            {
            cnt++;
        }
    }
    cout<<cnt<<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 Maximizing XOR Solution

Problem Statement
Given two integers: L and R,
find the maximal values of A xor B given, L ≤ A ≤ B ≤ R
Input Format
The input contains two lines, L is present in the first line.
R in the second line.
Constraints
1 ≤ L ≤ R ≤ 103
Output Format
The maximal value as mentioned in the problem statement.
Sample Input#00
1
10
Sample Output#00
15
Sample Input#01
10
15
Sample Output#01
7
Explanation
In the second sample let's say L=10R=15, then all pairs which comply to above condition are
1010=0
1011=1
1012=6
1013=7
1014=4
1015=5
1111=0
1112=7
1113=6
1114=5
1115=4
1212=0
1213=1
1214=2
1215=3
1313=0
1314=3
1315=2
1414=0
1415=1
1515=0
Here two pairs (10,13) and (11,12) have maximum xor value 7 and this is the answer.

Source Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int maxXor(int l, int r) {

    int answer = 0 ,i,j,temp=0;
    for(i=l;i<=r;i++){
        for(j=i;j<=r;j++){
         
            temp = (i | j) & ~(i & j);
        if(answer<temp)
            answer=temp;
        }
    }
 
    return answer;

}
int main() {
    int res;
    int _l;
    scanf("%d", &_l);
 
    int _r;
    scanf("%d", &_r);
 
    res = maxXor(_l, _r);
    printf("%d\n", res);
 
    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..... :) :) :)