package com.coderevisited.coding;

public class ReplaceWithNextGreatest
{
    public static void main(String[] args)
    {
        int[] array = new int[]{16, 6, 9, 10, 11, 17, 4, 3, 9, 8, 5, 2, 14};
        replaceWithNextGreatest(array);

        for (int i : array) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

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

@@ -28,16 +28,111 @@
 oding;%0A%0A
+import java.io.BufferedWriter;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.PrintWriter;%0A%0A
 public c
@@ -203,32 +203,131 @@
 g%5B%5D args)%0A    %7B%0A
+        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));%0A%0A
         int%5B%5D ar
@@ -427,25 +427,24 @@
 array);%0A
-%0A
         
 for (int
@@ -439,105 +439,160 @@
     
-for (int i : array) %7B%0A            System.out.print(i + %22 %22);%0A        %7D%0A        System.out.println
+printArray(array, pw);%0A%0A        array = new int%5B%5D%7B-2, -7, -8%7D;%0A        replaceWithNextGreatest(array);%0A        printArray(array, pw);%0A%0A%0A        pw.close
 ();%0A
@@ -687,10 +687,25 @@
 x = 
--1
+Integer.MIN_VALUE
 ;%0A  
@@ -879,16 +879,225 @@
       %7D%0A
+        array%5Barray.length - 1%5D = -1;%0A    %7D%0A%0A    private static void printArray(int%5B%5D array, PrintWriter pw)%0A    %7B%0A        for (int i : array) %7B%0A            pw.print(i + %22 %22);%0A        %7D%0A        pw.println();%0A%0A
     %7D%0A%7D%0A

Replace With next greatest