public class ReplaceMinWithLast {
public static void main(String[] args) {
// Example array
int[] arr = {5, 2, 8, 3, 1, 9};
// Find the index of the minimum element
int minIndex = 0;
int minValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < minValue) {
minValue = arr[i];
minIndex = i;
}
}
// Replace the minimum element with the last element of the array
int lastIndex = arr.length - 1;
int lastValue = arr[lastIndex];
arr[minIndex] = lastValue;
arr[lastIndex] = minValue;
// Print the updated array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Dostları ilə paylaş: |