Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to find the combination of three elements in an array whose sum is equal to N in Java

Booking.com | Official site | The best hotels, flights, car rentals & accommodations


48 views
asked Sep 6, 2022 by avibootz
edited Sep 6, 2022 by avibootz

1 Answer

0 votes
public class MyClass {
    public static void PrintThreeElements(int[] arr, int N) {
    	int size = arr.length;
    	
    	for (int i = 0; i < size; i++) {
    		for (int j = i + 1; j < size; j++) {
    			for (int k = j + 1 ; k < size; k++) {
    				if (arr[i] + arr[j] + arr[k] == N) {
    				   System.out.print(arr[i] + " " + arr[j] + " " + arr[k]);
    				   return;
    				}
    			}
    		}
    	}
    }
    public static void main(String args[]) {
        int[] arr = {3, 2, 6, 4, 10, 5, 9, 7, 8, 12};
	    int N = 24;

	    PrintThreeElements(arr, N);
    }
}




/*
run:
 
3 9 12
 
*/

 





answered Sep 6, 2022 by avibootz
...