Write a Java program to find all combination of four elements of an given array whose sum is equal to a given value
package com.renstudy.Arrays; public class Combination { public static void main(String[] args) { int arr[] = {20, 30, 40, 50, 1, 2}; int n = arr.length; int m = 73; System.out.println("the value: "+m); System.out.print("Combination of four elements:"); for (int i = 0; i < n - 3; i++) { for (int j = i + 1; j < n - 2; j++) { for (int k = j + 1; k < n - 1; k++) { for (int l = k + 1; l < n; l++) { if (arr[i] + arr[j] + arr[k] + arr[l] == m) System.out.print("\n"+arr[i]+" "+arr[j]+" "+arr[k] +" "+arr[l]); } } } } } }
Copyright © 2018 RenStudy. All rights reserved.