Skip to main content

Posts

Showing posts with the label challenge

Java 8 coding challenge: Find Product

Problem: You have been given an array   A of size   N consisting of positive integers. You need to find and print the product of all the number in this array  Modulo     10 9 +7 . Input Format : The first line contains a single integer   N N  denoting the size of the array. The next line contains   N N  space separated integers denoting the elements of the array Output Format : Print a single integer denoting the product of all the elements of the array Modulo    10 9 +7 . Constraints :  1≤N≤10 3 1≤A[i]≤10 3 SAMPLE INPUT 5 1 2 3 4 5 SAMPLE OUTPUT   120 Code: import java.lang.Math; import java.util.Scanner; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc=new Scanner(System.in); int []A=new int[1000]; int a=sc.nextInt(); int i; double answer=1.0; for(i=0;i<a;i++) { A[i]=sc.nextInt(); } for(i=0;i<a;i++...

Java 8 coding challenge: Find Product

Problem: You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo   10 9 +7 . Input Format : The first line contains a single integer N N denoting the size of the array. The next line contains N N space separated integers denoting the elements of the array Output Format : Print a single integer denoting the product of all the elements of the array Modulo 10 9 +7 . Constraints : 1≤N≤10 3 1≤A[i]≤10 3   SAMPLE INPUT 5 1 2 3 4 5 SAMPLE OUTPUT 120 Code: import java.lang.Math; import java.util.Scanner; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc=new Scanner(System.in); int []A=new int[1000]; int a=sc.nextInt(); int i; double answer=1.0; for(i=0;i<a;i++) { A[i]=sc.nextInt(); } for(i=0;i<a;i++) { answer=(A[i]*answer)%((Math.pow(10,9))+7); } System.out.println((int)answer); } }

Java 8 coding challenge: Palindromic String

Problem: You have been given a String  S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String  S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints   1 ≤ | S | ≤ 100 1≤|S|≤100 Note String  S S  consists of lowercase English Alphabets only. SAMPLE INPUT     aba SAMPLE OUTPUT     YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read ...

Java 8 coding challenge: Palindromic String

Problem: You have been given a String S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100 Note String S S consists of lowercase English Alphabets only. SAMPLE INPUT aba SAMPLE OUTPUT YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read input from stdin and provide input before running * Use either of these method...

Java 8 coding challenge: Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits. SAMPLE INPUT 1 2 88 42 99 SAMPLE OUTPUT 1 2 88   Code: import java.util.*; import java.io.*; import java.math.BigInteger; public class Tester { public static long mod=(long)(1e9+7); public static long xx,yy,d; static int minPrime[]; static boolean isPrime[]; public static void main(String[] args) throws IOException { InputReader s=new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out=new PrintWriter(outputStream); while(true) { int n=s.nextInt(); if(n==42) break; else out.println(n); } out.close(); } static void modifiedSieve(int n) { minPrime = new int[n+1]; for(int i=2; i<=Math.sqrt(n); i++) { if(minPrime[i] == 0) {...

Java 8 coding challenge: Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits. SAMPLE INPUT 1 2 88 42 99 SAMPLE OUTPUT 1 2 88 Code: import java.util.*; import java.io.*; import java.math.BigInteger; public class Tester { public static long mod=(long)(1e9+7); public static long xx,yy,d; static int minPrime[]; static boolean isPrime[]; public static void main(String[] args) throws IOException { InputReader s=new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out=new PrintWriter(outputStream); while(true) { int n=s.nextInt(); if(n==42) break; else out.println(n); } out.close(); } static void modifiedSieve(int n) { minPrime = new int[n+1]; for(int i=2; i<=Math.sqrt(n); i++) { if(minPrime[i] == 0) { for(int j=i*i; j<=n...

Java 8 Coding challenge: Factorial!

Problem: You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N.  Input Format: The first and only line of the input contains a single integer N denoting the number whose factorial you need to find. Output Format Output a single line denoting the factorial of the number N. Constraints 1≤N≤101≤N≤10 SAMPLE INPUT  2 SAMPLE OUTPUT  2 Code: import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { try (Scanner input = new Scanner(System.in)) { int fatorial = input.nextInt(); int total=fatorial; for(int i=fatorial-1;i>=1;i--){ total = total * i; } System.out.println(total); } catch (Throwable e) { e.printStackTrace(); } } }

Java 8 Coding challenge: Factorial!

Problem: You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N. Input Format: The first and only line of the input contains a single integer N denoting the number whose factorial you need to find. Output Format Output a single line denoting the factorial of the number N. Constraints 1≤N≤101≤N≤10 SAMPLE INPUT 2 SAMPLE OUTPUT 2 Code: import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { try (Scanner input = new Scanner(System.in)) { int fatorial = input.nextInt(); int total=fatorial; for(int i=fatorial-1;i>=1;i--){ total = total * i; } System.out.println(total); } catch (Throwable e) { e.printStackTrace(); } } }  

Java 8 coding challenge: Roy and Profile Picture

Problem:  Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be  L x L , where  L  is the length of the side of square. Now Roy has  N  photos of various dimensions. Dimension of a photo is denoted as  W x H where  W  - width of the photo and  H  - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains  L . Second...

Java 8 coding challenge: Roy and Profile Picture

Problem: Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be L x L , where L is the length of the side of square. Now Roy has N photos of various dimensions. Dimension of a photo is denoted as W x H where W - width of the photo and H - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains L . Second line contains N , number of photos. Following N lines...