📌  相关文章
📜  重复删除任意3个连续元素的中位数后,查找剩余的最后两个元素

📅  最后修改于: 2021-04-27 21:21:21             🧑  作者: Mango

给定一个不同整数的序列A 1 ,A 2 ,A 3 ,…A n 。任务是在从序列中重复删除任意3个连续元素的中值后,找到剩余的最后2个元素。

例子:

方法:对于每个操作,中位元素都是既不是最大值也不是最小值的元素。因此,应用该操作后,最小元素或最大元素都不会受到影响。将其概括后,可以看出最终数组应仅包含初始数组中的最小和最大元素。

下面是上述方法的实现:

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[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


输出:
7 102

时间复杂度: O(n)