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

enter image description here

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 output the number which occurs first in the Favorite list.

Constraints:
Test cases< 10
A[i] < 10^6
Size of list < 10^5

SAMPLE INPUT
1
5
1 2 3 4 5
SAMPLE OUTPUT
1
Explanation

Number 1 needs only two bulbs to represent.

Code:

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
 
 
 
 
public class Solution {
 
 
 public static void main(String args[]) throws IOException{
 InputReader in=new InputReader(System.in);
 PrintWriter out=new PrintWriter(System.out);
 int T=in.readInt();
 
 while(T-->0){
 long N=in.readLong();
 long min=222222222;
 long minnum=0;
 while(N-->0){
 long num=in.readCount();
 long count=in.getcount();
 if(count<min){
 min=count;
 minnum=num;
 }
 }
 out.println(minnum);
 out.flush();
 
 }
 
 out.close();
 
 }
}
 
class InputReader{
 InputStream in;
 int[] segment={6,2,5,5,4,5,6,3,7,6};
 long count=0;
 InputReader(InputStream in){
 this.in=in;
 }
 
 public long getcount(){
 return count;
 }
 private int read() throws IOException{
 return in.read();
 }
 
 
 
 public char readChar() throws IOException{
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 return (char)n;
 }
 
 public int readInt() throws IOException{
 int number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 public long readCount() throws IOException{
 long number=0;
 count=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 count+=segment[integer];
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 
 public long readLong() throws IOException{
 long number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 private boolean isWhiteSpace(int n){
 if(n=='\n'||n=='\r'||n=='\t'||n==' '||n==-1){
 return true;
 }else{
 return false;
 }
 
 }
 }

Java 8 Coding challenge: Raees’s Liquor Tank

enter image description here

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 ( A0A(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;
 private int numChars;
 private SpaceCharFilter filter;
 
 public InputReader(InputStream stream)
 {
 this.stream = stream;
 }
 
 public int read()
 {
 if (numChars==-1) 
 throw new InputMismatchException();
 
 if (curChar >= numChars)
 {
 curChar = 0;
 try 
 {
 numChars = stream.read(buf);
 }
 catch (IOException e)
 {
 throw new InputMismatchException();
 }
 
 if(numChars <= 0) 
 return -1;
 }
 return buf[curChar++];
 }
 
 public String nextLine()
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 String str = "";
 try
 {
 str = br.readLine();
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 return str;
 }
 public int nextInt()
 {
 int c = read();
 
 while(isSpaceChar(c)) 
 c = read();
 
 int sgn = 1;
 
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 
 int res = 0;
 do 
 {
 if(c<'0'||c>'9') 
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 while (!isSpaceChar(c)); 
 
 return res * sgn;
 }
 
 public long nextLong() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 int sgn = 1;
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 long res = 0;
 
 do 
 {
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 while (!isSpaceChar(c));
 return res * sgn;
 }
 
 public double nextDouble() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 int sgn = 1;
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 double res = 0;
 while (!isSpaceChar(c) && c != '.') 
 {
 if (c == 'e' || c == 'E')
 return res * Math.pow(10, nextInt());
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 if (c == '.') 
 {
 c = read();
 double m = 1;
 while (!isSpaceChar(c)) 
 {
 if (c == 'e' || c == 'E')
 return res * Math.pow(10, nextInt());
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 m /= 10;
 res += (c - '0') * m;
 c = read();
 }
 }
 return res * sgn;
 }
 
 public String readString() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 StringBuilder res = new StringBuilder();
 do 
 {
 res.appendCodePoint(c);
 c = read();
 } 
 while (!isSpaceChar(c));
 
 return res.toString();
 }
 
 public boolean isSpaceChar(int c) 
 {
 if (filter != null)
 return filter.isSpaceChar(c);
 return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
 }
 
 public String next() 
 {
 return readString();
 }
 
 public interface SpaceCharFilter 
 {
 public boolean isSpaceChar(int ch);
 }
 }
 
 public static int[] SOE()
 {
 int n=(int)(1e5); //till which digit
 
 int ip[]=new int[n];
 
 for(int i=2;i<(int)Math.sqrt(n);i++)
 {
 if(ip[i]==0)
 {
 for(int j=i*i;j<n;j=j+i)
 {
 ip[j]++;
 }
 }
 }
 
 ip[1]++; //counts 1 and 0 as primes
 ip[0]++;
 
 return ip;
 }
 
 private static long gcd(long a, long b)
 {
 while (b > 0)
 {
 long temp = b;
 b = a % b; // % is remainder
 a = temp;
 }
 return a;
}
 
 private static long gcd(long[] input)
 {
 long result = input[0];
 
 for(int i = 1; i < input.length; i++) 
 result = gcd(result, input[i]);
 
 return result;
 }
 
 private static long lcm(long a, long b)
 {
 return a * (b / gcd(a, b));
 }
 
 private static long lcm(long[] input)
 {
 long result = input[0];
 
 for(int i = 1; i < input.length; i++) 
 result = lcm(result, input[i]);
 
 return result;
 }
 
 public static void Array_2dsort(Integer[][] a)
 {
 
 Arrays.sort(a, new Comparator<Integer[]>() {
 public int compare(Integer[] int1, Integer[] int2) {
 Integer numOfKeys1 = int1[1]; //about which column u want to sort
 Integer numOfKeys2 = int2[1];
 return numOfKeys1.compareTo(numOfKeys2);
 }
 });
 
 }
 
 
 public static boolean Square(int x)
 {
 boolean ans=false;
 
 int a=(int)Math.sqrt(x);
 
 if(a*a==x)
 ans=true;
 
 return ans;
 }
 
 public static void main(String args[]) throws Exception
 {
 InputReader in=new InputReader(System.in); 
 PrintWriter w=new PrintWriter(System.out);
 
 int n=in.nextInt();
 
 int a[]=new int[n];
 
 for(int i=0;i<n;i++)
 a[i]=in.nextInt();
 
 
 int total=0;
 int l[]=new int[n];
 int r[]=new int[n];
 l[0]=a[0];
 r[n-1]=a[n-1];
 
 for(int i=1;i<n;i++)
 l[i]=(int)Math.max(l[i-1],a[i]);
 
 for(int i=n-2;i>=0;i--)
 r[i]=(int)Math.max(r[i+1],a[i]);
 
 for(int i=0;i<n;i++)
 total+=(int)Math.min(l[i],r[i])-a[i];
 
 w.println(total);
 
 w.close(); 
 }
}

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:

  1. Every ith character of string will be added to every ith character of reverse string.
  2. Both string will contain only lower case alphabets(a-z).
  3. 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 BufferedWriter(new OutputStreamWriter(System.out));
 }
 
 public void print(Object object) throws IOException {
 bw.append("" + object);
 }
 
 public void println(Object object) throws IOException {
 print(object);
 bw.append("\n");
 }
 
 public void close() throws IOException {
 bw.close();
 }
 }
 
