Friday, 19 January 2018

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 Program to find LCM of n elements

public class GFG
{
public static long lcm_of_array_elements(int[] element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
while (true)
{
int counter = 0;
boolean divisible = false;
for (int i = 0; i < element_array.length; i++)
{

// lcm_of_array_elements (n1,n2,... 0) = 0.
// For negative number we convert into
// positive and calculate lcm_of_array_elements.

if (element_array[i] == 0)
{
return 0;
}
else if (element_array[i] < 0)
{
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1)
{
counter++;
}

// divide element_array by devisor if complete division i.e. without
// remainder then replace number with quotient; used for find
// next factor

if (element_array[i] % divisor == 0)
{
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}

// If divisor able to completely divide any number from array
// multiply with lcm_of_array_elements and store into lcm_of_array_elements
// and continue to same divisor
// for next factor finding. else increment divisor

if (divisible)
{
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else
{
divisor++;
}

// Check if all element_array is 1 indicate we found all factors and
// terminate while loop.

if (counter == element_array.length)
{
return lcm_of_array_elements;
}
}
}
public static void main(String[] args)
{
int[] element_array = {222, 111, 222, 333, 666};
System.out.println(lcm_of_array_elements(element_array));

}
}


Kangaroo Hacker Rank Solution in Java

You are choreograhing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).
  • The first kangaroo starts at location  and moves at a rate of  meters per jump.
  • The second kangaroo starts at location  and moves at a rate of  meters per jump.
You have to figure out a way to get both kangaroos at the same location as part of the show.
Complete the function kangaroo which takes starting location and speed of both kangaroos as input, and return  or  appropriately. Can you determine if the kangaroos will ever land at the same location at the same time? The two kangaroos must land at the same location after making the same number of jumps.
Input Format
A single line of four space-separated integers denoting the respective values of , and .
Constraints
Output Format
Print YES if they can land on the same location at the same time; otherwise, print NO.
Note: The two kangaroos must land at the same location after making the same number of jumps.
Sample Input 0
0 3 4 2
Sample Output 0
YES
Explanation 0
The two kangaroos jump through the following sequence of locations:
image
From the image, it is clear that the kangaroos meet at the same location (number  on the number line) after same number of jumps ( jumps), and we print YES.
Sample Input 1
0 2 5 3
Sample Output 1
NO
Explanation 1
The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.
Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
static String meetornot(int low,int lows,int high,int highs) {
    while(low != high)
    {
        low = low+lows;
        high = high+highs;
        if(low>high)
        {
            break;
        }
    }
    if(low==high)
    {
        return "YES";
    }
    else
    {
        return "NO";
    }
}
    static String kangaroo(int x1, int v1, int x2, int v2) {
        // Complete this function
        if((x2>x1 && v2>v1)||(x1>x2 && v1>v2)){return "NO";}
        else
        {
            if(x1>x2)
            {
                String res = meetornot(x2,v2,x1,v1);
                return res;
            }
            else
            {
                 String res = meetornot(x1,v1,x2,v2);
                return res;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x1 = in.nextInt();
        int v1 = in.nextInt();
        int x2 = in.nextInt();
        int v2 = in.nextInt();
        String result = kangaroo(x1, v1, x2, v2);
        System.out.println(result);
    }
}




Thursday, 18 January 2018

HackerLand University has the following grading policy:
  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.
Sam is a professor at the university and likes to round each student's  according to these rules:
  • If the difference between the  and the next multiple of  is less than , round  up to the next multiple of .
  • If the value of  is less than , no rounding occurs as the result will still be a failing grade.
For example,  will be rounded to  but  will not be rounded because the rounding would result in a number that is less than .
Given the initial value of  for each of Sam's  students, write code to automate the rounding process. Complete the function solve that takes an integer array of all grades, and return an integer array consisting of the rounded grades. For each , round it according to the rules above and print the result on a new line.
Input Format
The first line contains a single integer denoting  (the number of students). 
Each line  of the  subsequent lines contains a single integer, , denoting student 's grade.
Constraints
Output Format
For each  of the  grades, print the rounded grade on a new line.
Sample Input 0
4
73
67
38
33
Sample Output 0
75
67
40
33
Explanation 0
image
  1. Student  received a , and the next multiple of  from  is . Since , the student's grade is rounded to .
  2. Student  received a , and the next multiple of  from  is . Since , the grade will not be modified and the student's final grade is .
  3. Student  received a , and the next multiple of  from  is . Since , the student's grade will be rounded to .
  4. Student  received a grade below , so the grade will not be modified and the student's final grade is .



Java Code:


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

public class Solution {

    static int solve(int grade){
        // Complete this function
        int multi5 = grade;
        while(multi5%5 != 0)
        {
            multi5++;
        }
        if((multi5-grade) < 3)
        {
            return multi5;
        }
        else
            return grade;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] grades = new int[n];
        for(int grades_i=0; grades_i < n; grades_i++){
            grades[grades_i] = in.nextInt();
            if(grades[grades_i]>=38){
             int result = solve(grades[grades_i]);System.out.println(result);}
            else
            { 
             System.out.println(grades[grades_i]);}
        }
        

    }
}






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