Thursday, 14 December 2017

Pangrams Hacker Rank Problem Solution using JAVA

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 , tell Roy if it is a pangram or not.
Input Format
Input consists of a string .
Constraints 
Length of  can be at most   and it may contain spaces, lower case and upper case letters. Lower-case and upper-case instances of a letter are considered the same.
Output Format
Output a line containing pangram if  is a pangram, otherwise output not pangram.
Sample Input
Input #1
We promptly judged antique ivory buckles for the next prize    
Input #2
We promptly judged antique ivory buckles for the prize    
Sample Output
Output #1
pangram
Output #2
not pangram
Explanation
In the first test case, the answer is pangram because the sentence contains all the letters of the English alphabet.
Java Code :
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[] ) throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ Scanner sc = new Scanner(System.in); int[] Lettercount = new int[26]; String str = sc.nextLine(); String lstr = str.toLowerCase(); for(char ch:lstr.toCharArray()) { if(ch != ' ') { Lettercount[ch-'a']++; } } int count = 0; for(int j =0;j<26;j++) { if(Lettercount[j] >0) { count++; } } if(count == 26) { System.out.print("pangram"); } else { System.out.print("not pangram"); } } } 
 

Thursday, 7 December 2017

Birthday Cake Candles Hacker Rank Problem Solution Using JAVA.

Colleen is turning  years old! Therefore, she has  candles of various heights on her cake, and candle  has height . Because the taller candles tower over the shorter ones, Colleen can only blow out the tallest candles.
Given the  for each individual candle, find and print the number of candles she can successfully blow out.
Input Format
The first line contains a single integer, , denoting the number of candles on the cake. 
The second line contains  space-separated integers, where each integer  describes the height of candle .
Constraints
Output Format
Print the number of candles Colleen blows out on a new line.
Sample Input 0
4
3 2 1 3
Sample Output 0
2
Explanation 0
We have one candle of height , one candle of height , and two candles of height . Colleen only blows out the tallest candles, meaning the candles where . Because there are  such candles, we print  on a new line.
Java Code :
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 in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        int count = 0;
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int greater = ar[0];
        for(int i =1 ; i<n ;i++)
        {
            if(ar[i]>greater)
            {
                greater = ar[i];
            }
        }   
        for(int z= 0;z<n;z++)
        {
            
            if(greater == ar[z])
            {
                count++;
            }
        }    
        System.out.print(count);           
        }
        }

Funny String Hacker Rank Problem Solution Using JAVA.

Suppose you have a String, , of length  that is indexed from  to . You also have some String, , that is the reverse of String  is funny if the condition  is true for every character from  to .
Note: For some String  denotes the ASCII value of the  -indexed character in . The absolute value of an integer, , is written as .
Input Format
The first line contains an integer,  (the number of test cases). 
Each line  of the  subsequent lines contain a string, .
Constraints
Output Format
For each String  (where ), print whether it is  or  on a new line.
Sample Input
2
acxz
bcxz
Sample Output
Funny
Not Funny
Explanation
Test Case 0 
 
 
 
As each comparison is equal, we print .
Test Case 1 
, but  
At this point, we stop evaluating  (as ) and print .
Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String funnyString(String s){
        // Complete this function
        
        int count1 =0;
        int count2 =0;
        int j=0;
        for(int i=0;i<s.length()-1;i++)
        {
            count1 = count1+Math.abs((int)s.charAt(i)-(int)s.charAt(i+1));
            count2 = count2+Math.abs((int)s.charAt(s.length()-i-1)-(int)s.charAt(s.length()-i-2));
          if(count1 != count2)
          {
              j++;
              break;
          }        
        }
          if(j>0)
            return "Not Funny";
        else
            return "Funny";
       
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            String result = funnyString(s);
            System.out.println(result);
        }
    }
}

LCM of n numbers Using Java

Found it in Geeks For Geeks : https://www.geeksforgeeks.org/lcm-of-given-array-elements/ A good Solution to be Considered: // Java Pro...