 static class Reader
 {
 final private int BUFFER_SIZE = 1 << 16;
 private DataInputStream din;
 private byte[] buffer;
 private int bufferPointer, bytesRead;
 
 public Reader()
 {
 din = new DataInputStream(System.in);
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public Reader(String file_name) throws IOException
 {
 din = new DataInputStream(new FileInputStream(file_name));
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public String readLine() throws IOException
 {
 byte[] buf = new byte[100000]; // line length
 int cnt = 0, c;
 while ((c = read()) != -1)
 {
 if (c == '\n')
 break;
 buf[cnt++] = (byte) c;
 }
 return new String(buf, 0, cnt);
 }
 
 public int nextInt() throws IOException
 {
 int ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do
 {
 ret = ret * 10 + c - '0';
 } while ((c = read()) >= '0' && c <= '9');
 
 if (neg)
 return -ret;
 return ret;
 }
 
 public long nextLong() throws IOException
 {
 long ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do {
 ret = ret * 10 + c - '0';
 }
 while ((c = read()) >= '0' && c <= '9');
 if (neg)
 return -ret;
 return ret;
 }
 
 public double nextDouble() throws IOException
 {
 double ret = 0, div = 1;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 
 do {
 ret = ret * 10 + c - '0';
 }
 while ((c = read()) >= '0' && c <= '9');
 
 if (c == '.')
 {
 while ((c = read()) >= '0' && c <= '9')
 {
 ret += (c - '0') / (div *= 10);
 }
 }
 
 if (neg)
 return -ret;
 return ret;
 }
 
 private void fillBuffer() throws IOException
 {
 bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
 if (bytesRead == -1)
 buffer[0] = -1;
 }
 
 private byte read() throws IOException
 {
 if (bufferPointer == bytesRead)
 fillBuffer();
 return buffer[bufferPointer++];
 }
 
 public void close() throws IOException
 {
 if (din == null)
 return;
 din.close();
 }
 }
 
 public static void main(String[] args) throws Exception{
 Reader rd = new Reader();
 Print pr = new Print();
 
 int t = rd.nextInt();
 String ip;
 
 
 int len;
 
 while(t-->0) {
 ip = rd.readLine();
 len = ip.length();
 char op[] = new char[len];
 
 for (int i = 0; i < len; i++) {
 op[i] = (char) ((( ip.charAt(i) + ip.charAt(len - i - 1) - 193) % 26)+97);
 }
 pr.println(String.valueOf(op));
 }
 pr.close();
 }
}

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

