package miscellaneous; import java.util.ArrayList; import java.util.List; public class Find2NumbersWhoseSumIsKInArray { public static void main(String[] args) { int[] array = new int[]{10,-1,15,8,-1,15,1}; /*List<Pair> listOfPair = getPairUsingHashtable(array, 14); for (Pair pair : listOfPair) { System.out.print(pair.getA() + " " + pair.getB() + "\n"); }*/ List<Pair> listOfPair = getPairUsing2Pointers(array, 14); for (Pair pair : listOfPair) { System.out.print(pair.getA() + " " + pair.getB() + "\n"); } } //Time Complexity O(n), Space Complexity O(n) private static List<Pair> getPairUsingHashtable(int arr[], int k){ if(arr==null || arr.length<1)return null; List<Pair> listOfPair = new ArrayList<Pair>(); List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { int temp = k - arr[i]; if(list.contains(temp)){ listOfPair.add(new Pair(arr[i], temp)); list.remove(list.indexOf(temp)); }else{ list.add(arr[i]); } } return listOfPair; } //Time Complexity O(n log n), Space Complexity O(1) //Sorting O(n log n) //Finding Pair O(n) //Total complexity: O(n) + O(n log n) = O(n log n) //How O(n) + O(n log n) = O(n log n) ? //For Big O complexity, all you care about is the dominant term. n log(n) dominates n so that's the only term that you care about. //Another Way, the complexity class for function is the union of O(n) and O(n log(n)) but O(n log(n)) is a superset of O(n) so really it is just O(n log(n)). private static List<Pair> getPairUsing2Pointers(int arr[], int k){ if(arr==null || arr.length<1)return null; //First Sort the array System.out.println("Before Sorting :"); printArray(arr); quickSort(arr, 0, arr.length-1); System.out.println("After Sorting :"); printArray(arr); List<Pair> listOfPair = new ArrayList<Pair>(); int i=0; int j=arr.length-1; while(i<=j){ if(arr[i]+arr[j]==k){ listOfPair.add(new Pair(arr[i], arr[j])); i++; j--; }else if(arr[i]+arr[j]>k){ j--; }else{ i++; } } return listOfPair; } private static void quickSort(int[] array, int lowerIndex, int higherIndex) { int i = lowerIndex; int j = higherIndex; int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2]; while (i <= j) { while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { int tmp = array[i]; array[i]=array[j]; array[j]=tmp; i++; j--; } } if (lowerIndex < j) quickSort(array, lowerIndex, j); if (i < higherIndex) quickSort(array, i, higherIndex); } private static void printArray(int arr[]){ for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } class Pair{ private int a; private int b; public Pair(int a, int b) { this.a = a; this.b = b; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } }
Questions on Stack, Queues, Linkedlist, Binary Trees, Sorting, Searching, Graphs etc with solution using Java Language.
Wednesday, 27 May 2015
Given an array and a number x, check for pair in array whose sum is x
Labels:
Array,
miscellaneous
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment