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

📅  最后修改于: 2021-10-26 02:39:05             🧑  作者: Mango

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

例子:

方法:通过观察如果第一个数组元素小于最后一个数组元素,则可以通过执行给定操作删除它们之间的所有元素,从而解决给定的问题。因此,只需检查arr[0] < arr[N – 1]是否。如果发现为真,则打印第一个或最后一个数组元素。否则,打印-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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程