📌  相关文章
📜  从成对增加的相邻数组元素中重复删除一个元素后,找到最后剩余的元素

📅  最后修改于: 2021-05-17 19:49:43             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是在反复选择成对的递增相邻元素(arr [i],arr [i + 1])并删除其中的任何元素后,打印最后剩余的数组元素。一对。如果无法将数组缩小为单个元素,则打印“ Not不可能”

例子:

方法:可以通过观察以下事实来解决给定的问题:如果第一个数组元素小于最后一个数组元素,则可以通过执行给定的操作来删除它们之间的所有元素。因此,只需检查arr [0] 是否正确。如果发现为真,则打印第一个或最后一个数组元素。否则,打印-1

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the last remaining
// array element after after performing
// given operations
void canArrayBeReduced(int arr[], int N)
{
    // If size of the array is 1
    if (N == 1) {
        cout << arr[0];
        return;
    }
 
    // Check for the condition
    if (arr[0] < arr[N - 1]) {
        cout << arr[N - 1];
    }
 
    // If condition is not satisfied
    else
        cout << "Not Possible";
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 5, 2, 4, 1, 3, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    canArrayBeReduced(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to print the last remaining
  // array element after after performing
  // given operations
  static void canArrayBeReduced(int[] arr, int N)
  {
 
    // If size of the array is 1
    if (N == 1)
    {
      System.out.print(arr[0]);
      return;
    }
 
    // Check for the condition
    if (arr[0] < arr[N - 1])
    {
      System.out.print(arr[N - 1]);
    }
 
    // If condition is not satisfied
    else
      System.out.print("Not Possible");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 6, 5, 2, 4, 1, 3, 7 };
    int N = arr.length;
 
    // Function Call
    canArrayBeReduced(arr, N);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python 3 program for the above approach
 
# Function to print the last remaining
# array element after after performing
# given operations
def canArrayBeReduced(arr,  N):
 
    # If size of the array is 1
    if (N == 1):
        print(arr[0])
        return
 
    # Check for the condition
    if (arr[0] < arr[N - 1]):
        print(arr[N - 1])
 
    # If condition is not satisfied
    else:
        print("Not Possible")
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [6, 5, 2, 4, 1, 3, 7]
    N = len(arr)
 
    # Function Call
    canArrayBeReduced(arr, N)
 
    # This code is contributed by chitranayal.


C#
// C# program for the above approach
using System;
 
public class GFG {
 
  // Function to print the last remaining
  // array element after after performing
  // given operations
  static void canArrayBeReduced(int[] arr, int N)
  {
 
    // If size of the array is 1
    if (N == 1)
    {
      Console.Write(arr[0]);
      return;
    }
 
    // Check for the condition
    if (arr[0] < arr[N - 1])
    {
      Console.Write(arr[N - 1]);
    }
 
    // If condition is not satisfied
    else
      Console.Write("Not Possible");
  }
 
  // Driver Code
  static public void Main()
  {
    int[] arr = { 6, 5, 2, 4, 1, 3, 7 };
    int N = arr.Length;
 
    // Function Call
    canArrayBeReduced(arr, N);
  }
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出:
7

时间复杂度: O(1)
辅助空间: O(1)