Possible Path

Adam is standing at point (a,b) in an infinite 2D grid. He wants to know if he can reach point (x,y) or not. The only operation he can do is to move to point (a+b,b), (a,a+b), (a-b,b), or (a,a-b) from some point (a,b). It is given that he can move to any point on this 2D grid,i.e., the points having positive or negative X(or Y) co-ordinates.

Tell Adam whether he can reach (x,y) or not.

Input Format
The first line contains an integer, T, followed by T lines, each containing 4 space separated integers i.e. a b x y

Output Format
For each test case, display YES or NO that indicates if Adam can reach (x,y) or not.

Constraints
0 ≤ T ≤ 1000
0 ≤ a,b,x,y ≤ 1018

Sample Input

3
1 1 2 3
2 1 2 3
3 3 1 1

Sample Output

YES
YES
NO

Explanation

(1,1) -> (2,1) -> (2,3).

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T>0)
            {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            while(a!=b)
                {
                if(a>b)
                    a=a-b;
                else
                    b=b-a;
            }
            while(x!=y)
                {
                if(x>y)
                    x=x-y;
                else
                    y=y-x;
            }
            if(a==x)
                System.out.println("YES") ;
            else
                System.out.println("NO") ;
            T=T-1;
        }
    }
}

Compile : http://ideone.com/3HPJ1Q
Twitter : https://twitter.com/codeifucansolve

Strange Grid

#codeifyoucansolve
A strange grid has been recovered from an old book. It has 5 columns and infinite number of rows. The bottom row is considered as the first row. First few rows of the grid are like this:

…………..

…………..

20 22 24 26 28

11 13 15 17 19

10 12 14 16 18

1 3 5 7 9

0 2 4 6 8

The grid grows upwards forever!

Your task is to find the integer in cth column in rth row of the grid.

Input Format

There will be two integers r and c separated by a single space.

Constraints

1≤r≤2 * 109

1≤c≤5

Rows are indexed from bottom to top and columns are indexed from left to right.

Output Format

Output the answer in a single line.

Sample Input

6 3

Sample Output

25

Explanation

The number in the 6th row and 3rd column is 25.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger stval;
        BigInteger mod = new BigInteger("2");
        BigInteger ten = new BigInteger("10");
        BigInteger one = new BigInteger("1");

        BigInteger bg=new BigInteger(sc.next());
        int c;
        c=sc.nextInt();
        if(bg.remainder(mod).intValue() == 0)
            {
            bg=bg.subtract(one);
            bg=bg.divide(mod);
            bg=bg.multiply(ten);
            bg=bg.add(one);
            stval = new BigInteger(bg.toString());
        }
        else
            {
            bg=bg.divide(mod);
            bg=bg.multiply(ten);
            stval = new BigInteger(bg.toString());
        }
        c = (c-1)*2;
        String str = String.valueOf(c);
        System.out.println(stval.add(new BigInteger(str)));
    }
}

Follow on Twitter : https://twitter.com/codeifucansolve

Name string

#codeifyoucansolve
Name string is a string consisting of letters “R”,”K” and “V”. Today Oz wants to design a name string in a beautiful manner. Actually Oz cannot insert these three letters arbitrary anywhere ,he has to follow some rules to make the name string look beautiful. First thing is that the name string should consist of at most two different letters. Secondly adjacent letters in name string must be different.

After this procedure Oz wants name string to be as long as possible. Given the number of “R”,”K” and “V” letters that you have initially ,help Oz to find the maximum length of name string that Oz can make.

Input :
The first line contains the number of test cases T . Each test case consists of three space separated integers – A,B and C representing number of “R” letters, number of “K” letters and number of “V” letters respectively.

Output :
For each test case T, output maximum length of name string that Oz can make.

Constraints :
1 <= T <=100
0 <= A,B,C <=106
Sample Input (Plaintext Link)

2
1 2 5
0 0 2

Sample Output (Plaintext Link)

5
1

Explanation
For first sample :
The longest name string possible is : VKVKV using 3 “V” letters and 2 “K” letters and its length is 5.

#include <stdio.h>

int main()
{
	int T;
	long int A[3],tmp,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%ld%ld%ld",&A[0],&A[1],&A[2]);
		 for(i=0;i<2;i++)
		 {
		 	for(j=i+1;j<3;j++)
		 	{
		 		if(A[j] > A[i])
		 		{
		 			tmp =A[i];
		 			A[i] = A[j];
		 			A[j] = tmp;
		 		}
		 	}
		 }
		 //for(i=0;i<3;i++)
		   //printf("%d ",A[i]);
		 if(A[0] == 1 && A[1] == 0)
		     printf("1\n");
		 else if(A[0] == 1 && A[1] == 1)
		     printf("2\n");
		 else if(A[0] <= 0)
		     printf("0\n");
		else
			 printf("%ld\n",2*A[1]+1);
		  
	}
    return 0;
}

