2D Array Implementation

Let's do the implementation and understand it in a deep-dive. We will go step-by-step and implement the most useful method.
Declare and Initialization Phase- O(1)
private int currentRow = 0;
private int currentColumn = 0;
private int maxRow, maxColumn;
private int[][] nums;
private int maxElement;
public MultiDimensionArray(int maxRow, int maxColumn) {
    this.maxRow = maxRow;
    this.maxColumn = maxColumn;
    nums = new int[maxRow][maxColumn];
    maxElement = maxRow*maxColumn;
}
Add Element- O(1)
public void add(int value) {
    if (size() <= maxElement) {
        if (currentColumn <= maxColumn-1) {
            nums[currentRow][currentColumn++] = value;
        } else {
            if (currentRow < maxRow-1) {
                currentRow++;
                currentColumn = 0;
            }
            nums[currentRow][currentColumn++] = value;
        }
    } else
        throw new ArrayIndexOutOfBoundsException();
}
Update the element value for a particular index- O(1)
public int set(int row, int column, int value){
    if (row <= currentRow) {
        if (row == currentRow && column < currentColumn) {
            return findAndReplace(row, column, value);
        } else if (row < currentRow && column < maxColumn){
            return findAndReplace(row, column, value);
        } else
            throw new ArrayIndexOutOfBoundsException(column);
    }
    else
        throw new ArrayIndexOutOfBoundsException(row);
}
Fetch element for a particular index- O(1)
public int get(int row, int column) {
    if (row <= currentRow) {
        if (row == currentRow && column < currentColumn) {
            return nums[row][column];
        } else if (row < currentRow && column < maxColumn){
            return nums[row][column];
        } else
            throw new ArrayIndexOutOfBoundsException(column);
    }
    else
        throw new ArrayIndexOutOfBoundsException(row);
}
Remove element for a particular index- O(n*m)
public void remove(int row, int column) {
    if ((currentRow == row && column <= currentColumn)
                 || (row < currentRow && column < maxColumn)) {
        fastRemoval(row, column);
    } else
        throw new ArrayIndexOutOfBoundsException();
}

private void fastRemoval(int row, int column) {
    for (int i = row; i <= currentRow; i++) {
        int columnStart = (i == row) ? column : 0;
        int endLength = (i == currentRow) ? currentColumn -1 : nums[i].length;
        for (int j = columnStart; j < endLength; j++) {
            if (j < endLength-1 || i == currentRow) {
                nums[i][j] = nums[i][j+1];
            } else {
                if (i != currentRow)
                    nums[i][j] = nums[i+1][0];
            }
        }
    }
    if (currentColumn == 0) {
        currentColumn = maxColumn -1;
        currentRow--;
    } else
        currentColumn--;
}
Search element from the start and return first found index- O(n*m)
public int indexOf(int val) {
    for (int i = 0; i <= currentRow; i++) {
        int startIndex = (i == currentRow) ? currentColumn-1 : nums[i].length-1;
        for (int j = 0; j <=startIndex; j++) {
            if (nums[i][j] == val)
              return  ((i) * maxColumn) + j;
        }
    }
    return -1;
}
Search element from the end and return first found index- O(n*m)
public int lastIndexOf(int val) {
    for (int row = currentRow; row >= 0; row--) {
        int startIndex = (row == currentRow) ? currentColumn-1 : nums[row].length-1;
        for (int column = startIndex; column >= 0; column--) {
            if (nums[row][column] == val)
              return (row*maxColumn) + column;
        }
    }
    return -1;
}
Check element is exists- O(n*m)
public boolean contains(int value) {
    for (int i = 0; i <= currentRow; i++) {
        for (int j = 0; j < currentColumn; j++) {
            if (nums[i][j] == value)
                return true;
        }
    }
    return false;
}
Check is array empty- O(1)
public boolean isEmpty() {
    return currentColumn == 0  && currentRow == 0;
}
Get the size- O(1)
public int size() {
    return (((currentRow) * maxColumn) + currentColumn+1);
}

Comments

Popular posts from this blog

Recursion

Stack

Data Structure & Algorithm