Wednesday, 29 March 2017

Hacker Rank HourGlassMaxValue Solutions

Context
Given a  2D Array:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in  to be a subset of values with indices falling in this pattern in 's graphical representation:
a b c
  d
e f g
There are  hourglasses in , and an hourglass sum is the sum of an hourglass' values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.
Input Format
There are  lines of input, where each line contains  space-separated integers describing 2D Array ; every value in  will be in the inclusive range of  to .
Constraints
Output Format
Print the largest (maximum) hourglass sum found in .
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Explanation
 contains the following hourglasses:
1 1 1   1 1 0   1 0 0   0 0 0
  1       0       0       0
1 1 1   1 1 0   1 0 0   0 0 0

0 1 0   1 0 0   0 0 0   0 0 0
  1       1       0       0
0 0 2   0 2 4   2 4 4   4 4 0

1 1 1   1 1 0   1 0 0   0 0 0
  0       2       4       4
0 0 0   0 0 2   0 2 0   2 0 0

0 0 2   0 2 4   2 4 4   4 4 0
  0       0       2       0
0 0 1   0 1 2   1 2 4   2 4 0
The hourglass with the maximum sum () is:
2 4 4
  2
1 2 4

Solutions : 
import java.util.Scanner;

public class HourGlassMaxValue{
 public static void main(String arr[]){
  try{
   
   Scanner in=new Scanner(System.in);
   
   System.out.println(" Please enter the value : ");
   
   int arrayI[][]=new int[6][6];
   
   int k=36;
   
   for(int i=0;i<arrayI.length;i++){
    for(int j=0;j<arrayI.length;j++){
     arrayI[i][j]=in.nextInt();
     k--;
     System.out.println(" Remaining "+k);
    }
   }
   
   System.out.println(" Number Input completed.");
   
   int maxInputValue=0;
   
   
   for(int i=0;i<arrayI.length;i++){
    for(int j=0;j<arrayI.length;j++){
     
     
     int sum=getMaxValue(arrayI,i,j);
     
     if(sum!=-1){
      if(sum>maxInputValue){
       
       System.out.println(" Max value : "+sum);
       
       maxInputValue=sum;
       
      }
     }
    }
   }
   
   System.out.println(" Max value is : "+maxInputValue);
   
   
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 private static int getMaxValue(int array[][],int x,int y){
  
  if(x==0|| y==0||x==array.length-1||y==array.length-1){
   System.out.println(" X or Y "+x+" "+y);
   return -1;
  }
  
  int sum=array[y+1][x-1]+array[y+1][x]+array[y+1][x+1]+array[y][x]+array[y-1][x-1]+array[y-1][x]+array[y-1][x+1];
  
  System.out.println(" The sum : "+x+" "+y+" are : "+sum);
  
  return sum;
 }
}

Wednesday, 28 December 2016

Duplex (Double End ) Network Chat By Socket Class in Java by CMD

Program for Client And Server :

import java.io.*;
import java.net.*;
import java.util.*;


// This Class "NetTwo" is for Server

public class NetTwo{

public static void main(String arr[]){
try{

ServerSocket server=new ServerSocket(NetTwoConstants.PORT_NUMBER);

System.out.println(" Waiting for client to connect .... ");
Socket socket=server.accept();

System.out.println(" Client connected.... ");

new NetTwoSendThread(socket,"Server");
new NetTwoReceiveThread(socket,"Client");

}catch(Exception e){
System.out.println(e);
}
}
}


// This class for client.

class NetTwoClient{
public static void main(String arr[]){
try{

Socket socket=new Socket(NetTwoConstants.HOST_NAME,NetTwoConstants.PORT_NUMBER);

System.out.println(" Server Connected. \n");

new NetTwoSendThread(socket,"Client");
new NetTwoReceiveThread(socket,"Server");

}catch(Exception e){
System.out.println(e);
}
}
}


//****************


// This class NetTwoSendThread is thread class use for the Send Message from One End to other.


class NetTwoSendThread extends Thread{
private Socket socket;
private String name;

public NetTwoSendThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}

public void run(){
try{

Scanner in=new Scanner(System.in);

PrintWriter out=new PrintWriter(socket.getOutputStream(),true);

String sendMessage="";

do{

sendMessage=in.nextLine();

out.println(sendMessage);

}while(!sendMessage.equalsIgnoreCase("exit"));

socket.close();

System.out.println(" *************** Connection close by "+name+" **************** ");

System.exit(0);

}catch(Exception e){
System.out.println(e);
}
}
}


//************************************************

// This class is of Thread Manage receive message from other end.

class NetTwoReceiveThread extends Thread{
private Socket socket;
private String name;

public NetTwoReceiveThread(Socket socket,String name){
this.socket=socket;
this.name=name;
start();
}

public void run(){
try{

Scanner in=new Scanner(socket.getInputStream());

String receiveMessage="";

do{
receiveMessage=in.nextLine();

System.out.println(name+" : "+receiveMessage);

}while(!receiveMessage.equalsIgnoreCase("exit"));

socket.close();

System.out.println(" **************** connection close ******************** ");

System.exit(0);

}catch(Exception e){
System.out.println(e);
}
}


}


//***************


// This class hold static constants.

class NetTwoConstants{

public static int PORT_NUMBER=1412;

public static String HOST_NAME="localhost";

}



Server Compile by CMD : ----->>>>> 




Client Compile by CMD : ----- >>>>> 





Typing Hello From Server CMD for CLient and message Hello from server receive by client.




Client Server Communications : -->  




Message "Exit" Type by Server to end Communications :--> 



This is total overall :-  *****************************>>>>>>>>>>>>>>>>>>>

Friday, 11 November 2016

Custom similar tree implementaition : Tree Data Structure in OOPS Java

class TOne{

public int m;

public TOne tLeft;

public TOne tRight;

public TOne(){

}


public void transverse(){
System.out.println("value "+m);

if(tLeft!=null){
System.out.println("Left tree ");
tLeft.transverse();
}


if(tRight!=null){

System.out.println("Right tree ");

tRight.transverse();

}
}
public boolean insertValue(int vale){



if(m==0){

m=vale;
return true;

}else if(m!=0){
if(vale<m){

if(tLeft==null){
tLeft=new TOne();
}

return tLeft.insertValue(vale);

}else if(vale>m){

if(tRight==null){
tRight=new TOne();
}

return tRight.insertValue(vale);

}else {
System.out.println(" value equal.");
return false;
}
}

return false;
}

public boolean search(int val){
if(m==val){
return true;
}else if(tLeft!=null){
return tLeft.search(val);
}else if(tRight!=null){
return tRight.search(val);
}else{
return false;
}
}

public static void main(String arr[]){
TOne one=new TOne();

System.out.println(""+one.insertValue(10));


System.out.println(""+one.insertValue(20));


System.out.println(""+one.insertValue(16));

System.out.println(""+one.insertValue(12));

System.out.println("");

one.transverse();

System.out.println("search 10 : "+one.search(10));


System.out.println("search 20 : "+one.search(20));


System.out.println("search 16 : "+one.search(16));


System.out.println("search 4 : "+one.search(4));

System.out.println("search 4 : "+one.size());

}

public int size(){

int size=1;



if(tLeft!=null && tRight!=null){

int leftSize=tLeft.getSize(size);
int rightSize=tRight.getSize(size);

if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}

return leftSize;

}else if(tLeft!=null && tRight==null){
return tLeft.getSize(size);
}else if(tLeft==null && tRight!=null){
return tRight.getSize(size);
}else{
return size;
}


}

public int getSize(int sizze){
if(tLeft==null && tRight == null){
sizze+=1;
return sizze;
}else if(tLeft!=null && tRight==null){
sizze+=1;
return tLeft.getSize(sizze);
}else   if(tLeft==null && tRight!=null){
sizze+=1;
return tRight.getSize(sizze);
}else{

sizze+=1;

int leftSize=tLeft.getSize(sizze);
int rightSize=tRight.getSize(sizze);

if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}

return leftSize;
}

}


}

