Skip to main content

Posts

Showing posts with the label math

Java 8 Coding challenge: Raees's Liquor Tank

Raees wants to build a new tank to hide his illegal liquor . He comes up with some designs . Now he asks you to find out maximum units of liquor he can fill in the tank. You are given  N  elements , corresponding to heights of walls . See the figure for input 0 ,8 ,0 ,4 ,0 ,3 ,0 ,5 ,0 ,3 ,0 ,1 ,0. Maximum amount of liquor that can be filled is 5 + 1 + 5 + 2 + 5 + 3 + 1 = 22. Eg : For input 4 ,2 ,4 ,3 Output will be 2 . INPUT First line of input contains a single integer  N . Second line of input contains  N  non-negative integers (  A0  ---  A(n-1)  ). OUTPUT Single integer , answer to the Raees's question. CONSTRAINTS 1 <=  N  <= 1000 1 <=  Ai  <= 1000 SAMPLE INPUT 13 0 8 0 4 0 3 0 5 0 3 0 1 0 SAMPLE OUTPUT 22  Code: import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Q1 {...

Java 8 Coding challenge: Raees's Liquor Tank

Raees wants to build a new tank to hide his illegal liquor . He comes up with some designs . Now he asks you to find out maximum units of liquor he can fill in the tank. You are given N elements , corresponding to heights of walls . See the figure for input 0 ,8 ,0 ,4 ,0 ,3 ,0 ,5 ,0 ,3 ,0 ,1 ,0. Maximum amount of liquor that can be filled is 5 + 1 + 5 + 2 + 5 + 3 + 1 = 22. Eg : For input 4 ,2 ,4 ,3 Output will be 2 . INPUT First line of input contains a single integer N . Second line of input contains N non-negative integers ( A0 --- A(n-1) ). OUTPUT Single integer , answer to the Raees's question. CONSTRAINTS 1 <= N <= 1000 1 <= Ai <= 1000 SAMPLE INPUT 13 0 8 0 4 0 3 0 5 0 3 0 1 0 SAMPLE OUTPUT 22  Code: import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Q1 { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; priv...

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); } }