Given an array of integers, write a program to replace its elements with the maximum element on the right side from their respective positions.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Arrays; public class JavaExample { public static void replace(int [] arrA){ int size = arrA.length; if(size==0) return; System.out.println("Original Array: " + Arrays.toString(arrA)); //initialize last element in array as maximum element seen so far int max_so_far = arrA[size-1]; //traverse the array from right to left //if visited element is smaller than max_so_far, replace element by max_so_far //if visited element is equal to max_so_far, replace element by 0 //if visited element is greater than max_so_far, do max_so_far = element and element = 0 for (int i = size-1; i >=0 ; i--) { if(max_so_far==arrA[i]) arrA[i]=0; else if(max_so_far>arrA[i]) arrA[i]=max_so_far; else if(max_so_far<arrA[i]){ max_so_far = arrA[i]; arrA[i] = 0; } } System.out.println("Modified Array: " + Arrays.toString(arrA)); } public static void main(String[] args) { int [] arrA = {2, 5, 1, 6, 3, 4}; replace(arrA); } } |
Output:
1 2 3 4 |
Original Array: [2, 5, 1, 6, 3, 4] Modified Array: [6, 6, 6, 0, 4, 0] |