Compile & Run : https://ideone.com/AeC9hL
Follow on Twitter : https://twitter.com/codeifucansolve
Follow on Quora : http://codeifyoucansolve.quora.com/Name-string

Dipu and Interesting Numbers

#codeifyoucansolve
Little Dipu is a small kid and like all the other kids, he likes to play, but he plays with numbers (He is extraordinary you know). Now-a-days Dipu has some extra interest in odd numbers. So, he says that a number N is interesting if it has odd number of divisors. Now Dipu turns to you and asks you to tell him how many interesting numbers are there between two given numbers, L and R (both inclusive).

Input
First line of input contains the number of test cases T. Each of the next T lines contains two space separated integers L and R.
Output
For each test case, output an integer, denoting the count of interesting numbers between L and R (both inclusive).
Constraints
1 <= T <= 100000
1 <= L <= R <= 1018

Sample Input (Plaintext Link)

2
1 3
6 10

Sample Output (Plaintext Link)

1
1

Explanation
In 1st test case, 1 has 1 divisor, 2 has 2 divisors and 3 also has 2 divisors. So only 1 has odd number of divisors. Hence count of interesting numbers is 1.

In 2nd test case, 9 is the only number to have odd number of divisors. So answer is again 1.

#include <stdio.h>
//unsigned long long prime[10000000];
int main()
{
    long int T,flag;
    unsigned long long int L,R,X,D,p,dn,total_div,count_div,k,i,j;
    scanf("%ld",&T);
    while(T--)
    {
    	scanf("%lld%lld",&L,&R);
    	count_div = 0;
    	/*
    	prime[0]=2;
    	prime[1]=3;
    	prime[2]=5;
    	k=3;
    	for(i=6;i<=R;i++)
    	  {
    	  	flag=0;
    	       for(j=2;j<=sqrt(i);j++)
    	       {
    	       	  if(i%j == 0)
    	       	    flag = 1;
    	       }
    	       if(flag == 0)
    	         prime[k++] = i;
    	  }
        */
    	for(X=L;X<=R;X++)
    	{
    		D=X;
    		//p=0;
    		p=2;
    		total_div = 1;
    		while(D>1)
    		{
    			dn=1;
    		    while(D%p == 0)  //D%prime[p]
    		    {
    		    	dn +=1;
    		    	//D=D/prime[p];
    		    	D=D/p;
    		    }
    		    total_div *= dn;
    		    
    		    if(p % 2 == 0)
    		      p+=1;
    		   else
    		      p+=2;
    		   if(p > (X/2))
    		     {
    		     	if(total_div == 1)
    		     	  total_div +=1;
    		     	break;
    		     }
    		}
    		if(X != 1 && total_div == 1)
    		   total_div +=1;
    		if(total_div%2 != 0)
    		   count_div++;
    	}
    	printf("%lld\n",count_div);
    }
    return 0;
}

Compile & Run : https://ideone.com/M0hlzA
Follow on Twitter : https://twitter.com/codeifucansolve
Follow on Quora : http://codeifyoucansolve.quora.com/Dipu-and-Interesting-Numbers

Perfect square number has odd number of divisor so you should do programming on the basis of perfect square

Gem Stones

#codeifyoucansolve
John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lowercase latin letter from ‘a’ to ‘z’. An element can be present multiple times in a rock. An element is called a ‘gem-element’ if it occurs at least once in each of the rocks.

Given the list of N rocks with their compositions, display the number of gem-elements that exist in those rocks.

Input Format
The first line consists of N, the number of rocks.
Each of the next N lines contain rocks’ composition. Each composition consists of lowercase letters of English alphabet.

Output Format
Print the number of gem-elements that are common in these rocks. If there are none, print 0.

Constraints
1 ≤ N ≤ 100
Each composition consists of only lowercase latin letters (‘a’-‘z’).
1 ≤ Length of each composition ≤ 100

Sample Input

3
abcdde
baccd
eeabg

Sample Output

2

Explanation
Only “a”, “b” are the two kind of gem-elements, since these are the only characters that occur in each of the rocks’ composition.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int count[26];
int main() {
    int N,t,i,c=0,j;
    char s[101];
    scanf("%d",&N);
    t=N;
    j=1;
    while(t--)
        {
        scanf("%s",s);
        for(i=0;i<strlen(s);i++)
            {
            if(count[s[i]-97]==(j-1))
                count[s[i]-97]=j;
        }
        j+=1;
    }
    for(i=0;i<26;i++)
        {
        if(count[i]==N)
            c++;
    }
    printf("%d\n",c);
    return 0;
}

