给定一个正整数N ,任务是生成两个长度为N的数组,以使两个数组的相同索引元素都是互质的,并且数组元素之和之间的绝对差为N。
例子:
Input: N = 5
Output:
{1, 3, 5, 7, 9}
{2, 4, 6, 8, 10}
Explanation: Pairs of same-indexed elements are (1, 2), (3, 4), (5, 6), (7, 8), (9, 10). It can be observed that all the pairs are coprime and the absolute difference of the sum of the two arrays is 5.
Input: N = 3
Output:
{2, 4, 7}
{1, 3, 6}
方法:这个想法是基于以下观察:两个连续的自然数始终是素数,并且它们之间的差是1 。请按照以下步骤解决问题:
- 初始化大小为N的两个数组A []和B [] 。
- 使用变量i遍历[1,2 * N]范围。对于范围内的每个元素,检查i是否可被2整除。如果发现为真,则将i插入数组A [] 。否则,将i插入数组B []中。
- 完成上述步骤后,打印两个阵列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to generate two arrays
// satisfying the given conditions
void printArrays(int n)
{
// Declare the two arrays A and B
vector A, B;
// Iterate from range [1, 2*n]
for (int i = 1; i <= 2 * n; i++) {
// Assign consecutive numbers to
// same indices of the two arrays
if (i % 2 == 0)
A.push_back(i);
else
B.push_back(i);
}
// Print the first array
cout << "{ ";
for (int i = 0; i < n; i++) {
cout << A[i];
if (i != n - 1)
cout << ", ";
}
cout << " }\n";
// Print the second array, B
cout << "{ ";
for (int i = 0; i < n; i++) {
cout << B[i];
if (i != n - 1)
cout << ", ";
}
cout << " }";
}
// Driver Code
int main()
{
int N = 5;
// Function Call
printArrays(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Satisfying the given conditions
static void printArrays(int n)
{
// Declare the two arrays A and B
ArrayList A = new ArrayList();
ArrayList B = new ArrayList();
// Iterate from range [1, 2*n]
for(int i = 1; i <= 2 * n; i++)
{
// Assign consecutive numbers to
// same indices of the two arrays
if (i % 2 == 0)
A.add(i);
else
B.add(i);
}
// Print the first array
System.out.print("{ ");
for(int i = 0; i < n; i++)
{
System.out.print(A.get(i));
if (i != n - 1)
System.out.print(", ");
}
System.out.print(" }\n");
// Print the second array, B
System.out.print("{ ");
for(int i = 0; i < n; i++)
{
System.out.print(B.get(i));
if (i != n - 1)
System.out.print(", ");
}
System.out.print(" }");
}
// Driver code
public static void main (String[] args)
{
int N = 5;
// Function Call
printArrays(N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to generate two arrays
# satisfying the given conditions
def printArrays(n) :
# Declare the two arrays A and B
A, B = [], [];
# Iterate from range [1, 2*n]
for i in range(1, 2 * n + 1):
# Assign consecutive numbers to
# same indices of the two arrays
if (i % 2 == 0) :
A.append(i);
else :
B.append(i);
# Print the first array
print("{ ", end="");
for i in range(n) :
print(A[i], end="");
if (i != n - 1) :
print(", ", end="");
print("}");
# Print the second array, B
print("{ ", end="");
for i in range(n) :
print(B[i], end="");
if (i != n - 1) :
print(",", end=" ");
print(" }", end="");
# Driver Code
if __name__ == "__main__" :
N = 5;
# Function Call
printArrays(N);
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Satisfying the given conditions
static void printArrays(int n)
{
// Declare the two arrays A and B
List A = new List();
List B = new List();
// Iterate from range [1, 2*n]
for(int i = 1; i <= 2 * n; i++)
{
// Assign consecutive numbers to
// same indices of the two arrays
if (i % 2 == 0)
A.Add(i);
else
B.Add(i);
}
// Print the first array
Console.Write("{ ");
for(int i = 0; i < n; i++)
{
Console.Write(A[i]);
if (i != n - 1)
Console.Write(", ");
}
Console.Write(" }\n");
// Print the second array, B
Console.Write("{ ");
for(int i = 0; i < n; i++)
{
Console.Write(B[i]);
if (i != n - 1)
Console.Write(", ");
}
Console.Write(" }");
}
// Driver Code
public static void Main()
{
int N = 5;
// Function Call
printArrays(N);
}
}
// This code is contributed by susmitakundugoaldanga
输出:
{ 2, 4, 6, 8, 10 }
{ 1, 3, 5, 7, 9 }
时间复杂度: O(N)
辅助空间: O(N)