给定一个由N 个整数组成的数组arr[] ,任务是在重复选择递增的相邻元素对(arr[i], arr[i + 1])并删除数组中的任何元素后打印最后剩余的数组元素一对。如果无法将数组缩减为单个元素,则打印“不可能” 。
例子:
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] < 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 现场工作专业课程和学生竞争性编程现场课程。