/* This is the solution to the second question.Note: For finding the tuples A[i]+A[j]=A[k] we will traverse the list in such a way that number of accesses are minimized and each such tuple will printed only once and moreover premutation between i and j is prevented. for eg 1 3 7 (means A[1] + A[3] = A[7]) will be printed only once and 3 1 7 will not be printed as they both have same meaning */ import java.util.Random; public class array{ public static void build_array(int []A,int offset) { Random rand = new Random(); int i,value; for(i=0;i<=A.length-1;i++){ value = rand.nextInt(); value = Math.abs(value); value = value%20; A[i] = value+offset; } } public static void print_array(int[] A) { int i; for(i=0;i<=A.length-1;i++){ System.out.print(A[i]+","); } System.out.println(); } public static void main(String args[]) { int []A = new int[20]; int []B = new int[20]; int []C = new int[40]; int i,j,k,value; build_array(A,0); build_array(B,0); build_array(C,10); print_array(A); print_array(B); print_array(C); System.out.println(); int largest,small1,small2; for(i=0;i<20;i++) { for(j=i+1;j<20;j++) { // value = A[i] + A[j]; for(k=j+1;k<20;k++) { if(A[i] > A[j] && A[i] > A[k]) { largest = i; small1= k; small2=j; } else if(A[j] > A[j] && A[j] > A[k]) { largest = j; small1 =k; small2=i; } else { largest = k; small1=i; small2=j; } value = A[small1] + A[small2]; if(value == A[largest] && i!=j && i!=k && j!=k) System.out.println((small1+1)+" "+(small2+1)+" "+(largest+1)); } } } } }