Posts

Showing posts from 2017

Segregate 0's, 1's and 2's

Efficient way of segregating 0's, 1's and 2's in a given array. Input:    0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 Output:   0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2 Java Program: package com.sudharsan.examples; import java.util.Arrays; public class Seggrega012 {     static public void main( final String[] args ) {         int a [] = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};        int left = 0, mid =0 ;        int right = a . length -1;        while ( mid <= right ) {            switch ( a [ mid ]) {            case 0:                swap( a , left , mid );                left ++;                mid ++;          ...

Longest Common Subsequence

Longest common subsequence is the common characters across two strings. Example Input 1 :  abcde Input 2 :  cdfge Output : 3 (c, d, e) Java Program: package com.sudharsan.examples; import java.util.HashSet; import java.util.Set; public class LongestCommonSubsequence {     public static void main( final String[] args ) {         final String s1 = "abcde" ;         final String s2 = "cdfge" ;         final Set<Character> c = new HashSet<>();         System. out .println(lcs( s1 , s2 , s1 .length(), s2 .length(), c ));         System. out .println( c );     }     private static int lcs( final String s1 ,             final String s2 ,             final int m ,         ...

Heap Sort

Time Complexity : O(n logn) Java Program: package com.sudharsan.examples; import java.util.Arrays; public class HeapSort {     private static void heapify( int [] a , int i ) {         int lc = i * 2 + 1;         int rc = i * 2 + 2;         int largest = i ;         if ( lc < a . length && a [ largest ] < a [ lc ]) {             largest = lc ;         }         if ( rc < a . length && a [ largest ] < a [ rc ]) {             largest = rc ;         }         if ( largest != i ) {             swap( a , i , largest );             heapify( a , largest );         }   ...

Median of two arrays

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class MedianOfTwoArrays { public static void main(final String[] args) { findMedianSortedArrays(Arrays.asList(-50, -41, -40, -19, 5, 21, 28), Arrays.asList(-50, -21, -10)); findMedianSortedArrays(Arrays.asList(0, 23), Collections.emptyList()); } public static double findMedianSortedArrays(final List<Integer> a, final List<Integer> b) { final List<Integer> temp = new ArrayList<>(); int sizeA = a.size() == 0 ? 0 : a.size(); int sizeB = b.size() == 0 ? 0 : b.size(); int i = 0, j = 0; // Merge both the list into single list. while (i != sizeA && j != sizeB) { if (a.get(i) <= b.get(j)) { temp.add(a.get(i)); i++; } else { temp.add(b.get(j)); j++; } } while (i <= sizeA - 1) { temp.add(a.get(i)); i++; } while (j <= sizeB - 1) { temp.add(b.get(j)...

Convert Infix into Postfix expression

import  java.util.Stack; public   class  InfixToPostfix { private   final   static  Stack<Character>  stack  =  new  Stack<>(); static   boolean  isOperand( final   char   c ) { if  (( c  >=  'a'  &&  c  <=  'z' ) || ( c  >= 0 &&  c  <= 9)) { return   true ; } return   false ; } static   boolean  isOperator( final   char   c ) { switch  ( c ) { case   '+' : case   '-' : case   '*' : case   '/' : return   true ; } return   false ; } static   int  prec( final   char   c ) { switch ( c ) { case   '+' : case   '-' : return  1; case   '/' : case   '*' : return  2; case   '^' : return  3; ...

Producer Consumer Using Blocking Queue

package com.sudharsan.examples; import java.util.ArrayList; import java.util.List; class Producer implements Runnable { private List queue; private int threshold; public Producer(final List queue, int threshold) { this.queue = queue; this.threshold = threshold; } @Override public void run() { for (int i = 0; i queue; private int threshold; Consumer(final List queue, final int threshold) { this.queue = queue; this.threshold = threshold; } @Override public void run() { for (int i = 0; i list = new ArrayList (); final Producer1 producer = new Producer1(list, 20); final Consumer1 consumer = new Consumer1(list, 20); final Thread producerThread = new Thread(producer, "Producer"); final Thread consumerThread = new Thread(consumer, "Consimer"); producerThread.start(); consumerThread.start(); } }

Singleton using Lazy Holder pattern in Java

package com.sudharsan.examples; public class Singleton {  private Singleton() {    }  public static class LazyHolder {   private static final Singleton singleton = new Singleton();     public static Singleton getInstance() {    return singleton;   }  } }

Reading a file using Java

package com.sudharsan.examples; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.io.IOException; import java.nio.file.Files; public class FileExample { static public void main(final String... arg) throws IOException { final Path path = Paths.get("/Users/sudharsanthumatti/Desktop/file.txt"); byte[] data = Files.readAllBytes(path); System.out.println(Arrays.toString(data)); } }

Tiny URL Converter

package com.sudharsan.examples; public class TinyUrl { static public void main( final String[] args ) { String tinyURL = convertIdIntoTinyUrl(12345); int id = convertURLIntoInt( tinyURL ); System. out .println( id ); } private static int convertURLIntoInt(String tinyURL ) { int id = 0; char temp [] = tinyURL .toCharArray(); for ( int i = 0; i < tinyURL .length() ; i ++) { if ( tinyURL .charAt( i ) >= 'a' && tinyURL .charAt( i ) <= 'z' ) { id = ( id *62) + temp [ i ] - 'a' ; }  if ( tinyURL .charAt( i ) >= 'A' && tinyURL .charAt( i ) <= 'Z' ) { id = ( id * 62) + temp [ i ] - 'A' + 26; } if ( tinyURL .charAt( i ) >= '0' && tinyURL .charAt( i ) <= '9' ) { id = ( id * 62) + temp [ i ] - '0' + 52; } } return id ; } private static Strin...

Blocking Queue

package com.sudharsan.examples; import java.util.LinkedList; public class BlockingQueue { private LinkedList queue = new LinkedList (); private int threshold = 25; public BlockingQueue(int threshold) { this.threshold = threshold; } public void enqueue(T t) throws InterruptedException { if (this.queue.size() == threshold) { wait(); } if (this.queue.size() == 0) { notifyAll(); } this.queue.add(t); } public T dequeue() throws InterruptedException { if (this.queue.size() == 0) { wait(); } if (this.queue.size() == threshold) { notifyAll(); } return this.queue.remove(0); } }

Installation of CentOS in Virtual Box step by step

Image
Add caption Open Virtual Box Enter any name and select type as 'Linux' . Enter the the version as 'Red Hat' Click Next and select the memory size. The configuration might vary from one system to another system. Create a Virtual Hard drive. Select the Hard drive file type as 'VDI (Virtual Disk Image)'. Creating a file size depends on your PC configuration. Click on the Virtual machine created and Goto 'Settings'. Click 'Storage' on left panel and select 'Controller: IDE' and select the ISO file from your system. Restart your Virtual Machine. Below is the list of commands we need to run in case if the GUI is missing from the ISO file: Commands to install KDE desktop. yum -y groupinstall "Desktop" "Desktop Platform" "X Window System" "Fonts" ...

Russian Peasent Multiplication

package com.sudharsan.examples; public class RussianPeasentMultiplication { static public void main( final String... arg ) { int a = 18, b = 20; int result = 0; while ( b > 0) {   if ((1 & b ) == 1) { result += a ; } a = a << 1; b = b >> 1; } System. out .println( "Result  " + result ); } } OUTPUT: 360

Count no of Coins

JAVA PROGRAM: package com.sudharsan.examples; public class CoinsCount { static public void main( final String[] args ) { int a [] = {1, 2, 3}; System. out .println(count( a , a . length , 6)); } private static int count( int [] a , int m , int n ) { if ( n == 0) { return 1; } if ( n <= 0) { return 0; } if ( m <= 0 && n >= 1) { return 0; } return count( a , m -1, n ) + count( a , m , n - a [ m -1]); } } OUTPUT: 7

Remove Duplicates in a Sorted Array

JAVA PROGRAM : package com.sudharsan.examples; import java.util.Arrays; public class RemoveDuplicatesInSortedArray { static public void main( final String[] args ) { int [] a = {1, 2, 2, 3, 3, 3, 4, 6, 6, 8, 8, 9, 9, 10}; int b [] = removeDuplicates( a ); System. out .println(Arrays.toString( b )); } private static int [] removeDuplicates( final int [] a ) { if ( a != null   && a . length <= 2) { return a ; } int i = 0, j = 1; while ( j < a . length -1 ) { if ( a [ i ] != a [ j ]) { i ++; j ++; } else { while ( a [ i ] == a [ j ]) { j ++; } i ++; a [ i ] = a [ j ]; } } return Arrays.copyOf( a , i + 1); } } OUTPUT: [1, 2, 3, 4, 6, 8, 9, 10]