📌  相关文章
📜  将数组拆分为两个子序列,其子对的最小数量和为X

📅  最后修改于: 2021-05-04 22:12:46             🧑  作者: Mango

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

例子:

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

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

下面是上述方法的实现:

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


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

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