Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation

  • Updating the inventory

    The shop has n items with different prices. When a customer purchases an item, the shop owner wants to know what would be the cheapest and the most expensive item in the inventory after the purchased item is gone. They ask you to help them with implementing the program that would calculate the cheapest and the most expensive items in the shop after removing each element in the inventory. Note that the removals are independent i.e. for each index we consider only removing that index without touching other elements.

    Input

    The first line of the input contains an integer n - the number of items in the shop (1 ≀ n ≀ ).
    The next line contains n integers separated by a space, that represent the prices of those items .

    Output

    The program should print n lines. Line i should contain two numbers - the cheapest and the most expensive item in the inventory after removing item i.

    Examples

    Input
    Output
    5 6 8 1 10 3
    1 10 1 10 3 10 1 8 1 10
    Β 

    Constraints

    Time limit: 2 seconds

    Memory limit: 512 MB

    Output limit: 10 MB

    To check your solution you need to sign in
    Sign in to continue