给定一个由N个整数组成的数组arr [] ,任务是在反复选择成对的递增相邻元素(arr [i],arr [i + 1])并删除其中的任何元素后,打印最后剩余的数组元素。一对。如果无法将数组缩小为单个元素,则打印“ Not不可能” 。
例子:
Input: arr[] = {3, 1, 2, 4}
Output: 4
Explanation:
Step 1: Choose a pair (1, 2) and remove 2 from it. Now the array becomes [3, 1, 4].
Step 2: Choose pair (1, 4) and remove 1 from it. Now the array becomes [3, 4].
Step 3: Choose pair (3, 4) and remove 3 from it. Now the array becomes [4].
Hence, the remaining element is 4.
Input: arr[] = {2, 3, 1}
Output: Not Possible
方法:可以通过观察以下事实来解决给定的问题:如果第一个数组元素小于最后一个数组元素,则可以通过执行给定的操作来删除它们之间的所有元素。因此,只需检查arr [0]
下面是上述方法的实现:
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)