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

Given a sequence **A _{1}, A_{2}, A_{3}, … A_{n}** 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 <bits/stdc++.h>` `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[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);` ` ` ` ` `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

`<script>` `// Java Script implementation of the approach` `function` `min_element(A,n)` ` ` `{` ` ` `let min = A[0];` ` ` `for` `(let i = 0; i < n; i++)` ` ` `if` `(A[i] < min )` ` ` `min = A[i];` ` ` ` ` `return` `min;` ` ` ` ` `}` ` ` ` ` `function` `max_element(A,n)` ` ` `{` ` ` `let max = A[0];` ` ` `for` `(let i = 0; i < n; i++)` ` ` `if` `(A[i] > max )` ` ` `max = A[i];` ` ` ` ` `return` `max;` ` ` `}` ` ` ` ` `// Function to find the last` ` ` `// two remaining elements` ` ` `function` `lastTwoElement(A,n)` ` ` `{` ` ` ` ` `// Find the minimum and the maximum` ` ` `// element from the array` ` ` `let minn = min_element(A, n);` ` ` `let maxx = max_element(A, n);` ` ` ` ` `document.write(minn + ` `" "` `+ maxx);` ` ` `}` ` ` ` ` `// Driver code` ` ` ` ` `let A = [ 38, 9, 102, 10, 96,` ` ` `7, 46, 28, 88, 13 ];` ` ` ` ` `let n = A.length;` ` ` ` ` `lastTwoElement(A, n);` ` ` `// This code is contributed by sravan kumar Gottumukkala` `</script>` |

**Output:**

7 102

**Time Complexity:** O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.