Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind last two remaining elements after removing median of any 3 consecutive...

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




// 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)
 Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments