eureka
Question:

Write a Java program to find all combination of four elements of an given array whose sum is equal to a given value


Answer:

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]); } } } } } }

Post a question

Answer from Our Experts
RenStudy
About RenStudy
  • About Us
  • Terms of Use
  • FAQ
  • Site Map
Useful Links
  • Become a Tutor
  • Post a Question
  • Solutions Search
  • All Questions
  • Feedback
Contact Us
20905 SE
7th Pl, Sammamish,
WA, 98074
1-800-586-4564
admin@renstudy.com
renstudyus@gmail.com
JOIN THE CONVERSATION

Copyright © 2018 RenStudy. All rights reserved.