📜  将数组拆分为两个子序列,这些子序列的总和等于 X

📅  最后修改于: 2021-09-06 05:49:32             🧑  作者: Mango

给定一个由N 个整数和一个整数X组成的数组arr[] ,任务是将数组拆分为两个子序列,使得两个数组中总和等于X的对的数量最小。

例子:

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

  • 创建两个数组来存储两个拆分的子序列。
  • 遍历数组并执行以下操作:
    • 如果 arr[i] * 2 < X:将其插入到第一个数组中。
    • 由于第一个数组当前包含所有小于X / 2 的数字,因此当前数组中没有任何一对的总和等于X。
    • 如果 arr[i] * 2 > X:将其插入到第二个数组中。
    • 由于第二个数组当前包含所有大于X / 2 的数字,因此当前数组中没有任何一对的总和等于X。
    • 如果 arr[i] * 2 < X:分别将元素交替插入到第一个和第二个数组中以获得最小对。
  • 最后,在完全遍历数组后,打印两个数组。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to split the array
// into two subsequences
void solve(int arr[], int N, int X)
{
    // Stores the two subsequences
    vector A, B;
 
    // Flag to set/reset to split
    // arrays elements alternately
    // into two arrays
    int c = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // If 2 * arr[i] is less than X
        if ((2 * arr[i]) < X) {
 
            // Push element into
            // the first array
            A.push_back(arr[i]);
        }
 
        // If 2 * arr[i] is greater than X
        else if ((2 * arr[i]) > X) {
 
            // Push element into
            // the second array
            B.push_back(arr[i]);
        }
 
        // If 2 * arr[i] is equal to X
        else {
 
            // Alternatively place the
            // elements into the two arrays
            if (c % 2 == 0) {
 
                A.push_back(arr[i]);
            }
            else {
 
                B.push_back(arr[i]);
            }
            c++;
        }
    }
 
    // Print both the arrays
    cout << "The First Array is - ";
    for (int i = 0; i < A.size(); i++) {
 
        cout << A[i] << " ";
    }
    cout << endl;
    cout << "The Second Array is - ";
    for (int i = 0; i < B.size(); i++) {
 
        cout << B[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 4, 3,
                  6, 2, 4, 3 };
    int X = 7;
 
    // Size of the array
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function Call
    solve(arr, N, X);
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to split the array
// into two subsequences
static void solve(int arr[],
                  int N, int X)
{
  // Stores the two subsequences
  Vector A =
         new Vector(),
   B = new Vector();
 
  // Flag to set/reset to split
  // arrays elements alternately
  // into two arrays
  int c = 0;
 
  // Traverse the given array
  for (int i = 0; i < N; i++)
  {
    // If 2 * arr[i] is
    // less than X
    if ((2 * arr[i]) < X)
    {
      // Push element into
      // the first array
      A.add(arr[i]);
    }
 
    // If 2 * arr[i] is greater
    // than X
    else if ((2 * arr[i]) > X)
    {
      // Push element into
      // the second array
      B.add(arr[i]);
    }
 
    // If 2 * arr[i] is
    // equal to X
    else
    {
      // Alternatively place the
      // elements into the two arrays
      if (c % 2 == 0)
      {
        A.add(arr[i]);
      }
      else
      {
        B.add(arr[i]);
      }
      c++;
    }
  }
 
  // Print both the arrays
  System.out.print("The First Array is - ");
  for (int i = 0; i < A.size(); i++)
  {
    System.out.print(A.get(i) + " ");
  }
   
  System.out.println();
   
  System.out.print("The Second Array is - "); 
  for (int i = 0; i < B.size(); i++)
  {
    System.out.print(B.get(i) + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {1, 5, 4, 3,
               6, 2, 4, 3};
  int X = 7;
 
  // Size of the array
  int N = arr.length;
 
  // Function Call
  solve(arr, N, X);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for above approach
 
# Function to split the array
# into two subsequences
def solve(arr, N, X):
     
    # Stores the two subsequences
    A = []
    B = []
     
    # Flag to set/reset to split
    # arrays elements alternately
    # into two arrays
    c = 0
 
    # Traverse the given array
    for i in range(N):
 
        # If 2 * arr[i] is less than X
        if ((2 * arr[i]) < X):
 
            # Push element into
            # the first array
            A.append(arr[i])
 
        # If 2 * arr[i] is greater than X
        elif ((2 * arr[i]) > X):
 
            # Push element into
            # the second array
            B.append(arr[i])
 
        # If 2 * arr[i] is equal to X
        else:
             
            # Alternatively place the
            # elements into the two arrays
            if (c % 2 == 0):
                A.append(arr[i])
            else:
                B.append(arr[i])
                 
            c += 1
 
    # Print both the arrays
    print("The First Array is - ", end = " ")
    for i in range(len(A)):
        print(A[i], end = " ")
 
    print()
     
    print("The Second Array is - ", end = " ")
    for i in range(len(B)):
        print(B[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 5, 4, 3, 6, 2, 4, 3 ]
    X = 7
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    solve(arr, N, X)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to split the array
// into two subsequences
static void solve(int []arr,
                  int N, int X)
{
  // Stores the two subsequences
  List A =
       new List(),
   B = new List();
 
  // Flag to set/reset to
  // split arrays elements
  // alternately into two
  // arrays
  int c = 0;
 
  // Traverse the given array
  for (int i = 0; i < N; i++)
  {
    // If 2 * arr[i] is
    // less than X
    if ((2 * arr[i]) < X)
    {
      // Push element into
      // the first array
      A.Add(arr[i]);
    }
 
    // If 2 * arr[i] is greater
    // than X
    else if ((2 * arr[i]) > X)
    {
      // Push element into
      // the second array
      B.Add(arr[i]);
    }
 
    // If 2 * arr[i] is
    // equal to X
    else
    {
      // Alternatively place the
      // elements into the two arrays
      if (c % 2 == 0)
      {
        A.Add(arr[i]);
      }
      else
      {
        B.Add(arr[i]);
      }
      c++;
    }
  }
 
  // Print both the arrays
  Console.Write("The First Array is - ");
  for (int i = 0; i < A.Count; i++)
  {
    Console.Write(A[i] + " ");
  }
   
  Console.WriteLine();
   
  Console.Write("The Second Array is - "); 
  for (int i = 0; i < B.Count; i++)
  {
    Console.Write(B[i] + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {1, 5, 4, 3,
               6, 2, 4, 3};
  int X = 7;
 
  // Size of the array
  int N = arr.Length;
 
  // Function Call
  solve(arr, N, X);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出
The First Array is - 1 3 2 3 
The Second Array is - 5 4 6 4 

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live