Hi,
The code which I’ve written works for the example test case in the question. However, only one test case has been passed when I’ve tried to submit the code. Can you please check and let me know what’s wrong with the code?
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int j = 0; j < n; j++) {
int arrVal = sc.nextInt();
arr[j] = arrVal;
}
bubbleSort(arr);
int check = sc.nextInt();
for (int i = 0; i < n-2; i++) {
int left = i+1;
int right = n-1;
while(left < right) {
int condition = arr[left] + arr [right] + arr[i];
if(condition == check) {
System.out.println(arr[i] + ", "+ arr[left] + " and " + arr [right] );
left ++;
right --;
} else {
right --;
}
}
}
}
public static void bubbleSort (int arr []) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}