ONE TEST CASE IS NOT WORKING { WHICH I DON’T KNOW }
ALSO THERE IS NOTHING MENTIONED ABOUT THE ORDER OF PRINTING [ ASCENDING OR DESCENDING] . IF THAT IS THE CASE THEN PLEASE LET ME KNOW ABOUT THAT.
Please verify my code
Please share your code here or cb ide link
THIS IS THE CODE import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n>0) { int [] arr = new int [n]; for(int i =0 ;i<n ;i++) { arr[i] = sc.nextInt(); } int target = sc.nextInt(); Triplets(arr,n,target); } else { System.out.println(“Invalid Input”); } } public static void Triplets(int [] arr , int n , int target) { for(int i = 0 ; i<n;i++) { for(int j = 0 ; j < n&& j!=i ; j++) { for(int k = 0 ; k<n&&k!=j&&k!=i;k++) { if(arr[i]+arr[j]+arr[k]==target ) { int large = arr[i]>arr[j]?arr[i]>arr[k]?arr[i]:arr[k]:arr[j]>arr[k]?arr[j]:arr[k]; int small = arr[i]<arr[j]?arr[i]<arr[k]?arr[i]:arr[k]:arr[j]<arr[k]?arr[j]:arr[k]; int mid = target - large -small; System.out.println(small + ", " + mid + " and " + large); } } } } } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n>0) { int [] arr = new int [n]; for(int i =0 ;i<n ;i++) { arr[i] = sc.nextInt(); } int target = sc.nextInt(); Triplets(arr,n,target); } else { System.out.println(“Invalid Input”); } } public static void Triplets(int [] arr , int n , int target) { for(int i = 0 ; i<n;i++) { for(int j = 0 ; j < n&& j!=i ; j++) { for(int k = 0 ; k<n&&k!=j&&k!=i;k++) { if(arr[i]+arr[j]+arr[k]==target ) { int large = arr[i]>arr[j]?arr[i]>arr[k]?arr[i]:arr[k]:arr[j]>arr[k]?arr[j]:arr[k]; int small = arr[i]<arr[j]?arr[i]<arr[k]?arr[i]:arr[k]:arr[j]<arr[k]?arr[j]:arr[k]; int mid = target - large -small; System.out.println(small + ", " + mid + " and " + large); } } } } } }
Yes your output is absolutely correct, Its just the order of printing.
Print with the least number together first
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.