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

Wednesday, April 20, 2016

Amazon Question: +1 or -1 Array Searching

Given an array, next element is either +1 or -1 of previous element then find any number k ?

    public int getIndex(int element, int[] input) { //element = k
        int size = input.length;
        for(int i=0; i < size;) {

            if(input[i]==element)
                return i;
            i=i+getPositive(element - input[i]);
        }
        return -9999; // element not found
    }
    private int getPositive(int i)    {
        if (i>0) return i;
        return (i * -1);
    }


Example 1:
Input: 4 5 4 5 6 7 8 9 8 9 10 11
k = 8
Output = 6

Example 2:
Input: 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11
k = 4
Output = 7

Sunday, February 14, 2016

Rotate an Array

    public int[] rotate(int count, int... input)    {
        for(int i=0; i&lt;count; i++)    {
            input = rotate(input);
        }
        return input;
    }
    private int[] rotate(int... input)    {
        int s =  input.length-1;
        int[] output = new int[input.length];
        for(int j=0; j < s; j++)    {
            output[j+1]=input[j];
        }
        output[0]=input[s];
        return output;
    }

Saturday, December 19, 2015

Inserting an Array of Elements in a Linked List

    public void insert(int... data)    {
        LinkedList L = this;
        while(L.next!=null)    {
            L=L.next;
        }
        int size=data.length;
        for(int i=0; i<size; i++)    {
            L.next=new LinkedList(data[i]);
            L=L.next;
        }
    }

Tuesday, December 15, 2015

Get All SubArray of a given Array

    public ArrayList<ArrayList<Integer>> getAllSubArray(int[] a)    {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        for(int subarray_size=1; subarray_size<a.length; subarray_size++)    {
            ArrayList<Integer> newList = new ArrayList<Integer>();
            for(int i=0; i<a.length; i++)    {
                if((subarray_size+i)>a.length-1)
                    break;
                newList.add(a[i+subarray_size]);
                result.add(new ArrayList<>(newList));
            }
        }
        return result;
    }

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,

Tuesday, December 1, 2015

Find Index of an element in a Circularly Sorted Array

input=Find 3 in {6,7,8,9,0,1,2,3,4,5};
output=7

    public int FindIndexCircularArray(int element, int[] input)    {
        int high = input.length-1;
        int low = 0;
        while(low<=high)    {
            int mid=(high+low)/2;
            if(element==input[mid])
                return mid;
            else if(input[low]<=input[mid])    {
                //left array is sorted
                if(element>=input[low] && element<input[mid])
                    high=mid-1;
                else    low=mid+1;
            }    else if(input[mid]<=input[high])    {
                //right array is sorted
                if(element>input[mid] && element<=input[high])
                    low=mid+1;
                else    high=mid-1;
            }
        }
        return -1;
    }

Starting Element of a Rotated Array using Binary Search

Sample Input: 
input={6,7,8,9,0,1,2,3,4,5};
output=4
input={0,1,2,3,4,5,6,7,8,9};
output=0

    public int FindStartIndex(int[] input)    {
        int high = input.length-1;
        int low = 0;
        while(low<high)    {
            if(input[low]<=input[high])
                return low;
            int mid=(high+low)/2;
            if(input[mid]<input[mid+1] && input[mid]<input[mid-1])
                return mid;
            else if(input[mid]>input[low])    {
                //left array is sorted
                //start index is not in left
                low=mid+1;
            }    else if(input[mid]<input[high])    {
                //right array is sorted
                //start index is not in right
                high=mid-1;
            }
        }
        return -1;
    }

Saturday, November 28, 2015

Implementing 3 fixed size Stacks using same Array

public class ThreeStacks {
    int ArraySize;
    int[] Array;
    int[] topPointers =  {-1,-1,-1};
    public ThreeStacks(int size) {
        this.ArraySize = size;
        this.Array = new int[size * 3];
    }
    public int peek(int StackNumber)    {
        if(topPointers[StackNumber-1]>-1)    {
            return Array[(ArraySize*StackNumber)+topPointers[StackNumber]];
        }    else    {
            System.out.println("Error: "+StackNumber+" is empty");
            return ErrorCode;
        }
    }
    public static final int ErrorCode = -999;
    public int pop(int StackNumber)    {
        StackNumber--;
        if(topPointers[StackNumber]>-1)    {
            int ret = Array[(ArraySize*StackNumber)+topPointers[StackNumber]];
            topPointers[StackNumber]--;
            return ret;
        }    else    {
            System.out.println("Error: "+StackNumber+" is empty");
            return ErrorCode;
        }
    }
    public void push(int data, int StackNumber)    {
        int maxSize = (ArraySize*StackNumber)-1;
        if(topPointers[StackNumber-1]<maxSize)    {
            topPointers[StackNumber-1]++;
            int nextLoc = (ArraySize*(StackNumber-1))+topPointers[StackNumber-1];
            Array[nextLoc]=data;
        }    else    {
            System.out.println("Stack "+StackNumber+" is full");
        }
    }
    public void print3Stacks()    {
        StringBuffer sb =new StringBuffer();
        for(int i=0; i<=topPointers[0]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
        sb =new StringBuffer();
        for(int i=ArraySize; i<=ArraySize+topPointers[1]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
        sb =new StringBuffer();
        for(int i=(ArraySize*2); i<=(ArraySize*2)+topPointers[2]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
    }
}

Friday, November 27, 2015

Stack implementation using Array

public class Stack {
    int[] array;
    private int top;
    private int size;
    public Stack(int size) {
        this.size=size;
        array = new int[size];
        top = -1;
    }
    public void push (int... data)    {
        int size = data.length;
        for(int i=0; i<size; i++)
            push(data[i]);
    }
    public void push (int data)    {
        if (top<size-1)    {
            top++;
            array[top]=data;
        }
    }
    public final static int ErrorCode = -999;
    public int pop()    {
        if(!isEmpty())    {
            int a = array[top];
            top--;
            return a;
        }    else return ErrorCode;
    }
    public int peek()    {
        return array[top];
    }
    public boolean isEmpty()    {
        if(top<0) return true;
        else    return false;
    }
    public void printArray()    {
        int size = top+1;
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<size; i++)
            sb.append(array[i]).append(", ");
        System.out.println(sb.toString());   
    }
}
UA-39217154-2