Compile & Run : http://ideone.com/1DgUhe
Follow On Twitter : https://twitter.com/codeifucansolve
Follow On Quora : http://codeifyoucansolve.quora.com/Gem-Stones

Make it Anagram

#codeifyoucansolve
Alice recently started learning about cryptography and found that anagrams are very useful. Two strings are anagrams of each other if they have same character set. For example strings “bacdc” and “dcbac” are anagrams, while strings “bacdc” and “dcbad” are not.

Alice decides on an encryption scheme involving 2 large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. She need your help in finding out this number.

Given two strings (they can be of same or different length) help her in finding out the minimum number of character deletions required to make two strings anagrams. Any characters can be deleted from any of the strings.

Input Format
Two lines each containing a string.

Constraints
1 <= Length of A,B <= 10000
A and B will only consist of lowercase latin letter.

Output Format
A single integer which is the number of character deletions.

Sample Input #00:

cde
abc
Sample Output #00:

4
Explanation #00:
We need to delete 4 characters to make both strings anagram i.e. ‘d’ and ‘e’ from first string and ‘b’ and ‘a’ from second string.

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits
3744 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int f[26];
int sd[26];
int main() {
    char s[10000];
    int i,sum=0;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        f[(s[i]-97)]+=1;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        sd[(s[i]-97)]+=1; 
    for(i=0;i<26;i++)
        sum += abs(f[i]-sd[i]);
    printf("%d\n",sum);
    return 0;
}

Compile & Run : http://ideone.com/Z002B8
Follow on Twitter : https://twitter.com/codeifucansolve
Follow on Quora Blog : http://codeifyoucansolve.quora.com/Make-it-Anagram

Anagram

#codeifyoucansolve
Sid is obsessed about reading short stories. Being a CS student, he is doing some interesting frequency analysis with the books. He chooses strings S1 and S2 in such a way |len(S1)−len(S2)|≤1.

Your task is to help him find the minimum number of characters of the first string he needs to change to enable him to make it an anagram of the second string.

Note: A word x is anagram of another word y, if we can produce y by rearranging the letters of x.

Input Format

The first line will contain an integer T representing the number of test cases. Each test case will contain a string having length len(S1)+len(S2) which will be concatenation of both the strings described above in the problem.The given string will contain only characters from a to z.

Output Format

An integer corresponding to each test case is printed in a different line i.e., the number of changes required for each test case. Print −1 if it is not possible.

Constraints

1≤T≤100
1≤len(S1)+len(S2)≤104

Sample Input

6
aaabbb
ab
abc
mnop
xyyx
xaxbbbxx
Sample Output

3
1
-1
2
0
1
Explanation
Test Case #01: We have to replace at least three characters from any of the string to make both of strings anagram. Here, a = “aaa” and b = “bbb”. One possible solution is to replace all character ‘a’ in string a with character ‘b’.
Test Case #02: Either replace ‘a’ with ‘b’, which will generate “bb”. Or replace ‘b’ with ‘a’ to generate “aa”. Both of the solution are valid.
Test Case #03: It is not possible for two strings of unequal length to be anagram for each other.
Test Case #04: We have to replace both the characters of any string to make it anagram of other one.
Test Case #05: _S1_ and S2 are already anagram to each other.
Test Case #06: Here S1 = “xaxb” and S2 = “bbxx”. He had to replace ‘a’ from S1 with ‘b’ so that S1 = “xbxb” and we can rearrange its letter to “bbxx” in order to get S2.

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits
6610 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int f[26];
int sd[26];
int main() {
    int T,sum,i;
    char s[10000];
    scanf("%d",&T);
    while(T--)
        {
        sum = 0;
        scanf("%s",s);
        for(i=0;i<26;i++)
            {
            f[i]=sd[i]=0;
        }
        if(strlen(s)%2 != 0)
            {
            printf("-1\n");
            continue;
        }
        for(i=0;i<(strlen(s)/2);i++)
            f[(s[i]-97)] +=1;
        for(i=(strlen(s)/2);i<(strlen(s));i++)
            sd[(s[i]-97)] +=1;
        for(i=0;i<26;i++)
            sum += abs(f[i]-sd[i]);
            printf("%d\n",(sum/2));
    }
    return 0;
}

Compile & Run : http://ideone.com/NNGGfp
Follow On Twitter : https://twitter.com/codeifucansolve
Follow On Quora Blog : http://codeifyoucansolve.quora.com/Anagram

Pangrams

#codeifyoucansolve
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.

Input Format

Input consists of a line containing s.

Constraints
Length of s can be atmost 103 (1≤|s|≤103) and it may contain spaces, lowercase and uppercase letters. Lowercase and uppercase instances of a letter are considered same.

Output Format

Output a line containing pangram if s is a pangram, otherwise output not pangram.

Sample Input #1

We promptly judged antique ivory buckles for the next prize

