Skip to main content
Commonmark migration
Source Link

#Java 7, 138 bytes

Java 7, 138 bytes

#Java 7, 138 bytes

Java 7, 138 bytes

Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Java 7, 138 bytes

boolean c(int[]a,int s){String r="";for(int l=a.length,i=0,j;i<l;i++)for(j=-1;++j<l;)if(i!=j)r+=(a[i]+a[j])+",";return r.contains(s+",");} 

Explanation:

boolean c(int[]a,int s){ // Method with integer-array and integer parameters and boolean return-type String r=""; // Temp String for(int l=a.length,i=0,j;i<l;i++) // Loop (1) over the input array for(j=-1;++j<l;) // Inner loop (2) over the input array again if(i!=j) // If the index of both loops aren't equal (so not the same item in the list) r+=(a[i]+a[j])+","; // Append the sum with a comma to the temp-String // End of loop (2) (implicit / single-line body) // End of loop (1) (implicit / single-line body) return r.contains(s+","); // Return whether the temp-String contains the given sum (+ comma) } // End of method 

Test code:

Try it here.

class M{ static boolean c(int[]a,int s){String r="";for(int l=a.length,i=0,j;i<l;i++)for(j=-1;++j<l;)if(i!=j)r+=(a[i]+a[j])+",";return r.contains(s+",");} public static void main(String[] a){ System.out.println(c(new int[]{1,2,3,4,5,6},11)); System.out.println(c(new int[]{4,23,8174,42,-1},41)); System.out.println(c(new int[]{2,1,1,1,53,2},4)); System.out.println(c(new int[]{2,2,2,2},4)); System.out.println(); System.out.println(c(new int[]{1,1,1,1,1,3},3)); System.out.println(c(new int[]{3,4,5},6)); } } 

Output:

true true true true false false