Created
September 17, 2016 14:32
-
-
Save jaisontj/1e0ae9c620128a8368893b854c69f814 to your computer and use it in GitHub Desktop.
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
/*For an input of 1, 2, 3, 4, 7, 6, 5, 9, 2, 1, 4, 11, 3 | |
Should print out -> (1,1),(2,2),(3,3),(4,4),(6,7),(9,11)*/ | |
import java.util.Collections; | |
public class PatternExtractor { | |
List<Integer> array = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 7, 6, 5, 9, 2, 1, 4, 11, 3)); | |
public static void main(String[] args) { | |
for (int i = 0; i < array.size(); i++) { | |
int value1 = array.get(i); | |
for (int j = i + 1; j < array.size(); j++) { | |
if (value1 == array.get(j)) { | |
System.out.println("(" + value1 + "," + array.get(j) + ")"); | |
array.remove(j); | |
array.remove(i); | |
i--; | |
break; | |
} | |
} | |
} | |
Collections.sort(array,Collections.reverseOrder()); | |
for (int i = 0; i < array.size(); i++) { | |
int value1 = array.get(i); | |
int value2 = -1; | |
int value2Position = -1; | |
for (int j = i + 1; j < array.size(); j++) { | |
if (Math.abs(value1 - array.get(j)) <= 2) { | |
if (value2 < array.get(j)) { | |
value2 = array.get(j); | |
value2Position = j; | |
} | |
} | |
} | |
if (value2Position != -1) { | |
if (value1 < value2) | |
System.out.println("(" + value1 + "," + value2 + ")"); | |
else System.out.println("(" + value2 + "," + value1 + ")"); | |
array.remove(value2Position); | |
array.remove(i); | |
i--; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment