📌  相关文章
📜  将数组分为两个不包含任何对和K的数组

📅  最后修改于: 2021-04-26 07:48:38             🧑  作者: Mango

给定由N个非负唯一整数和整数K组成的数组arr [] ,任务是将数组分配为两个数组,以使两个数组都不包含总和为K的对。

例子:

方法:想法是遍历数组并将大于K / 2的数组元素放在一个数组中,其余元素放在另一个数组中。请按照以下步骤解决问题:

  • 初始化两个单独的载体的第一第二存储两个分布式阵列。
  • 由于所有数组元素都是不同的,因此大于K / 2的元素可以存储在一个数组中,其余元素可以存储在另一个数组中。
  • 遍历给定的数组,对于每个元素,检查arr [i]是否大于K / 2 。如果发现是真的,则将该元素插入向量second 。否则,请先将其插入vector中。
  • 完全遍历数组后,在两个向量中打印元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to split the given
// array into two separate arrays
// satisfying given condition
void splitArray(int a[], int n,
                int k)
{
    // Stores resultant arrays
    vector first, second;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If a[i] is smaller than
        // or equal to k/2
        if (a[i] <= k / 2)
            first.push_back(a[i]);
        else
            second.push_back(a[i]);
    }
 
    // Print first array
    for (int i = 0; i < first.size();
         i++) {
        cout << first[i] << " ";
    }
 
    // Print second array
    cout << "\n";
    for (int i = 0; i < second.size();
         i++) {
        cout << second[i] << " ";
    }
}
 
// Driver Code
int main()
{
 
    // Given K
    int k = 5;
 
    // Given array
    int a[] = { 0, 1, 3, 2, 4, 5,
                6, 7, 8, 9, 10 };
 
    // Given size
    int n = sizeof(a)
            / sizeof(int);
 
    splitArray(a, n, k);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
  
// Function to split the given
// array into two separate arrays
// satisfying given condition
static void splitArray(int a[], int n,
                       int k)
{
     
    // Stores resultant arrays
    Vector first = new Vector<>();
    Vector second = new Vector<>();
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If a[i] is smaller than
        // or equal to k/2
        if (a[i] <= k / 2)
            first.add(a[i]);
        else
            second.add(a[i]);
    }
  
    // Print first array
    for(int i = 0; i < first.size(); i++)
    {
        System.out.print(first.get(i) + " ");
    }
  
    // Print second array
    System.out.println();
    for(int i = 0; i < second.size(); i++)
    {
        System.out.print(second.get(i) + " ");
    }
}
  
// Driver Code
public static void main(String[] args)
{
     
    // Given K
    int k = 5;
  
    // Given array
    int a[] = { 0, 1, 3, 2, 4, 5,
                6, 7, 8, 9, 10 };
  
    int n = a.length;
     
    splitArray(a, n, k);
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program for the above approach
 
# Function to split the given
# array into two separate arrays
# satisfying given condition
def splitArray(a, n, k):
     
    # Stores resultant arrays
    first = []
    second = []
 
    # Traverse the array
    for i in range(n):
         
        # If a[i] is smaller than
        # or equal to k/2
        if (a[i] <= k // 2):
            first.append(a[i])
        else:
            second.append(a[i])
 
    # Print first array
    for i in range(len(first)):
        print(first[i], end = " ")
 
    # Print second array
    print("\n", end = "")
    for i in range(len(second)):
        print(second[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given K
    k = 5
     
    # Given array
    a =  [ 0, 1, 3, 2, 4, 5,
           6, 7, 8, 9, 10 ]
            
    n =  len(a)
     
    splitArray(a, n, k)
     
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to split the given
// array into two separate arrays
// satisfying given condition
static void splitArray(int[] a, int n,
                       int k)
{
     
    // Stores resultant arrays
    List first = new List();
    List second = new List();
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If a[i] is smaller than
        // or equal to k/2
        if (a[i] <= k / 2)
            first.Add(a[i]);
        else
            second.Add(a[i]);
    }
   
    // Print first array
    for(int i = 0; i < first.Count; i++)
    {
        Console.Write(first[i] + " ");
    }
   
    // Print second array
    Console.WriteLine();
    for(int i = 0; i < second.Count; i++)
    {
        Console.Write(second[i] + " ");
    }
}
   
// Driver Code
public static void Main()
{
     
    // Given K
    int k = 5;
   
    // Given array
    int[] a = { 0, 1, 3, 2, 4, 5,
                6, 7, 8, 9, 10 };
   
    int n = a.Length;
      
    splitArray(a, n, k);
}
}
   
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
0 1 2 
3 4 5 6 7 8 9 10

时间复杂度: O(N),其中N是给定数组的大小。
辅助空间: O(N)