Showing posts with label 2D Array. Show all posts
Showing posts with label 2D Array. Show all posts

Monday, February 15, 2016

Find Maximum Sum of a SubArray

Example Input: -1,2,6,4,-4,-5,56,78,-2,9
Desired Output: 134 

    public int MaxSumOfSubArray(int... input)    {
        int size = input.length;
        int max = 0;
        boolean isIn = false;
        int current_sum = 0;
        int previous = -1;
        for(int i=0; i<size; i++)    {
            if(!isIn) current_sum = 0;
            if(input[i]>=0)    {
                isIn = true;
                current_sum = current_sum+input[i];
            }    else if(previous>=0 && max<current_sum)    {
                max = current_sum;
                isIn = false;
            }
            previous = input[i];
        }
        return max;
    }

Saturday, December 5, 2015

Chess Knight Move in Board

public class ChessBoard {
    int[][] Board;
    public ChessBoard() {
        Board = new int[8][8];
        for(int i=0; i<8; i++)
            for(int j=0; j<8; j++)
                Board[i][j]='E';//Empty
    }
    public void KnightMove(int x, int y)    {
        if(x>-1 && x<8 && y>-1 && y<8)
            Board[x][y]='K';//Knight
        else    {
            System.out.println("Error: Not valid Points");
            return;
        }
        int a=x+2;int b=y+1;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        System.out.println(a+" "+b);
        a=x+2;b=y-1;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x-2;b=y+1;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x-2;b=y-1;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x+1;b=y+2;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x-1;b=y+2;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x+1;b=y-2;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        a=x-1;b=y-2;
        if(a>-1 && a<8 && b>-1 && b<8)
            Board[a][b]='T';//Threten
        printBoard();
    }
    public void printBoard()    {
        for(int i=0; i<8; i++)    {
            StringBuffer sb = new StringBuffer();
            for(int j=0; j<8; j++)
                sb.append((char)Board[i][j]).append(", ");
            System.out.println(sb.toString());
        }
    }
}

Thursday, December 3, 2015

Make all diagonal to 0 when encountered a 0

Given a 2D array, write a method MakeDiagonalZeroWhenEncounteredZero() to convert all diagonal to zero when encountered a zero.

import java.util.ArrayList;

public class ChessBoard {
    public int[][] Board;
    public ChessBoard(int fill) { //Pass 1 for fill
        this.Board = new int[8][8];
        for(int i=0; i<8; i++)
            for(int j=0; j<8; j++)
                this.Board[i][j]=fill;
    }
    public void FillZero(int x, int y)    {
        if(x<8 && x>-1 && y<8 && y>-1)
            Board[x][y]=0;
        else
            System.out.println("Error: Possition Not Valid");
    }
    public void MakeDiagonalZeroWhenEncounteredZero()    {
        ArrayList<Coordinates> zeros = getZeroCordinates();
        for(Coordinates c: zeros)    {
            int x=c.getX();
            int y=c.getY();
            while(x<7 && y<7)    {
                x++; y++;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x>0 && y>0)    {
                x--; y--;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x<7 && y>0)    {
                x++; y--;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x>0 && y<7)    {
                x--; y++;
                Board[x][y]=0;
            }
        }
    }
    private ArrayList<Coordinates> getZeroCordinates()    {
        ArrayList<Coordinates> zeros = new ArrayList<Coordinates>();
        for(int i=0; i<8; i++)
            for(int j=0; j<8; j++)    {
                if(Board[i][j]==0)
                    zeros.add(new Coordinates(i,j));
            }
        return zeros;
    }
    public void PrintBoard()    {
        for(int i=0; i<8; i++)    {
            StringBuffer sb = new StringBuffer();
            for(int j=0; j<8; j++)    {
                sb.append(Board[i][j]).append(", ");
            }
            System.out.println(sb.toString());
        }
    }
}
class Coordinates    {
    private int x, y;
    public Coordinates(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
}





Main Method:
        ChessBoard cb = new ChessBoard(1);
        cb.FillZero(4, 5);
        cb.FillZero(7, 2);
        System.out.println("Input: ");
        cb.PrintBoard();
        cb.MakeDiagonalZeroWhenEncounteredZero();
        System.out.println("Output: ");
        cb.PrintBoard();

   
Input:
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
Output:
1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 0,
1, 1, 1, 1, 0, 1, 0, 1,
1, 1, 1, 1, 1, 0, 1, 1,
0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 1,
UA-39217154-2