This is the Java Program to Replace the Element with its Next Greatest Element, Rightmost Element will be Replaced by 0.
Given an array of integers, replace each element of the array with the greatest element present on the right side of the element, in the array. The rightmost element or the last element should be replaced by 0.
Example:
Array = [-1, -3, 3, 2, 8 ,6]
Output: [8, 8, 8, 8, 6, 0].
Traverse the array from right to left and keep track of the maximum element found. Replace each element with the current maximum element, until the maximum changes. At the point of new maximum, swap the current element with the current maximum and continue. Finally, update the rightmost element to zero.
Here is the source code of the Java Program to Replace the Element with its Next Greatest Element, Rightmost Element will be Replaced by 0. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Replace the Element with its Next Greatest Element,
//Rightmost Element will be Replaced by 0
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ReplaceNextGreatest {
// Function to replace all the array elements
// with the next greatest element
static void nextGreatest(int[] array){
int max=array[array.length-1];
int i,temp;
for(i= array.length-2; i>=0; i--){
temp = array[i];
array[i] = max;
if(temp > max)
max= temp;
}
array[array.length-1] = 0;
}
// Function to read input and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
}
System.out.println("The initial array is");
for(i = 0;i < array.length; i++){
System.out.print(array[i] + " ");
}
nextGreatest(array);
System.out.println("\nThe final array is");
for(i = 0;i < array.length; i++){
System.out.print(array[i] + " ");
}
}
}
1. In function nextGreatest(), the variable max is initialized to the last value in the array (int max=array[array.length-1]).
2. The loop for(i=array.length-1; i>=0; i–) traverses the array from right to left.
3. The element array[i] is stored in a variable temp and its value is changed to max.
4. The variable max is updated using the condition if(temp > max).
5. Finally, the last value in the array is set to zero.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case): Enter the size of the array 6 Enter array elements -1 -3 3 2 8 6 The initial array is -1 -3 3 2 8 6 The final array is 8 8 8 8 6 0 Case 2 (Simple Test Case - another example): Enter the size of the array 5 Enter array elements 5 4 3 2 1 The initial array is 5 4 3 2 1 The final array is 4 3 2 1 0
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Apply for Java Internship