Sunday, 6 November 2016

Java - dead lock on String

class DeadLockString{

private static final String text1="this is text1";

private static final String text2="this is text2";

public static void main(String arr[]){

DeadLockString obj=new DeadLockString();

ThreadModelOne one=new ThreadModelOne();


ThreadModelTwo two=new ThreadModelTwo();

one.start();

two.start();
}

static class ThreadModelOne extends Thread{

public void run(){
synchronized(text1){
System.out.println("Lock aquired by thread model one in text1 ");
try{
Thread.sleep(2000);
}catch(Exception e){
System.out.println(e);
}

System.out.println("Waiting for lock on text2");

synchronized(text2){
System.out.println(" Locked aquired in text 1 and text 2 by thread model one.");
}
}
}
}


static class ThreadModelTwo extends Thread{

public void run(){
synchronized(text2){
System.out.println("Lock aquired by thread model one in text2 ");
try{
Thread.sleep(2000);
}catch(Exception e){
System.out.println(e);
}

System.out.println("Waiting for lock on text1");

synchronized(text1){
System.out.println(" Locked aquired in text 1 and text 2 by thread model two.");
}
}
}

}
}

Monday, 31 October 2016

Program for convert decimal number to Binary form number output

import java.util.Scanner;

class BinaryNum{

public static void main(String arr[]){
Scanner in=new Scanner(System.in);

System.out.println(" Enter number to convert : ");

BinaryNum obj=new BinaryNum();

int number=in.nextInt();

System.out.println(" The conveted number : "+obj.getBinaryNumber(number));

}


public String getBinaryNumber(int number){
if(number==0){
return "0";
}else if(number==1){
return "1";
}else if(number==2){
return "10";
}else if(number>2){
String bb="";

do{
int result=checkDivision(number);

if(result==0){
bb=result+bb;

number=number/2;
}else if(result==1){
bb=result+bb;
number=(number-1)/2;
if(number==1){
bb="1"+bb;
return bb;
}
}else if(result==10){
bb=result+bb;
return bb;

}
}while(true);
}

return null;
}


public int checkDivision(int number){
if(number==2){
return 10;
}else if(number%2==0){
return 0;
}else if(number%2!=0){
return 1;
}else{
return -1;
}
}

}