Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
Created October 17, 2013 00:55
Show Gist options
  • Save ravichandrae/7017591 to your computer and use it in GitHub Desktop.
Save ravichandrae/7017591 to your computer and use it in GitHub Desktop.
This program prints the array of biggest element to the right of given array
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/17/13
* Time: 6:01 AM
* To change this template use File | Settings | File Templates.
*/
public class BiggestElementsRight {
public static void main(String [] args)
{
int[] array = { 8, 7, 9, 4, 5, 6, 3, 1, 2};
int[] bigRight = getBiggestRight(array);
for( int i = 0; i < bigRight.length; i++ )
{
System.out.print( bigRight[i] + " ");
}
}
public static int[] getBiggestRight(int[] array)
{
int i;
int len = array.length;
int[] bigArray = new int[len];
//Right most element does not have any biggest elements to its right; so put -1
bigArray[ len - 1] = -1;
//start with last but one element assuming last element as the biggest
int biggestNum = array[ len - 1 ];
for( i = array.length - 2; i >= 0; i-- )
{
if( array[i+1] > biggestNum )
biggestNum = array[i+1];
bigArray[i] = biggestNum;
}
return bigArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment