Last active
February 10, 2021 05:54
-
-
Save sunloverz/0e8b5a047246fc62c6e68e6592091c3d to your computer and use it in GitHub Desktop.
Sum of pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hash_table; | |
import java.util.ArrayList; | |
public class SumOfPairs { | |
public static void getPairs(int[] arr, int sum) { | |
int length = arr.length; | |
boolean[] seen = new boolean[length]; | |
ArrayList<ArrayList<Integer>> list = new ArrayList<>(); | |
ArrayList<Integer> temp; | |
for(int i=0;i<arr.length;i++){ | |
for(int j=i+1;j<arr.length;j++) { | |
if(arr[i]+arr[j] == sum && !seen[j] && !seen[i]){ | |
temp = new ArrayList<>(); | |
temp.add(arr[i]); | |
temp.add(arr[j]); | |
list.add(temp); | |
seen[j] = true; | |
seen[i] = true; | |
} | |
} | |
} | |
System.out.println(list.toString()); | |
} | |
public static void main(String[] args) { | |
int[] arr = {5, 5, 5, 0, 0, 0, 5}; | |
getPairs(arr, 5); | |
int[] arr2 = {3, 3, 6, 0}; | |
getPairs(arr2, 6); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment