给定一个数组arr [] ,该数组arr []由N个非负唯一整数和整数K组成,任务是将数组分布在两个数组中,以使两个数组都不包含总和为K的一对。
例子:
Input: arr[] = {1, 0, 2, 3, 4, 7, 8, 9}, K = 4
Output:
3, 2, 4, 7, 8, 9
0, 1
Explanation: Pairs (1, 3) and (0, 4) from the given array cannot be placed in the same array. Therefore, 0, 1 can be placed in an array and 3, 4 can be placed in the other array. The remaining array elements can be placed in any of the two arrays.
Input: arr[] = {0, 1, 2, 4, 5, 6, 7, 8, 9}, K = 7
Output:
0, 1, 2, 4
5, 6, 7, 9, 8
方法:想法是遍历数组并将大于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)