Sample Output #1

pangram

Sample Input #2

We promptly judged antique ivory buckles for the prize

Sample Output #2

not pangram

Explanation

In the first testcase, the answer is pangram because the sentence contains all the letters.

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits

Choose a translation

9931 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int chk[26];
int main() {
    char ch;
    int count=0,i;
    while((ch=getchar())!=EOF)
        {
        //printf("%c",ch);
        
        if(ch==' ')
            continue;
        if(ch<97)
            {
        if(chk[(ch-97)] == 0)
            {
            chk[(ch-97)] =1;
            count++;
        }
        if(count==26)
            break;            
        }
        else
            {
        if(chk[(ch-65)] == 0)
            {
            chk[(ch-65)] =1;
            count++;
        }
        if(count==26)
            break;            
        }        
    }
    //return 0;
    
    if(count==26)
        printf("pangram\n");
    else
        printf("not pangram\n");
    return 0;
}

Compile & Run : http://ideone.com/sbogVI
Follow On : https://twitter.com/codeifucansolve
Quora Blog : http://codeifyoucansolve.quora.com/Pangrams

Palindrome Index

#codeifyoucansolve
You are given a string of lowercase letters. Your task is to figure out the index of the character on whose removal will make the string a palindrome. There will always be a valid solution.

In case string is already palindrome, then -1 is also a valid answer along with possible indices.

Input Format

The first line contains T i.e. number of test cases.
T lines follow, each line containing a string.

Output Format

Print the position ( 0 index ) of the letter by removing which the string turns into a palindrome. For string, such as

bcbc

we can remove b at index 0 or c at index 3. Both the answers are accepted.

Constraints
1 ≤ T ≤ 20
1 ≤ length of string ≤ 100005
All characters are latin lower case indexed.

Sample Input

3
aaab
baa
aaa

Sample Output

3
0
-1

Explanation

In the given input, T = 3,

For input aaab, we can see that removing b from the string makes the string a palindrome, hence the position 3.
For input baa, removing b from the string makes the string palindrome, hence the position 0.
As the string aaa is already a palindrome, you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.

Custom Checker logic

https://gist.github.com/shashank21j/58df3865a95bf960092c

Possible doubt is answered here

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits

Choose a translation

Solved score: 20.00pts
5661 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int T,flag;
    char s[100005];
    long i,j;
    scanf("%d",&T);
    while(T--)
        {
        scanf("%s",s);
        flag=0;
        for(i=0,j=(strlen(s)-1);i<j;i++,j--)
            {
            if(s[i]!=s[j])
                {
                if(s[i]!=s[i+1])
                    j=i;
                flag=1;
                break;
            }
        }
        if(flag==1)
            printf("%ld\n",j);
        else
            printf("-1\n");
    }
    
    
    return 0;
}

Compile & Run : http://ideone.com/vn75hA
Follow on Twitter : https://twitter.com/codeifucansolve
Quora Blog : http://codeifyoucansolve.quora.com/Palindrome-Index
 

Encryption

#codeifyoucansolve
An English text needs to be encrypted using the following encryption scheme.
First the spaces are removed from text, let L be the length of this text.
Then, characters are written into a grid, whose rows and columns have the following constraints:

⌊√L⌋≤rows≤column≤⌈√L⌉, where ⌊x⌋ is floor function and ⌈x⌉ is ceil function

For example, the sentence if man was meant to stay on the ground god would have given us roots after removing spaces is 54 characters long, so it is written in the form of a grid with 7 rows and 8 columns.

ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots

Ensure, rows×columns≥L
If multiple grids satisfy the above conditions, choose the one with the minimum area i.e. rows×columns.

The encoded message is obtained by displaying the characters in a column, inserting a space, and then displaying the next column and inserting a space and so on.For example, the encoded message for the above rectangle is:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

You will be given a message in English with no spaces between the words. The maximum message length can be 81 characters. Print the encoded message.

Here are some more examples:

Sample Input:

haveaniceday

Sample Output:

hae and via ecy

Sample Input:

feedthedog

Sample Output:

fto ehg ee dd

Sample Input:

chillout

Sample Output:

clu hlt io

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits
4397 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    char s[82];
    int i,j,len,sq;
    scanf("%s",s);
    len =   strlen(s);
    sq = sqrt(len);
    if(sqrt(len) > sq)
        sq = sq+1;
    for(i=0;i<(sq);i++)
        {
        for(j=i;j<len;j++)
            {
            printf("%c",s[j]);
            j=j+(sqrt(len));
        }
        printf(" ");
    }
    return 0;
}

Compile & Run : http://ideone.com/QCs6Fw
Follow on Twitter : https://twitter.com/codeifucansolve
Quora Blog : http://codeifyoucansolve.quora.com/Encryption