Skip to main content

Posts

Showing posts with the label java8

Java 8 coding challenge : Jarvis and Seven Segments

All over the world, peoples are working on energy solution. It would be a tough time for our next generation to survive if we don’t think about solution. Tony stark is working on a new project and wants to display his project using “ seven segment display - concept ”. Tony Stark gave Jarvis a task to find a number from his Favorite list of number for which the energy consumption is lowest. (Assuming that for a digit to represent Tony stark is using 7 bulbs and only those bulbs light up which are required to represent a number and rest other would be completely off. ) Help Jarvis and conserve energy. Seven segment display - https://en.wikipedia.org/wiki/Seven-segment_display Input: First line will contain the number of test cases and for every test case first line will contain length of favorite list and the second line for a test case will contain n numbers Output : For every test case print the answer. If there exist more than 1 numbers for which same number of bulbs are required than...

Java 8 coding challenge : Jarvis and Seven Segments

All over the world, peoples are working on energy solution. It would be a tough time for our next generation to survive if we don’t think about solution. Tony stark is working on a new project and wants to display his project using “ seven segment display - concept ”. Tony Stark gave Jarvis a task to find a number from his Favorite list of number for which the energy consumption is lowest. (Assuming that for a digit to represent Tony stark is using 7 bulbs and  only those bulbs light up which are required to represent a number and rest other would be completely off. ) Help Jarvis and conserve energy. Seven segment display -  https://en.wikipedia.org/wiki/Seven-segment_display Input: First line will contain the number of test cases and for every test case first line will contain length of favorite list and the second line for a test case will contain n numbers Output : For every test case print the answer. If there exist more than 1 numbers for which same number ...

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: Swapping Characters

Your are given a string of length  N . You have to follow some rounds of swapping in the string until the below explained condition is reached. In each round the, you have to swap the  ith and (i+1)th character  , then  (i+2)th and (i+3)th  character and continue till the end of string. In each round , a character from the left will be locked i.e not available for swapping after this round , thus after some rounds of swapping , all the characters will be locked at their final position to stop any more rounds of swapping . See the sample explanation for more clarity. Input The first line of input contains a  T , the number of test case. The first line of each Test case will contain an Integer  N  , denoting the length of the string. The second line of each Test case will contain a string of length  N . Output For each and every test case, output the string after all positions are locked. Constraints 0< T <100 0< N ...

Java 8 Coding challenge: Swapping Characters

Your are given a string of length N . You have to follow some rounds of swapping in the string until the below explained condition is reached. In each round the, you have to swap the ith and (i+1)th character , then (i+2)th and (i+3)th character and continue till the end of string. In each round , a character from the left will be locked i.e not available for swapping after this round , thus after some rounds of swapping , all the characters will be locked at their final position to stop any more rounds of swapping . See the sample explanation for more clarity. Input The first line of input contains a T , the number of test case. The first line of each Test case will contain an Integer N , denoting the length of the string. The second line of each Test case will contain a string of length N . Output For each and every test case, output the string after all positions are locked. Constraints 0< T <100 0< N <10000 SAMPLE INPUT 2 6 abcdef 3 abc SAMPLE OUTPUT bdfeca bca E...

Java 8 coding challenge: Addition ain't simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules: Every  i th  character of string will be added to every  i th  character of reverse string. Both string will contain only lower case alphabets(a-z). Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details) Input: First line contains a value  N  denoting number of test cases. Next  N  lines contains string  str . Output: For every string  str  output the string that Jack's friends wants from him. Constraints 1 <= N <= 10 1 <= str <= 10^5 SAMPLE INPUT     4 hello codeapocalypse programming world SAMPLE OUTPUT     wqxqw hhtdmqrrqmdthh wfxtebetxfw aajaa Code: import java.io.*; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; p...

Java 8 coding challenge: Addition ain't simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules: Every i th character of string will be added to every i th character of reverse string. Both string will contain only lower case alphabets(a-z). Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details) Input: First line contains a value N denoting number of test cases. Next N lines contains string str . Output: For every string str output the string that Jack's friends wants from him. Constraints 1 <= N <= 10 1 <= str <= 10^5 SAMPLE INPUT 4 hello codeapocalypse programming world SAMPLE OUTPUT wqxqw hhtdmqrrqmdthh wfxtebetxfw aajaa Code: import java.io.*; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; public class AdditionAintSimple { static class Print { private final BufferedWriter bw; public Print() { this.bw = new Buffere...

Java 8 Coding Challenge: COUNT NUMBERS

Your task is pretty simple , given a string  S  , find the total count of numbers present in the digit. Input The first line contains  T  , the number of test cases. The first line of each and every testc ase will contain a integer  N  , the length of the string . The second line of each and every test case will contain a string  S  of length  N . Output For each and every testcase , output the total count of numbers present in the string. Constraints 0<T<200 0<N<10000 SAMPLE INPUT     1 26 sadw96aeafae4awdw2wd100awd SAMPLE OUTPUT     4   Explanation For the first test case , the string given is "sadw96aeafae4awdw2wd100awd". There are total of 4 numbers in the string - [96,4,2,100]. So , we output 4. Code import java.io.BufferedReader; import java.io.InputStreamReader; class TestClass { public static void main(String args[] ) throws Exception { Buf...

Java 8 Coding Challenge: COUNT NUMBERS

Your task is pretty simple , given a string S , find the total count of numbers present in the digit. Input The first line contains T , the number of test cases. The first line of each and every testc ase will contain a integer N , the length of the string . The second line of each and every test case will contain a string S of length N . Output For each and every testcase , output the total count of numbers present in the string. Constraints 0<T<200 0<N<10000 SAMPLE INPUT 1 26 sadw96aeafae4awdw2wd100awd SAMPLE OUTPUT 4 Explanation For the first test case , the string given is "sadw96aeafae4awdw2wd100awd". There are total of 4 numbers in the string - [96,4,2,100]. So , we output 4. Code import java.io.BufferedReader; import java.io.InputStreamReader; class TestClass { public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); boolean bit...

Java 8 coding challenge: Count Divisors

Problem: You have been given  3  integers  l ,  r  and  k . Find how many numbers between  l  and  r  (both inclusive) are divisible by  k . You do not need to print these numbers, you just have to find their count. Input Format The first and only line of input contains 3 space separated integers  l ,  r  and  k . Output Format Print the required answer on a single line. Constraints 1 ≤ l ≤ r ≤ 1000 1≤l≤r≤1000 1 ≤ k ≤ 1000 1≤k≤1000 SAMPLE INPUT     1 10 1 SAMPLE OUTPUT     10 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.*; */ import java.util.Scanner; class TestClass { public static void main(String args[] ) thro...

Java 8 coding challenge: Count Divisors

Problem: You have been given 3  integers l , r and k . Find how many numbers between l  and r  (both inclusive) are divisible by k . You do not need to print these numbers, you just have to find their count. Input Format The first and only line of input contains 3 space separated integers l , r and k . Output Format Print the required answer on a single line. Constraints 1 ≤ l ≤ r ≤ 1000 1≤l≤r≤1000 1 ≤ k ≤ 1000 1≤k≤1000 SAMPLE INPUT 1 10 1 SAMPLE OUTPUT 10 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.*; */ import java.util.Scanner; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int r = sc.nextInt(); int k = sc.nextInt(); sc.close(); int count = 0; ...

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