Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
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
#include <bits/stdc++.h>
using namespace std;
void lastTwoElement( int A[], int n)
{
int minn = *min_element(A, A + n);
int maxx = *max_element(A, A + n);
cout << minn << " " << maxx;
}
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
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;
}
static void lastTwoElement( int A[], int n)
{
int minn = min_element(A, n);
int maxx = max_element(A, n);
System.out.println(minn + " " + maxx);
}
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);
}
}
|
Python
def lastTwoElement(A, n):
minn = min (A)
maxx = max (A)
print (minn, maxx)
A = [ 38 , 9 , 102 , 10 , 96 , 7 , 46 , 28 , 88 , 13 ]
n = len (A)
lastTwoElement(A, n)
|
C#
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;
}
static void lastTwoElement( int []A, int n)
{
int minn = min_element(A, n);
int maxx = max_element(A, n);
Console.WriteLine(minn + " " + maxx);
}
public static void Main ()
{
int []A = { 38, 9, 102, 10, 96,
7, 46, 28, 88, 13 };
int n = A.Length;
lastTwoElement(A, n);
}
}
|
Javascript
<script>
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 lastTwoElement(A,n)
{
let minn = min_element(A, n);
let maxx = max_element(A, n);
document.write(minn + " " + maxx);
}
let A = [ 38, 9, 102, 10, 96,
7, 46, 28, 88, 13 ];
let n = A.length;
lastTwoElement(A, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
28 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...