Pages

Showing posts with label Arrays & Lists. Show all posts
Showing posts with label Arrays & Lists. Show all posts

Friday, 21 October 2016

Find a Pair Whose Sum is Closest to Zero in Array

This problem is also called minimum absolute sum pair.

You are given an array of integers, containing both +ve and -ve numbers. You need to find the two elements such that their sum is closest to zero.
importjava.util.Arrays;

/**
 * Class to find the pair whose sum closer to zero.
 * @authorrajesh.kumar
 */
public classSumClosestToZero {

     public static voidmain(String[] args) {
           int[] array = {10,12,14,16,-8,10,18,19,7,-6};

           getPairWithCloserToZeroSum(array);

     }

     /**
      * Method to print the pair.
      * @param array
      */
     private static voidgetPairWithCloserToZeroSum(int[] array) {
           Arrays.sort(array);
           int length = array.length;
          
           if(length==0 || length==1) {
                System.out.println("No pair exists !!");
           }
          
           int i = 0;
           int j = length -1;
           int minSum = array[i] + array[j];
           int minL = i; int minR= j;
           while (i  <  j) {
                int sum = array[i] + array[j] ;
                /* If sum of the elements at index i and j equals 0 */
                if (Math.abs(minSum)>Math.abs(sum)) {
                     minSum = sum;
                     minL = i;
                     minR = j;
                } else if(sum<0) {
                     i++;
                 } else {
                     j--;
                }
           }
           System.out.println("Pair is"
               +array[minL]+","+array[minR]+")");
     }
}

Find two elements in Array whose sum is Zero

SumZeroAmazon.com
import java.util.Arrays;

/**
 * Class to find the pair whose sum equal to zero.
 * @author rajesh.kumar
 */
public class SumZeroAmazon {

     public static void main(String[] args) {
           int[] array = {10,12,14,16,-8,10,18,19,6,-6};

           getPairWithZeroSum(array);

     }

     /**
      * Method to print the pair.
      * @param array
      */
     private static void getPairWithZeroSum(int[] array) {
           Arrays.sort(array);
           int length = array.length;
          
           if(length==0 || length==1) {
                System.out.println("No pair exists !!");
           }
          
           int i = 0;
           int j = length -1;
          
           while (i  <  j) {

                /* If sum of the elements at index i and j equals 0 */
                if (array[i] + array[j] == 0) {
                     System.out.println("Pair is ("+array[i]+","+array[j]+")");
                     return;
                } else if(Math.abs(array[i]) > Math.abs(array[j])) {
                     i++;
                } else {
                     j--;
                }
           }
           System.out.println("No pair exists !!");
     }
}

Monday, 6 June 2016

Reverse merge the half linked list

Write the production level code to arrange the linked list as given example:

1->2->3->4->5->6, make the following changes 1->6->2->5->3->4.

class Node {
     int value; Node next;
     public Node(intvalue) {
           this.value = value;
     }
}

public class ArrangeSuffledList {
     private static void getReversedSuffledList(Node head) {
           if(head==null|| head.next==null) {
                printList(head);
                return;
           }
          
           Node end = head.next;
           int length = 1;
           while(end != null) {
                end = end.next;
                length++;
           }

           Node mid = head.next;
           for (inti = 0; i < length/2-1; i++) {
                mid = mid.next;
           }

           //printList(mid);
           Node head2 = new Node(mid.value);
           Node next = mid.next;

           for (inti = 0; i < length-length/2-1; i++) {
                Node tNext = next.next;
                next = new Node(next.value);
                Node temp = next;
                temp.next = head2;
                head2 = temp;
                next = tNext;       

           }

           printList(mergeList(head, head2, length));

     }

     private static Node mergeList(Node head1, Node head2, int length) {
           Node head = new Node(head1.value);
           head.next = new Node(head2.value);
           Node next = head.next;

           int count = 2;

           head1 = head1.next;
           head2 = head2.next;

           while(head2!=null) {
                Node temp = newNode(head1.value);
                next.next = temp;
                next = next.next;
                count++;
                if(count<length) {
                     temp = newNode(head2.value);
                     next.next = temp;
                     next = next.next;
                     count++;
                }

                head1 = head1.next;
                head2 = head2.next;
           }
           return head;
     }

     public static void main(String[] args) {
           Node head = new Node(1);
           head.next = new Node(2);
           head.next.next = new Node(3);
           head.next.next.next = new Node(4);
           head.next.next.next.next = new Node(5);
           head.next.next.next.next.next = new Node(6);
           //head.next.next.next.next.next.next = new Node(7);

           /* 1->2->3->4->5->6 :: 1->6->2->5->3->4 * */
           getReversedSuffledList(head);
           //printList(head);
     }

     private static void printList(Node head) {
           System.out.println("");
           Node next = null;
           if(head!=null) {
                System.out.print(head.value+" ");
                next = head.next;
           }

           while(next!=null) {
                System.out.print(next.value+" ");
                next = next.next;
           }
     }
}