  1. 0<T<200
  2. 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=false;
 int count=0,i=0,j=0,N=0;
 char c='.';
 String ST = "";
 
 for (i=0; i<T; i++)
 {
 count=0;
 N = Integer.parseInt(br.readLine());
 ST = br.readLine();
 
 for(j=0; j<N; j++)
 {
 c = ST.charAt(j);
 if(c>47 && c<58)
 bit=true;
 else
 { if(bit)
 { bit=false;
 count++;
 }
 }
 }
 if(bit)
 { bit=false;
 count++;
 }
 System.out.println(count);
 }
 }
}

Microservices at Netflix scale

2017-03-11 18_37_43-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_38_19-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_40_13-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_40_28-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_40_45-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

Netflix took 7 years to completely transform to microservices. The traditional approach that was followed was that developers contributed to their individual jars/wars which would go through the regular sprint iteration.

2017-03-11 18_42_47-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player.png

As can be seen above that there are issues with the velocity for delivery and reliability.

Any one change in one service it would reflect into the other services which was very difficult to handle. This caused too many bugs and single point database. Few years back the production database of netflix got corrupted and the users/customers saw the following message.

2017-03-11 18_47_48-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player.png

2017-03-11 18_49_46-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_50_22-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

2017-03-11 18_52_13-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_52_53-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

2017-03-11 18_54_05-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_54_44-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

2017-03-11 18_56_36-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player.png

2017-03-11 18_57_58-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player.png

2017-03-11 19_03_16-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_58_26-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_59_06-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 18_59_39-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_00_14-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_00_27-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_00_47-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_01_09-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_01_20-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_01_40-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_02_29-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_02_57-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

2017-03-11 19_07_25-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_07_47-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

2017-03-11 19_13_46-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_10_19-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_11_12-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_11_25-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_13_03-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

At netflix they want their services to isolate single point failures so here comes Hystrix. Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

They test their system with fault injection test framework (FIT).

2017-03-11 19_19_45-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_20_10-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_21_50-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_21_43-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player2017-03-11 19_22_05-GOTO2016•Micros-Download-From-YTPak.com (1).mp4 - VLC media player

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
1lr10001≤l≤r≤1000
1k10001≤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;
 if(l == r && l%k != 0) {
 System.out.println(0);
 System.exit(0);
 }
 
 while (l <= r) {
 if(l % k == 0) {
 break;
 }
 l++;
 }
 
 count = (r - l) / k + 1;
 System.out.println(count);
 }
}

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

Constraints:
1≤N≤103

1≤A[i]≤103

 

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 SS. 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 SS. The String shall consist of lowercase English alphabets only.

Output Format
Print the required answer on a single line.

Constraints 1|S|1001≤|S|≤100

