📌  相关文章
📜  将数组分成两个数组,其中不包含任何与 K 的对

📅  最后修改于: 2021-10-25 10:34:33             🧑  作者: 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程