📌  相关文章
📜  重复删除绝对差为2或0的对中的最小元素后剩余的数组元素

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

给定一个由n个正整数组成的数组arr [] ,任务是检查是否有可能通过从一对绝对值之差为2或0的对中重复删除最小的元素来将数组的大小减小为1 。如果无法减少,则打印“ -1” 。否则,打印数组中最后剩余的元素。

例子:

方法:请按照以下步骤解决问题:

  • 以升序对给定数组进行排序。
  • 从最小的元素开始遍历数组,并检查是否存在任何一对绝对差不是20的相邻元素。
  • 如果发现是真的,则打印“ -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)