Posts

Showing posts with the label Alorithm design

INTERVIEW BIT SOLUTION

 1) Rotate Matrix Interview Bit Solution using ArrayList public class Solution { public void rotate(ArrayList<ArrayList<Integer>> a) { int n = a.get(0).size(); for(int i=0;i<n/2;i++) { for(int j=i;j<n-i-1;j++) { int temp = a.get(i).get(j); int pp = a.get(n-j-1).get(i); a.get(i).set(j, pp); int oo = a.get(n-i-1).get(n-j-1); a.get(n-j-1).set(i, oo); int ll = a.get(j).get(n-i-1); a.get(n-i-1).set(n-j-1,ll); a.get(j).set(n-i-1,temp); } } } } Using array import java.util.Arrays; public class Rotate2DArrayInPlace { public static void main (String args [] ) throws Exception { Integer [] [] myNxNArray = new Integer [][] { {1, 2, 3}, {4,5,6}, {7,8,9} }; System.out.println("SOURCE ARRAY: \n" + Arrays.deepToString(myNxNArray)); Integer [][] rotatedInPlace = rotateMatrixInPlaceCounterClockwise(myNxNArray); System.out.println("ROTATED IN PLACE - 90 degrees COUNTER CLOCKWISE"); System.out.println(Arrays.deepToStrin...

All Algorithms of Searching & Sorting in the Best & Easiest way.

SEARCHING  &  SORTING ALGORITHM Linear Search     for(start to end of array) { if (current_element equals to 5) { print (current_index); } }    Time Complexity: O(N) Space Complexity: O(1) Binary Search int binarySearch ( int low , int high , int key ) { while ( low <= high ) { int mid =( low + high )/ 2 ; if ( a [ mid ]< key ) { low = mid + 1 ; } else if ( a [ mid ]> key ) { high = mid - 1 ; } else { return mid ; } } return - 1 ; //key not found }     Time Complexity: O ( log 2 N )  Space Complexity: O(1) Bubble Sort void bubble_sort ( int A [ ], int n ) { int temp ; for ( int k = 0 ; k < n - 1 ; k ++) { // (n-k-1) is for ignoring comparisons of elements which have already been compared in earlier ite...