Note
String SS 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 methods for input
 
 //BufferedReader
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String line = br.readLine();
 int N = Integer.parseInt(line);
 */
 //Scanner
 Scanner s = new Scanner(System.in);
 String S = s.nextLine();
 String result="";
 
 for (int i = S.length()-1; i >=0; i--) {
 result=result+S.substring(i,i+1);
 }
 if(result.equals(S)){
 System.out.println("YES");
 }
 else{
 System.out.println("NO");
 }
 }
}

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; j=j+i)
 {
 if(minPrime[j] == 0)
 minPrime[j] = i;
 }
 }
 }
 
 for(int i=2; i<=n; i++)
 {
 if(minPrime[i] == 0)
 minPrime[i] = i;
 }
 
 }
 
 static void Sieve(int n)
 {
 isPrime = new boolean[n+1];
 Arrays.fill(isPrime, true);
 
 isPrime[0]=false;
 isPrime[1]=false;
 
 for(int i=2; i<=Math.sqrt(n); i++)
 {
 if(isPrime[i])
 {
 for(int j=i*i; j<=n; j=j+i)
 isPrime[j]=false;
 }
 }
 
 }
 
 static void extendedEuclid(long A, long B)
 {
 if(B == 0) {
 d = A;
 xx = 1;
 yy = 0;
 }
 else {
 extendedEuclid(B, A%B);
 long temp = xx;
 xx = yy;
 yy = temp - (A/B)*yy;
 }
 }
 
 static long gcd(long a,long b)
 {
 if(b==0)
 return a;
 
 return gcd(b,a%b);
 }
 
 static long modulo(long x,long y,long p) 
 {
 long res = 1;
 x = x % p; 
 
 while (y > 0)
 {
 if (y%2==1)
 res = (res*x) % p;
 
 y = y>>1;
 x = (x*x) % p; 
 }
 return res;
 }
 
 public static void debug(Object... o)
 {
 System.out.println(Arrays.deepToString(o));
 }
 
 static long exp(long a, long b)
 {
 if(b==0)
 return 1;
 if(b==1)
 return a;
 if(b==2)
 return a*a;
 
 if(b%2==0)
 return exp(exp(a,b/2),2);
 else
 return a*exp(exp(a,(b-1)/2),2);
 }
 
 static class Pair implements Comparable<Pair>
 {
 long x,y;
 Pair(long ii, long cc)
 {
 x=ii;
 y=cc;
 }
 
 public int compareTo(Pair o) 
 {
 return Long.compare(this.x, o.x);
 }
 
 public int hashCode() {
 int hu = (int) (x ^ (x >>> 32));
 int hv = (int) (y ^ (y >>> 32));
 return 31 * hu + hv;
 }
 
 public boolean equals(Object o) {
 Pair other = (Pair) o;
 return x == other.x && y == other.y;
 }
 
 public String toString() {
 return "[x=" + x + ", y=" + y + "]";
 }
 
 }
 
 static final class InputReader{
 private final InputStream stream;
 private final byte[] buf=new byte[1024];
 private int curChar;
 private int Chars;
 public InputReader(InputStream stream){this.stream=stream;}
 private int read()throws IOException{
 if(curChar>=Chars){
 curChar=0;
 Chars=stream.read(buf);
 if(Chars<=0)
 return -1;
 }
 return buf[curChar++];
 }
 public final int nextInt()throws IOException{return (int)nextLong();}
 public final long nextLong()throws IOException{
 int c=read();
 while(isSpaceChar(c)){
 c=read();
 if(c==-1) throw new IOException();
 }
 boolean negative=false;
 if(c=='-'){
 negative=true;
 c=read();
 }
 long res=0;
 do{
 if(c<'0'||c>'9')throw new InputMismatchException();
 res*=10;
 res+=(c-'0');
 c=read();
 }while(!isSpaceChar(c));
 return negative?(-res):(res);
 }
 public final int[] nextIntBrray(int size)throws IOException{
 int[] arr=new int[size];
 for(int i=0;i<size;i++)arr[i]=nextInt();
 return arr;
 }
 public final String nextString()throws IOException{
 int c=read();
 while(isSpaceChar(c))c=read();
 StringBuilder res=new StringBuilder();
 do{
 res.append((char)c);
 c=read();
 }while(!isSpaceChar(c));
 return res.toString();
 }
 public final String nextLine()throws IOException{
 int c=read();
 while(isSpaceChar(c))c=read();
 StringBuilder res=new StringBuilder();
 do{
 res.append((char)c);
 c=read();
 }while(c!='\n'&&c!=-1);
 return res.toString();
 }
 private boolean isSpaceChar(int c)
 {
 return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
 }
 }
 
}

 

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 each contains two space separated integers W and H.

Output:
Print appropriate text for each photo in a new line.

Constraints:
1 <= L,W,H <= 10000
1 <= N <= 1000

SAMPLE INPUT
180
3
640 480
120 300
180 180
SAMPLE OUTPUT
CROP IT
UPLOAD ANOTHER
ACCEPTED

 Java code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class TestClass {
 public static void main(String args[] ) throws Exception {
 try{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String line = br.readLine();
 int L = Integer.parseInt(line);
 line = br.readLine();
 int N = Integer.parseInt(line);
 int X[][]=new int[N][2];
 for(int i=0;i<N;i++){
 String input=br.readLine();
 String[] tokens = input.split(" ");
 X[i][0]=Integer.parseInt(tokens[0]);
 X[i][1]=Integer.parseInt(tokens[1]); 
 }
 for(int i=0;i<N;i++){
 if(X[i][0]<L||X[i][1]<L){
 System.out.println("UPLOAD ANOTHER");
 }
 else if(X[i][0]>=L&&X[i][1]>=L){
 if(X[i][0]==X[i][1])
 System.out.println("ACCEPTED");
 else
 System.out.println("CROP IT");
 }
 }
 }
 catch(Exception e){
 System.out.println(e);
 }
 
 
 
 }
}