Ultimately, I am trying to create a genetic algorithm that will evolve a string that matches a target string. I do not have a conventional coding background, so my code will be extremely messy. Here is my complete code.
public class Main {
public static double similarity(String s1, String s2) {
String longer = s1, shorter = s2;
if (s1.length() < s2.length()) { // longer should always have greater
// length
longer = s2;
shorter = s1;
}
int longerLength = longer.length();
if (longerLength == 0) {
return 1.0;
/* both strings are zero length */ }
/*
* // If you have StringUtils, you can use it to calculate the edit
* distance: return (longerLength -
* StringUtils.getLevenshteinDistance(longer, shorter)) / (double)
* longerLength;
*/
return (longerLength - editDistance(longer, shorter)) / (double) longerLength;
}
public static int editDistance(String s1, String s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
int[] costs = new int[s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
int lastValue = i;
for (int j = 0; j <= s2.length(); j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
int newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length()] = lastValue;
}
return costs[s2.length()];
}
public static void printSimilarity(String s, String t) {
System.out.println(String.format("%.3f is the similarity between \"%s\" and \"%s\"", similarity(s, t), s, t));
}
private static String getCharForNumber(int i) {
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".toCharArray();
if (i > 27) {
return null;
}
return Character.toString(alphabet[i]);
}
public static String generateString(int numChar) {
Random random = new Random();
String randomString = "";
for (int i = 0; i < numChar; i++) {
String temp = getCharForNumber(random.nextInt(27));
randomString += temp;
}
return randomString;
}
public static String returnTwoChildren(String s1, String s2, boolean first) {
// chromosomes
// String s1;
// String s2;
// crossovers
String c1;
String c2;
Random r = new Random();
// get a random indices
int ind1 = r.nextInt(s1.length());
// make sure ind2 > ind1... leaving this part out;
// break both strings into parts like in your picture
String s1part1 = s1.substring(0, ind1);
String s1part2 = s1.substring(ind1);
String s2part1 = s2.substring(0, ind1);
String s2part2 = s2.substring(ind1);
// combine the parts
c1 = s1part1 + s2part2;
c2 = s2part1 + s1part2;
if (first)
return c1;
return c2;
}
Random random;
static String[] population;
static String[] childPopulation;
static String target = "Cat";
public static void createPopulation(int size) {
population = new String[size];
for (int i = 0; i < size; i++) {
population[i] = generateString(3);
// System.out.println(population[i]);
// if (similarity(population[i], target) > 0.3)
// printSimilarity(population[i], target);
}
}
public static void fitness(boolean print) {
for (int i = 0; i < population.length; i++) {
for (int j = i + 1; j < population.length; j++) {
if (similarity(population[j], target) > similarity(population[i], target)) {
String temp = population[i];
population[i] = population[j];
population[j] = temp;
}
}
if (print && similarity(population[i], target) > 0)
System.out.println(population[i] + ", " + similarity(population[i], target));
}
}
public static void createChildPopulation(int size) {
childPopulation = new String[size];
for (int i = 0; i < population.length; i += 2) {
population[i] = returnTwoChildren(population[i], population[i + 1], true);
population[i + 1] = returnTwoChildren(population[i], population[i + 1], false);
}
}
public static void mutate() {
Random random = new Random();
int prob;
String sub1, sub2;
for (int i = 0; i < population.length; i++) {
for (int j = 1; j < population[i].length(); j++) {
prob = random.nextInt(100);
if (prob == 0) {
sub1 = population[i].substring(0, j);
sub2 = population[i].substring(j);
population[i] = sub1 + generateString(1) + sub2;
}
}
}
}
public Main() {
// fightGame(random);
//String string1 = "acbdefghijklmnop";
//String string2 = "1234567891234567";
int populationSize = 80;
createPopulation(populationSize);
boolean print = true;
for (int i = 0; i <= 800; i++) {
if (i % 5 == 0) {
print = true;
System.out.println("Generation: " + i);
}
fitness(print);
if (similarity(population[0], target) == 1.0) {
System.out.println("Succeded! Generation: " + i + " String: " + population[0]);
break;
}
createChildPopulation(populationSize);
mutate();
print = false;
}
// returnTwoChildren(string1, string2);
System.out.println("Done!");
}
public static void main(String[] args) {
new Main();
}
}
When I run the program, it is fine for a few generations then seems to find a problem. I don't know why the strings are getting longer (than three characters). If someone could help point me to the problem, and solution, I would be extremely grateful.
Problematic is this part:
public static void mutate() {
Random random = new Random();
int prob;
String sub1, sub2;
for (int i = 0; i < population.length; i++) {
for (int j = 1; j < population[i].length(); j++) {
prob = random.nextInt(100);
if (prob == 0) {
sub1 = population[i].substring(0, j);
sub2 = population[i].substring(j);
population[i] = sub1 + generateString(1) + sub2;
}
}
}
}
When you hit prob = 0 then your population[i] is mutating, but instead of changing one letter you add one letter.
I think it shold be:
sub1 = population[i].substring(0, j-1);
instead of
sub1 = population[i].substring(0, j);
Then your strings will have always 3 letters
Related
I'm trying to solve this problem given a number 5, we display:
*
*****
*********
*****
*
And so on. So you give it a number and it format it for you like above. I tried to solve it using this code bellow and I can't see where the problem is in my code.
public class Exe01 {
public static String space = "";//global space var
public static String ast = "";//global * var
public static String adjustAst(int numOfAst) {
Exe01.ast = "";
for (int i = numOfAst; i > 0; i--) {
Exe01.ast+="*";
}
return Exe01.ast;
}
public static String adjustSpaces(int numOfSpaces) {
Exe01.space = "";
for (int i = numOfSpaces; i > 0; i--) {
Exe01.space = Exe01.space + " ";
}
return Exe01.space;
}
public static void showAst(int num) {
if (num <= 0 || num % 2 == 0)
System.out.println("arg to the function need to be positive and odd");
else if (num == 1)
System.out.println("*");
else {
int mid = (int) (num / 2);
int numberOfSpaces = num - 1;
for (int i = 0; i < num; i++) {
int k = 0;
if (i < mid) {
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces - 2;
} else if (i == mid) {
numberOfSpaces = 0;
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
} else {
k = k - 4;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
}
}
}
}
public static void main(String args[]) {
Exe01.showAst(5);
}
}
At compilation time it gives me this:
*
*
*
I have seen many resources from the internet but couldn't found the exact help. i am trying to figure out the edit distance between the two strings example:
String a = "put return between paragraph gioo";
String b = "put hello between line phone gio";
here I am always comparing with String a with the other string so here the edit distance should be 4.
I have done some code execution its comparing me with the each character in the string.
int len1 = row10.length();
int len2 = row01.length();
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= len2; j++) {
dp[0][j] = j;
}
for (int i = 0; i < len1; i++) {
char c1 = row10.charAt(i);
for (int j = 0; j < len2; j++) {
char c2 = row01.charAt(j);
if (c1 == c2) {
dp[i + 1][j + 1] = dp[i][j];
} else {
int replace = dp[i][j] + 1;
int insert = dp[i][j + 1] + 1;
int delete = dp[i + 1][j] + 1;
int min = replace > insert ? insert : replace;
min = delete > min ? min : delete;
dp[i + 1][j + 1] = min;
}
}
}
System.out.println(dp[len1][len2]);
Made a sample function. It doesn't really take into the consideration of corner cases but it works. Also, do think about the case sensitivity of the words.
package test;
public class CalcWordDiff {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "My name is ABC.";
String b = "My name xyz.";
System.out.println("Edit distance will be : "+calcDistanceBetweenWords(a,b));
}
public static int calcDistanceBetweenWords(String first, String second)
{
int res = 0;
String[] words_string_first = first.trim().split(" "); // By trim, I removed the Whitespaces if they exist
String[] words_string_second = second.trim().split(" ");
//Check the length of both the arrays
System.out.println("Size of arrays first is : "+words_string_first.length);
System.out.println("Size of arrays second is : "+words_string_second.length);
int lowerWordSentSize = 0;
if(words_string_first.length<=words_string_second.length)
{
lowerWordSentSize = words_string_first.length;
}
else
{
lowerWordSentSize = words_string_second.length;
}
//Now iterate through the array of lower size
for(int i = 0; i< lowerWordSentSize; i++)
{
if(words_string_first[i].equals(words_string_second[i]))
{
//Do nothing, it means both the words are same
}
else
{
System.out.println("Words mismatched at "+(i+1)+" th Position.");
res = i;
}
}
return res;
}
}
I am trying to find the maximum product of two non overlapping palindromic sub-sequences of string s that we'll refer to as a and b. I came up with below code but it's not giving correct output:
public static int max(String s) {
int[][] dp = new int[s.length()][s.length()];
for (int i = s.length() - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i+1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][s.length()-1];
}
For input string "acdapmpomp", we can choose a = "aca" and b ="pmpmp" to get a maximal product of score 3 * 5 = 15. But my program gives output as 5.
Firstly you should traverse the dp table to find out the length of longest palindromic subsequences using bottom up approach, then you can calculate the max product by multiplying dp[i][j] with dp[j+1][n-1] : Given below is the code in C++;
int longestPalindromicSubsequenceProduct(string x){
int n = x.size();
vector<vector<int>> dp(n,vector<int>(n,0));
for(int i=0;i<n;i++){
dp[i][i] = 1;
}
for(int k=1;k<n;k++){
for(int i=0;i<n-k;i++){
int j = i + k;
if(x[i]==x[j]){
dp[i][j] = 2 + dp[i+1][j-1];
} else{
dp[i][j] = max(dp[i][j-1],dp[i+1][j]);
}
}
}
int maxProd = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n-1;j++){
maxProd = max(maxProd,dp[i][j]*dp[j+1][n-1]);
}
}
return maxProd;
}
int multiplyPalindrome(string s) {
int n=s.size(),m=0;
vector<vector<int>> dp(n, vector<int> (n));
for(int i=0;i<n;i++) dp[i][i]=1;
for (int cl=2; cl<=n; cl++) {
for (int i=0; i<n-cl+1; i++){
int j = i+cl-1;
if (s[i] == s[j] && cl == 2) dp[i][j] = 2;
else if (s[i] == s[j]) dp[i][j] = dp[i+1][j-1] + 2;
else dp[i][j] = max(dp[i][j-1], dp[i+1][j]);
}
}
for(int i=0;i<n-1;i++){
m = max( m, dp[0][i]*dp[i+1][n-1] );
}
return m;
}
int palSize(string &s, int mask) {
int p1 = 0, p2 = s.size(), res = 0;
while (p1 <= p2) {
if ((mask & (1 << p1)) == 0)
++p1;
else if ((mask & (1 << p2)) == 0)
--p2;
else if (s[p1] != s[p2])
return 0;
else
res += 1 + (p1++ != p2--);
}
return res;
}
int maxProduct(string s) {
int mask[4096] = {}, res = 0;
for (int m = 1; m < (1 << s.size()); ++m)
mask[m] = palSize(s, m);
for (int m1 = 1; m1 < (1 << s.size()); ++m1)
if (mask[m1])
for (int m2 = 1; m2 < (1 << s.size()); ++m2)
if ((m1 & m2) == 0)
res = max(res, mask[m1] * mask[m2]);
return res;
}
You can loop through all non-overlapping palindromic subsequences and return the maximum value.
public int longestPalindromicSubsequenceProduct(String str) {
int maxProduct = 0;
for (int k = 0; k < str.length(); k++) {
String left = str.substring(0, k);
String right = str.substring(k);
int currProduct = longestPalindromicSubsequence(left) * longestPalindromicSubsequence(right);
maxProduct = Math.max(maxProduct, currProduct);
}
return maxProduct;
}
private int longestPalindromicSubsequence(String org) {
String rev = new StringBuilder(org).reverse().toString();
return longestCommonSubsequence(org, rev);
}
private int longestCommonSubsequence(String str1, String str2) {
int rows = str1.length();
int cols = str2.length();
int[][] dp = new int[rows + 1][cols + 1];
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
if (str1.charAt(r - 1) == str2.charAt(c - 1)) dp[r][c] = 1 + dp[r - 1][c - 1];
else dp[r][c] = Math.max(dp[r - 1][c], dp[r][c - 1]);
}
}
return dp[rows][cols];
}
Your algorithm returns the maximum length of a palyndrome, not the maximum of the product of two lengths.
UPDATE
Here's a possible solution:
public static int max(String s) {
int max = 0;
for (int i = 1; i < s.length()-1; ++i) {
String p1 = bestPalyndrome(s, 0, i);
String p2 = bestPalyndrome(s, i, s.length());
int prod = p1.length()*p2.length();
if (prod > max) {
System.out.println(p1 + " " + p2 + " -> " + prod);
max = prod;
}
}
return max;
}
private static String bestPalyndrome(String s, int start, int end) {
if (start >= end) {
return "";
} else if (end-start == 1) {
return s.substring(start, end);
} else if (s.charAt(start) == s.charAt(end-1)) {
return s.charAt(start) + bestPalyndrome(s, start+1, end-1)
+ s.charAt(end-1);
} else {
String s1 = bestPalyndrome(s, start, end-1);
String s2 = bestPalyndrome(s, start+1, end);
return s2.length() > s1.length() ? s2 : s1;
}
}
Given two numbers as input, return the sum of the numbers. Note that the numbers can be very large and hence are provided as Strings
Sample Input #1:
add("2354725234782357","9999999999999999999999999988888888888")
Sample Output #1:
10000000000000000000002354714123671245
Implementation:
public String add(String str1, String str2) {
int max = str1.length() > str2.length() ? str1.length() : str2.length();
int n1[] = new int[max];
int n2[] = new int[max];
for (int i = 0; i < str1.length(); i++) {
n1[i] = str1.charAt(str1.length() - 1 - i);
}
for (int i = 0; i < str2.length(); i++) {
n2[i] = str2.charAt(str2.length() - 1 - i);
}
int carry = 0;
int sum[] = new int[max + 1];
int k = 0;
for (k = 0; k < max; k++) {
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ((n1[k] + n2[k] + carry) >= 10) {
carry = 1;
} else {
carry = 0;
}
}
sum[max] = carry;
String result = "";
return result;
}
I have implemented my logic but I don't know how to get the output as a string.
Why don't use BigInteger this is much easier and still native java.
private static String add(String s1, String s2)
{
BigInteger n1 = new BigInteger(s1);
BigInteger n2 = new BigInteger(s2);
return n1.add(n2).toString();
}
Anyway, there is one bug in your Code. Dont cast char to int the ascii value is used, which is wrong. Parse it with Character.getNumericValue();
If you have done this, you can concat sum array to a string in reversed order.
Solution:
public static String add(String str1, String str2) {
int max = str1.length() > str2.length() ? str1.length() : str2.length();
int n1[] = new int[max];
int n2[] = new int[max];
for (int i = 0; i < str1.length(); i++)
{
// conver char to int
n1[i] = Character.getNumericValue(str1.charAt(str1.length() - 1 - i));
}
for (int i = 0; i < str2.length(); i++) {
// conver char to int
n2[i] = Character.getNumericValue(str2.charAt(str2.length() - 1 - i));
}
int carry = 0;
int sum[] = new int[max + 1];
int k = 0;
for (k = 0; k < max; k++) {
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ((n1[k] + n2[k] + carry) >= 10) {
carry = 1;
} else {
carry = 0;
}
}
sum[max] = carry;
// concat array in reverse order
StringBuilder sb = new StringBuilder();
for(int i = sum.length - 1; i >= 0; i--)
sb.append(sum[i]);
return sb.toString();
}
Input
add("2354725234782357","9999999999999999999999999988888888888")
Output
10000000000000000000002354714123671245
There is a logic error in your code: you are adding the char value of each integer instead of the integer themselves. You can get the numeric value of a char using Character.getNumericValue(char ch).
Then, you can construct the resulting String by looping over the sum array. The loop must be done in reverse order (to get the correct order). Beware of the first value sum[max], if it is 0, we must not add it to the String (otherwise, we will get a value padded with a 0):
public static String add(String str1, String str2) {
int max = Math.max(str1.length(), str2.length());
int n1[] = new int[max];
int n2[] = new int[max];
for (int i = 0; i < str1.length(); i++) {
//n1[i] = str1.charAt(str1.length() - 1 - i);
n1[i] = Character.getNumericValue(str1.charAt(str1.length() - 1 - i));
}
for (int i = 0; i < str2.length(); i++) {
//n2[i] = str2.charAt(str2.length() - 1 - i);
n2[i] = Character.getNumericValue(str2.charAt(str2.length() - 1 - i));
}
int carry = 0;
int sum[] = new int[max + 1];
int k = 0;
for (k = 0; k < max; k++) {
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ((n1[k] + n2[k] + carry) >= 10) {
carry = 1;
} else {
carry = 0;
}
}
sum[max] = carry;
StringBuilder sb = new StringBuilder();
if (sum[max] > 0) {
sb.append(String.valueOf(sum[max]));
}
for (int i = max - 1; i >= 0; i--) {
sb.append(String.valueOf(sum[i]));
}
return sb.toString();
}
Note that you can also replace
int max = str1.length() > str2.length() ? str1.length() : str2.length();
with
int max = Math.max(str1.length(), str2.length());
I have modified some code:(you can avoid array creation)
public static String add(String str1, String str2) {
int carry=0;
StringBuilder sum=new StringBuilder();
int l1=str1.length();
int l2=str2.length();
while(l1>0 && l2>0){
int s=Character.getNumericValue(str1.charAt(--l1))+Character.getNumericValue(str2.charAt(--l2))+carry;
if(s<10){
sum.append(s);
}else{
sum.append(s%10);
carry=s/10;
}
}
if(l2>0){
while(l2>0){
int s=Character.getNumericValue(str2.charAt(--l2))+carry;
if(s<10){
sum.append(s);
}else{
sum.append(s%10);
carry=s/10;
}
}
}
if(l1>0){
while(l2>0){
int s=Character.getNumericValue(str1.charAt(--l1))+carry;
if(s<10){
sum.append(s);
}else{
sum.append(s%10);
carry=s/10;
}
}
}
if(carry>0){
sum.append(carry);
}
return sum.reverse().toString();
}
You can use a StringBuilder to append all the digits from your int[] from max to 0.
StringBuilder sb = new StringBuilder();
for (int i=max; i>=0; i--)
{
sb.append(String.valueOf(sum[i]));
}
String result = sb.toString();
You can also improve this to skip leading zeroes if you want:
boolean leadingZero = true;
StringBuilder sb = new StringBuilder();
for (int i=max; i>=0; i--)
{
if (sum[i] != 0)
{
leadingZero=false;
}
if (!leadingZero)
{
sb.append(String.valueOf(sum[i]));
}
}
String result = sb.toString();
I'm taking each element as "sum", "first" and "sec". If (first + sec < sum) I'll make a hashset(tmp) of these 3 and put this hashset into a larger hashset(triangle) containing all tmp hashsets. This removes duplicate combinations of 3 numbers. Here's my code. It works but is there any better solution?
public void findTriangle(int[] a){
HashSet<HashSet<Integer>> triangle = new HashSet<HashSet<Integer>>();
HashSet<Integer> tmp;
for(int i=0;i<a.length;i++){
int sum=a[i];
for(int j=0;j<a.length;j++){
int first = a[j];
if(first!=sum){
for(int k=0;k<a.length;k++){
int sec = a[k];
if(sec!=first && sec!=sum && (first + sec < sum)){
tmp = new HashSet<Integer>();
tmp.add(first);
tmp.add(sec);
tmp.add(sum);
triangle.add(tmp);
}
}
}
}
}
for(HashSet<Integer> hs : triangle)
System.out.println(hs);
}
Sort the array and add the triplets to a list -
public static ArrayList<ArrayList<Integer>> get(int[] input) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (input.length < 3) {
return result;
}
Arrays.sort(input);
for (int i = 0; i < input.length - 2; i++) {
int k = i + 2;
for (int j = i + 1; j < input.length; j++) {
while (k < input.length && input[i] + input[j] > input[k]) {
ArrayList<Integer> inner = new ArrayList<Integer>();
inner.add(input[i]);
inner.add(input[j]);
inner.add(input[k]);
result.add(inner);
k++;
}
}
}
return result;
}
Not so optimal works yet. I tried testing the above two solutions and they seemed to not work. May be i was missing something. Hence decided to post my tested solution here. It does not check for duplicates.
Condition to find a triangle: http://www.wikihow.com/Determine-if-Three-Side-Lengths-Are-a-Triangle
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Arrays;
class Triangle {
int a;
int b;
int c;
public Triangle(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
#Override
public String toString() {
return this.a + " " + this.b + " " + this.c;
}
}
public class FindTriangle {
public List<Triangle> findTriangle(List<Integer> points) {
List<Triangle> result = new ArrayList<Triangle>();
System.out.println("Entered");
for (int i = 0; i < points.size(); i++) {
int pt0 = points.get(i);
System.out.println("Entered i:" + i);
for (int j = i + 1; j < points.size() - 2; j++) {
int pt1 = points.get(j);
int pt2 = points.get(j + 1);
Boolean isTri = isTriangle(pt0, pt1, pt2);
if (isTri.equals(Boolean.TRUE)) {
Triangle t = new Triangle(pt0, pt1, pt2);
result.add(t);
}
}
for (int j = 0; j < (i - 1) && j > 0; j++) {
int pt1 = points.get(j);
int pt2 = points.get(j + 1);
Boolean isTri = isTriangle(pt0, pt1, pt2);
if (isTri.equals(Boolean.TRUE)) {
Triangle t = new Triangle(pt0, pt1, pt2);
result.add(t);
}
}
// final
int pt1, pt2;
if (i == 0) {
pt1 = points.get(i + 1);
pt2 = points.get(points.size() - 1);
} else if (i == points.size() - 1) {
pt1 = points.get(0);
pt2 = points.get(i - 1);
} else {
pt1 = points.get(i + 1);
pt2 = points.get(i - 1);
}
Boolean isTri = isTriangle(pt0, pt1, pt2);
if (isTri.equals(Boolean.TRUE)) {
Triangle t = new Triangle(pt0, pt1, pt2);
result.add(t);
}
}
return result;
}
public Boolean isTriangle(Integer pt1, Integer pt2, Integer pt3) {
System.out.println("Pt1, Pt2, Pt3: " + pt1 + ":" + pt2 + ":" + pt3);
if ((pt1 + pt2) > pt3 && (pt1 + pt3) > pt2 && (pt2 + pt3) > pt1) {
System.out.println("This is triangle");
return Boolean.TRUE;
}
return Boolean.FALSE;
}
public ArrayList<ArrayList<Integer>> getTri(int[] input) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (input.length < 3) {
return result;
}
Arrays.sort(input);
for (int i = 0; i < input.length - 2; i++) {
int k = i + 2;
for (int j = i + 1; j < input.length; j++) {
while (k < input.length && input[i] + input[j] > input[k]) {
ArrayList<Integer> inner = new ArrayList<Integer>();
inner.add(input[i]);
inner.add(input[j]);
inner.add(input[k]);
result.add(inner);
k++;
}
}
}
return result;
}
public void findTriangleW(int[] a) {
HashSet<HashSet<Integer>> triangle = new HashSet<HashSet<Integer>>();
HashSet<Integer> tmp;
for (int i = 0; i < a.length; i++) {
int sum = a[i];
for (int j = 0; j < a.length; j++) {
int first = a[j];
if (first != sum) {
for (int k = 0; k < a.length; k++) {
int sec = a[k];
if (sec != first && sec != sum && (first + sec < sum)) {
tmp = new HashSet<Integer>();
tmp.add(first);
tmp.add(sec);
tmp.add(sum);
triangle.add(tmp);
}
}
}
}
}
for (HashSet<Integer> hs : triangle)
System.out.println(hs);
}
public static void main(String[] args) {
FindTriangle f = new FindTriangle();
List<Integer> points = new ArrayList<Integer>();
points.add(1);
points.add(5);
points.add(10);
points.add(7);
System.out.println("Printing final results");
List<Triangle> result = f.findTriangle(points);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i).toString());
}
}
}