Monday 5 November 2012

In 3 Different Integer Array Print the Combination of 3 Array Element that Sum upto Zero


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg3arraysum;

/**
 *
 * @author Sunshine
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a1[] = new int[]{2, -3, 7, 8};
        int a2[] = new int[]{4, -6, 2, -1};
        int a3[] = new int[]{-6, 4, 7, 0};

        sort(a1);
        sort(a2);
        sort(a3);
     
        int sum;
     
        for(int i=0;i<a1.length;i++){
            int j=0,k=a3.length-1;
            while(j<a2.length && k>0){
               sum=a1[i]+a2[j]+a3[k];
               if(sum>0)
                   --k;
               else if(sum<0)
                   ++j;
               else if(sum==0)
                  System.out.println(a1[i]+"+"+a2[j]+"+"+a3[k]+"=0");
            }
            }
     
    }

    public static void sort(int[] a) {
        int j;
        boolean flag = true;
        int temp;

        while (flag) {
            flag = false;
            for (int i = 0; i < a.length - 1; i++) {
                if(a[i]>a[i+1]){
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                flag = true;
                }
            }
        }
    }
}

String Manipulation - Printing Sub Strings of a String in Incremental Order & Replacing '=' When StringIndexOutOfBoundsException Occurs


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package something;

import java.util.List;

/**
 *
 * @author Sunshine
 */
public class Something {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
           String input = "RaviKiran";  
     
        int index = 0;
        for (int i = 1;    ; i++) {                
            for (int j = 0; j < i; j++) {
                try {
                System.out.print(input.charAt(index));
                } catch (StringIndexOutOfBoundsException ex) {              
                    System.out.print("=");
                }
                index++;
            }
            System.out.println();
            if (index >= input.length()) {
                break;
            }
    }
}
}


OUTPUT


run:
R
av
iKi
ran=
BUILD SUCCESSFUL (total time: 0 seconds)


Java: In place Negative and Positive separation Of Elements in an Array


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package separateposneg;

/**
 *
 * @author Sunshine
 */
public class SeparatePosNeg {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
        // TODO code application logic here
        int array[] = new int[]{2, -7, -6, -9, -5, 11, -19, 8, 14, 17};
        int neg,pos=0, temp;
        int n = array.length;
        for (int i = 0; i < n; i++)
        {
            if (array[i] > 0)
            {          
                temp=array[i];
                array[pos]=temp;            
                pos++;          
                for(int k =i ; k >pos ;k--)
                {            
                 array[k]=array[k-1];
                }          
            }
}
 for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);        }
    }
}