Next Largest no in the Array

package com.sudharsan.examples;

import java.util.Arrays;

public class NextLargestNoInArray {

static public void main(final String[] args) {
int a[] = {17, 4, 16, 5, 4, 2, 3};
System.out.println(Arrays.toString(a));

int size = a.length -1;
int max = a[size];
a[size] = -1;
for (int i = size - 2 ; i >= 0 ; i--) {
int temp = a[i];
a[i] = max;
if (temp > max) {
max = temp;
}
}
System.out.print("\nAfter change : ");
System.out.println(Arrays.toString(a));
}

}

OUTPUT:

Before Change : [17, 4, 16, 5, 4, 2, 3]

After change : [16, 16, 5, 4, 3, 2, -1]

Comments

Popular posts from this blog

Tiny URL Converter

Blocking Queue