import java.util.Scanner; import java.util.*; public class B_MentalWarmUp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //nth element input System.out.print("Enter the Trio of Elements: "); int n = sc.nextInt(); //Initialize array int [] myArray= { 10, 20, 10, 40, 50, 60, 70}; int sum = 0; //Loop through the array to calculate sum of elements for(int i = 0; i < myArray.length; i++) { n = myArray[i]; sum = sum + n; } System.out.println("Sum of all the elements of an array: " + sum); // If we reach here, then it is out of range { System.out.println("Your input is out of range, TRY AGAIN!"); return; } } }
Write a Java program that finds a trio of elements (i.e. indices of the three numbers) from a given array A whose sum equals a specific target number B as the output. If the user puts a number that is not within the size of the array. The system should respond "Your input is out of range, TRY AGAIN
N
S
success = false i=0,..N-3 j=i+1,..N-2 k=j+1,..,N-1 if a[i]+a[j]+a[k] = S then success = true, exit all the loops
for(int i = 0; i < myArray.length-2; i++) for(int j = i+1; j < myArray.length-1; j++) // ...
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)