给定一个由N 个正整数组成的数组arr[] ,任务是检查是否有可能通过从一对绝对差为 2 或 0 的对中重复删除最小元素来将数组的大小减小到1 。如果无法减少,则打印“-1” 。否则,打印数组中的最后一个剩余元素。
例子:
Input: arr[] = {2, 4, 6, 8, 0, 8}
Output: 8
Explanation:
arr[] = {2, 4, 6, 8, 0, 8}, Remove 0 from the pair (2, 0).
arr[] = {2, 4, 6, 8, 8}. Remove 2 from the pair (2, 4).
arr[] = {4, 6, 8, 8}, Remove 4 from the pair (4, 6).
arr[] = {6, 8, 8}. Remove 6 from the pair (6, 8).
arr[] = {8, 8}. Remove 8.
arr[] = {8}
Input: arr[] = {1, 7, 3, 3}
Output: -1
Explanation:
arr[] = {1, 7, 3, 3}. Remove 1 from the pair (1, 3).
arr[] = {7, 3, 3}. Remove 3 from the pair (3, 3).
arr[] = {7, 3}. No more removals possible.
处理方法:按照以下步骤解决问题:
- 按升序对给定数组进行排序。
- 从最小元素开始遍历数组,并检查是否存在绝对差值不为2或0 的任何相邻元素对。
- 如果发现为真,则打印“-1” 。否则,打印数组中存在的最大元素,因为这将是执行给定操作后唯一剩余的数组元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the last remaining
// array element after repeatedly removing
// the smallest from pairs having absolute
// difference 2 or 0
void findLastElement(int arr[], int N)
{
// Sort the given array in
// ascending order
sort(arr, arr + N);
int i = 0;
// Traverse the array
for (i = 1; i < N; i++) {
// If difference between
// adjacent elements is
// not equal to 0 or 2
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2) {
cout << "-1" << endl;
return;
}
}
// If operations can be performed
cout << arr[N - 1] << endl;
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 6, 8, 0, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
findLastElement(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the last remaining
// array element after repeatedly removing
// the smallest from pairs having absolute
// difference 2 or 0
static void findLastElement(int arr[], int N)
{
// Sort the given array in
// ascending order
Arrays.sort(arr);
int i = 0;
// Traverse the array
for (i = 1; i < N; i++) {
// If difference between
// adjacent elements is
// not equal to 0 or 2
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2)
{
System.out.println("-1");
return;
}
}
// If operations can be performed
System.out.println( arr[N - 1]);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 4, 6, 8, 0, 8 };
int N = arr.length;
findLastElement(arr, N);
}
}
// This code is contributed by code_hunt.
Python3
# Python program for the above approach
# Function to find the last remaining
# array element after repeatedly removing
# the smallest from pairs having absolute
# difference 2 or 0
def findLastElement(arr, N):
# Sort the given array in
# ascending order
arr.sort();
i = 0;
# Traverse the array
for i in range(1, N):
# If difference between
# adjacent elements is
# not equal to 0 or 2
if (arr[i] - arr[i - 1] != 0\
and arr[i] - arr[i - 1] != 2):
print("-1");
return;
# If operations can be performed
print(arr[N - 1]);
# Driver Code
if __name__ == '__main__':
arr = [2, 4, 6, 8, 0, 8];
N = len(arr);
findLastElement(arr, N);
# This code is contributed by 29AjayKumar.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the last remaining
// array element after repeatedly removing
// the smallest from pairs having absolute
// difference 2 or 0
static void findLastElement(int []arr, int N)
{
// Sort the given array in
// ascending order
Array.Sort(arr);
int i = 0;
// Traverse the array
for (i = 1; i < N; i++)
{
// If difference between
// adjacent elements is
// not equal to 0 or 2
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2)
{
Console.WriteLine("-1");
return;
}
}
// If operations can be performed
Console.WriteLine(arr[N - 1]);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 4, 6, 8, 0, 8 };
int N = arr.Length;
findLastElement(arr, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
8
时间复杂度: O(N*log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live