📌  相关文章
📜  检查给定的数组是否可以分成 N/2 对,总和相等

📅  最后修改于: 2022-05-13 01:56:06.688000             🧑  作者: Mango

检查给定的数组是否可以分成 N/2 对,总和相等

给定一个长度为N的整数数组A[] ,(其中 N 是偶数),任务是检查A[]是否可以分成N/2对,它们的总和相等。

例子:

方法:解决问题的想法是使用两个指针方法,遵循以下观察。

根据上述观察,按照以下步骤解决问题:

  • 对给定的数组进行排序。
  • 初始化一个变量(比如target ),该变量等于排序数组的第一个和最后一个元素的总和。
  • 初始化指向第一个和最后一个元素的两个指针。
    • 同时递增和递减指针,并检查指针处的元素之和是否等于目标。
    • 如果是,继续迭代。
    • 否则返回假。
  • 迭代完成后返回true。

下面是上述方法的实现。

C++
// C++ code for the above approach.
 
#include 
using namespace std;
 
// Function to check if it is possible
// to divide the given array into
// N/2 pairs having equal sum
bool isPossible(int N, int A[])
{
    // Sorting the given array
    sort(A, A + N);
 
    // Initializing target as the sum of
    // minimum and maximum element
    int target = A[0] + A[N - 1];
 
    // Initializing two pointers
    int i = 0, j = N - 1;
 
    while (i < j) {
 
        // If sum of elements at i and j
        // is equal to target then,
        // increment and decrement i and j
        // respectively
        if (A[i] + A[j] == target) {
            i++;
            j--;
        }
 
        // Else return false
        else {
            return false;
        }
    }
 
    // After whole array is traversed,
    // which means N/2 pairs have sum
    // equal to target, hence return true
    return true;
}
 
// Driver Code
int main()
{
    int N = 6;
    int A[] = { 4, 5, 3, 1, 2, 6 };
 
    // Function call
    bool answer = isPossible(N, A);
    if (answer)
        cout << "True";
    else
        cout << "False";
    return 0;
}


Java
// JAVA code for the above approach.
import java.util.*;
class GFG
{
   
  // Function to check if it is possible
  // to divide the given array into
  // N/2 pairs having equal sum
  public static boolean isPossible(int N, int A[])
  {
    // Sorting the given array
    Arrays.sort(A);
 
    // Initializing target as the sum of
    // minimum and maximum element
    int target = A[0] + A[N - 1];
 
    // Initializing two pointers
    int i = 0, j = N - 1;
 
    while (i < j) {
 
      // If sum of elements at i and j
      // is equal to target then,
      // increment and decrement i and j
      // respectively
      if (A[i] + A[j] == target) {
        i++;
        j--;
      }
 
      // Else return false
      else {
        return false;
      }
    }
 
    // After whole array is traversed,
    // which means N/2 pairs have sum
    // equal to target, hence return true
    return true;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 6;
    int A[] = { 4, 5, 3, 1, 2, 6 };
 
    // Function call
    boolean answer = isPossible(N, A);
    if (answer)
      System.out.print("True");
    else
      System.out.print("False");
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python3 code for the above approach.
 
# Function to check if it is possible
# to divide the given array into
# N/2 pairs having equal sum
def isPossible(N, A):
 
    # Sorting the given array
    A.sort()
 
    # Initializing target as the sum of
    # minimum and maximum element
    target = A[0] + A[N - 1]
 
    # Initializing two pointers
    i = 0
    j = N - 1
 
    while (i < j):
 
        # If sum of elements at i and j
        # is equal to target then,
        # increment and decrement i and j
        # respectively
        if (A[i] + A[j] == target):
            i += 1
            j -= 1
 
        # Else return false
        else:
            return False
 
    # After whole array is traversed,
    # which means N/2 pairs have sum
    # equal to target, hence return true
    return True
 
# Driver Code
N = 6
A = [ 4, 5, 3, 1, 2, 6 ]
 
# Function call
answer = isPossible(N, A)
if (answer):
   print("True")
else:
   print("False")
  
# This code is contributed by shinjanpatra


C#
// C# code for the above approach.
using System;
 
public class GFG{
 
  // Function to check if it is possible
  // to divide the given array into
  // N/2 pairs having equal sum
  static bool isPossible(int N, int[] A){
    // Sorting the given array
    Array.Sort(A);
 
    // Initializing target as the sum of
    // minimum and maximum element
    int target = A[0] + A[N - 1];
 
    // Initializing two pointers
    int i = 0, j = N - 1;
 
    while (i < j) {
 
      // If sum of elements at i and j
      // is equal to target then,
      // increment and decrement i and j
      // respectively
      if (A[i] + A[j] == target) {
        i++;
        j--;
      }
 
      // Else return false
      else {
        return false;
      }
    }
 
    // After whole array is traversed,
    // which means N/2 pairs have sum
    // equal to target, hence return true
    return true;
  }
 
  // Driver Code
  static public void Main (){
 
    int N = 6;
    int[] A = { 4, 5, 3, 1, 2, 6 };
 
    // Function call
    bool answer = isPossible(N, A);
    if (answer == true)
      Console.Write("True");
    else
      Console.Write("False");
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
1

时间复杂度: O(N * logN)
辅助空间: O(1)