# Find last two remaining elements after removing median of any 3 consecutive elements repeatedly

• Last Updated : 28 Mar, 2022

Given a sequence A1, A2, A3, … An of distinct integers. The task is to find the last 2 remaining elements after removing the median of any 3 consecutive elements repeatedly from the sequence.
Examples:

Input: A[] = {2, 5, 3}
Output: 2 5
Median of {2, 5, 3} is 3, after removing
it the remaining elements are {2, 5}.
Input: A[] = {38, 9, 102, 10, 96, 7, 46, 28, 88, 13}
Output: 7 102

Approach: For every operation, the median element is the element which is neither the maximum nor the minimum. So, after applying the operation, neither the minimum nor the maximum element is affected. After generalizing this, it can be seen that the final array shall contain only the minimum and the maximum element from the initial array.
Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to find the last``// two remaining elements``void` `lastTwoElement(``int` `A[], ``int` `n)``{` `    ``// Find the minimum and the maximum``    ``// element from the array``    ``int` `minn = *min_element(A, A + n);``    ``int` `maxx = *max_element(A, A + n);` `    ``cout << minn << ``" "` `<< maxx;``}` `// Driver code``int` `main()``{``    ``int` `A[] = { 38, 9, 102, 10, 96,``                ``7, 46, 28, 88, 13 };``    ``int` `n = ``sizeof``(A) / ``sizeof``(``int``);` `    ``lastTwoElement(A, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `    ``static` `int` `min_element(``int` `A[], ``int` `n)``    ``{``        ``int` `min = A[``0``];``        ``for``(``int` `i = ``0``; i < n; i++)``            ``if` `(A[i] < min )``                ``min = A[i];``                ` `        ``return` `min;``        ` `    ``}``    ` `    ``static` `int` `max_element(``int` `A[], ``int` `n)``    ``{``        ``int` `max = A[``0``];``        ``for``(``int` `i = ``0``; i < n; i++)``            ``if` `(A[i] > max )``                ``max = A[i];``                ` `        ``return` `max;``    ``}``    ` `    ``// Function to find the last``    ``// two remaining elements``    ``static` `void` `lastTwoElement(``int` `A[], ``int` `n)``    ``{``    ` `        ``// Find the minimum and the maximum``        ``// element from the array``        ``int` `minn = min_element(A, n);``        ``int` `maxx = max_element(A, n);``    ` `        ``System.out.println(minn + ``" "` `+ maxx);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `A[] = { ``38``, ``9``, ``102``, ``10``, ``96``,``                    ``7``, ``46``, ``28``, ``88``, ``13` `};``                    ` `        ``int` `n = A.length;``    ` `        ``lastTwoElement(A, n);``    ``}``}` `// This code is contributed by AnkitRai01`

## Python

 `# Python3 implementation of the approach` `# Function to find the last``# two remaining elements``def` `lastTwoElement(A, n):` `    ``# Find the minimum and the maximum``    ``# element from the array``    ``minn ``=` `min``(A)``    ``maxx ``=` `max``(A)` `    ``print``(minn, maxx)` `# Driver code``A ``=` `[``38``, ``9``, ``102``, ``10``, ``96``,``7``, ``46``, ``28``, ``88``, ``13``]``n ``=` `len``(A)` `lastTwoElement(A, n)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ``static` `int` `min_element(``int` `[]A, ``int` `n)``    ``{``        ``int` `min = A;``        ``for``(``int` `i = 0; i < n; i++)``            ``if` `(A[i] < min )``                ``min = A[i];``                ` `        ``return` `min;``    ``}``    ` `    ``static` `int` `max_element(``int` `[]A, ``int` `n)``    ``{``        ``int` `max = A;``        ``for``(``int` `i = 0; i < n; i++)``            ``if` `(A[i] > max )``                ``max = A[i];``                ` `        ``return` `max;``    ``}``    ` `    ``// Function to find the last``    ``// two remaining elements``    ``static` `void` `lastTwoElement(``int` `[]A, ``int` `n)``    ``{``    ` `        ``// Find the minimum and the maximum``        ``// element from the array``        ``int` `minn = min_element(A, n);``        ``int` `maxx = max_element(A, n);``    ` `        ``Console.WriteLine(minn + ``" "` `+ maxx);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `[]A = { 38, 9, 102, 10, 96,``                    ``7, 46, 28, 88, 13 };``                    ` `        ``int` `n = A.Length;``    ` `        ``lastTwoElement(A, n);``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`7 102`

Time Complexity: O(n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up