Archive for the ‘Algorithms’ Category.

Print a Square Matrix Spirally – Java Source Code

   public static void printSpirally(int[][] a) {
        int N = a[0].length;
        for (int i = N - 1, j = 0; i > 0; i--, j++) {
            for (int k = j; k < i; k++)System.out.println(a[j][k]);
            for (int k = j; k < i; k++)System.out.println(a[k][i]);
            for (int k = i; k > j; k--)System.out.println(a[i][k]);
            for (int k = i; k > j; k--)System.out.println(a[k][j]);
        }
        int middle = (N / 2);
        if ((N % 2) == 1) {
            System.out.println(a[middle][middle]);
        }
    }

Reverse Vowels in a String – Java Source Code

    public static boolean isVowel(char inp) {
        char c = Character.toUpperCase(inp);

        if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') || (c == 'U')) {
            return true;
        }

        return false;
    }

    public static void reverseVowels(char[] a) {
        int start = 0;
        int end = a.length - 1;

        while (start < end) {
            while ((start < a.length) && !isVowel(a[start])) {
                start++;
            }

            while ((end > 0) && !isVowel(a[end])) {
                end--;
            }

            if ((start > = end) || (start == a.length) || (end == -1)) {
                return;
            }

            //swap start and end;
            char t = a[start];
            a[start] = a[end];
            a[end] = t;
            start++;
            end--;
        }
    }

Card Shuffle – Java Source Code


  public static void shuffle(int[] a)
  {
    int N = a.length;
    Random r = new Random(new Date().getTime());
    for (int i = 0; i < a.length-1; i++)
    {
       int tmp=0;
       // Find a random element from i to N-1
       int randElem = i + r.nextInt(N-i);
       //Replace randElem with current element
       tmp = a[i];a[i]=a[randElem];a[randElem]=tmp;
    }
  }

Find 2 Elements in a Sorted Array with a given Sum – Java Source Code

Find if the sum of two elements in a sorted array sum up to b (a number) with complexity of O(n).

  public static void find2ElementsOfSum(int[] a,int S)
  {
    int left = 0;
    int right = a.length-1;
    while(left < right)
    {
      int sum = a[left]+a[right];
      System.out.println("current Sum="+sum);
      if(sum==S){
       System.out.println("FOUND left="+left+" rigt="+right);
       return;
      }
      if(sum < S)
      {
        left++;
      }else
      {
        right--;
      }
    }
    System.out.println("NOT FOUND");
  }

Maximum SubArray Sum – Java Source Code

Given an array of positive and negative numbers. Find the contiguous sub array of maximum sum.

   public static void subArraySum(int a[])
  {
    int i=0;
    int j=0;
    int start=0;
    int end=0;
    int maxSum = -99999;
    int cSum = 0;
    while(j < a.length)
    {
       cSum +=a[j];
       if(cSum>maxSum)// Set this as the new result
       {
          start=i;
          end = j;
          maxSum=cSum;
       }else if(cSum < 0)
       {
         cSum=0;
         i=j+1;
       }
       j++;
    }
    System.out.println("start="+start+" end="+end+" maxSum="+maxSum);

  }

Insertion Sort – Java Source Code Snippet

  public static void sort(int[] a)
  {
    for (int i = 1; i < a.length; i++)
    {
       int current_val= a[i];
       int j= i;
       while(j > 0 && a[j-1] > current_val)
       {
          a[j]=a[j-1];
          j--;
       }
       a[j]=current_val;

    }

  }

Merge 2 Binary Search Trees

Write a program to mergeĀ  two binary search trees, in O(n) time with O(1) space

Least Common Ancestor in a Tree with only Parent pointers

Given a tree with only children pointing to the parents, find the least common ancestor of 2 nodes.

Find a cycle in a linked list

How would you find a cycle in a linked list? Optimize for speed.

Reverse the words in a sentence

Write a program to reverse the words in a string.String is represented as an array.

Input: hello my world

Expected Output: world my hello