import java.io.*;

// Array Rank Programming Problem
// Input: An array size, followed by the specified number of integers
// Output: The corresponding rank array.

// The rank of an element is the number of smaller elements in the
// array plus the number of equal elements that appear to its left.
// The rank array is an array of element ranks.  For example, if the
// input array is [4, 3, 9, 3, 7], the corresponding rank array is
// [2, 0, 4, 1, 3].

public class Rank
{

  public static void main( String[] args ) throws Exception
  {
    // get input from user:
    BufferedReader br = new BufferedReader(
                        new InputStreamReader( System.in ) );
    final int ARRAY_SIZE = promptSize( br );
    int[] theInts = new int[ARRAY_SIZE];
    int[] ranks = new int[ARRAY_SIZE];

    System.out.println( "Enter the ints, each on a separate line:" );
    for( int i = 0; i < ARRAY_SIZE; i++ )
    {
      theInts[i] = Integer.parseInt( br.readLine() );
      ranks[i] = 0; // simultaneously set all ranks to 0 as we get input
    }               // (saves having another for loop)

    // compare all pairs of elements, increment the proper ranks
    for( int i = 1; i < ARRAY_SIZE; i++ )
      for( int j = 0; j < i; j++ )
        if( theInts[j] <= theInts[i] )
          ranks[i]++;
        else
          ranks[j]++;

    // output the rank array
    System.out.println( "The input array was: " );
    for( int i = 0; i < ARRAY_SIZE; i++ )
      System.out.print( theInts[i] + " " );
    System.out.println();

    System.out.println( "The corresponding rank array is: " );
    for( int i = 0; i < ARRAY_SIZE; i++ )
      System.out.print( ranks[i] + " " );
    System.out.println();

  }


  private static int promptSize( BufferedReader br ) throws Exception
  {
    int s = 0;
    System.out.print( "Enter desired size of array : " );
    s = Integer.parseInt( br.readLine() );
    while( s < 1 )
    {
      System.out.print( "Please enter a size of at least 1: " );
      s = Integer.parseInt( br.readLine() );
    }
    return s;
  }

}
