package com.coderevisited.coding;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class ReplaceWithNextGreatest
{
    public static void main(String[] args)
    {
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        int[] array = new int[]{16, 6, 9, 10, 11, 17, 4, 3, 9, 8, 5, 2, 14};
        replaceWithNextGreatest(array);
        printArray(array, pw);

        array = new int[]{-2, -7, -8};
        replaceWithNextGreatest(array);
        printArray(array, pw);

        pw.close();
    }

    private static void replaceWithNextGreatest(int[] array)
    {
        int max = Integer.MIN_VALUE;
        for (int i = array.length - 1; i >= 0; i--) {
            int current = array[i];
            array[i] = max;
            max = max > current ? max : current;
        }
        array[array.length - 1] = -1;
    }

    private static void printArray(int[] array, PrintWriter pw)
    {
        for (int i : array) {
            pw.print(i + " ");
        }
        pw.println();

    }
}

@@ -309,17 +309,16 @@
 out)));%0A
-%0A
         
@@ -449,33 +449,32 @@
 ray(array, pw);%0A
-%0A
         array = 
@@ -567,17 +567,16 @@
 y, pw);%0A
-%0A
         
@@ -1083,17 +1083,16 @@
 ntln();%0A
-%0A
     %7D%0A%7D%0A

Replace With next greatest