给定一个正整数N ,任务是使用前N 个自然数构造一个数组a[] ,其中不包含满足a[k] * 2 = a[i] + a[j] 的三元组(i, j, k)并且i < j < k 。
例子:
Input: N = 3
Output: {2, 3, 1 }
Explanation:
Since no such triplet exists in the array satisfying the condition, the required output is { 2, 3, 1 }.
Input: N = 10
Output: { 8, 4, 6, 10, 2, 7, 3, 5, 9, 1 }
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 递归查找结果数组的前(N / 2) 个元素和结果数组的最后(N / 2) 个元素。
- 合并数组的两半,使得数组的前半部分包含偶数,数组的后半部分包含奇数。
- 最后,打印结果数组。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to construct the array of size N
// that contains no such triplet satisfying
// the given conditions
vector constructArray(int N)
{
// Base case
if (N == 1) {
return { 1 };
}
// Stores the first half
// of the array
vector first
= constructArray(N / 2);
// Stores the last half
// of the array
vector last
= constructArray(N - (N / 2));
// Stores the merged array
vector ans;
// Insert even numbers
for (auto e : first) {
// Insert 2 * e
ans.push_back(2 * e);
}
// Insert odd numbers
for (auto o : last) {
// Insert (2 * o - 1)
ans.push_back((2 * o) - 1);
}
return ans;
}
// Function to print the resultant array
void printArray(vector ans, int N)
{
// Print resultant array
cout << "{ ";
for (int i = 0; i < N; i++) {
// Print current element
cout << ans[i];
// If i is not the last index
// of the resultant array
if (i != N - 1) {
cout << ", ";
}
}
cout << " }";
}
// Driver Code
int main()
{
int N = 10;
// Store the resultant array
vector ans
= constructArray(N);
printArray(ans, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to construct the array of size N
// that contains no such triplet satisfying
// the given conditions
static ArrayList constructArray(int N)
{
// Base case
if (N == 1)
{
ArrayList a = new ArrayList(1);
a.add(1);
return a;
}
// Stores the first half
// of the array
ArrayList first = new ArrayList(N);
first = constructArray(N / 2);
// Stores the last half
// of the array
ArrayList last = new ArrayList(N);
last = constructArray(N - N / 2);
ArrayList ans = new ArrayList(N);
// Insert even numbers
for(int i = 0; i < first.size(); i++)
{
// Insert 2 * first[i]
ans.add(2 * first.get(i));
}
// Insert odd numbers
for(int i = 0; i < last.size(); i++)
{
// Insert (2 * last[i] - 1)
ans.add(2 * last.get(i) - 1);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 10;
ArrayList answer = new ArrayList(N);
answer = constructArray(N);
System.out.print("{");
for(int i = 0; i < answer.size(); i++)
{
System.out.print(answer.get(i));
System.out.print(", ");
}
System.out.print("}");
}
}
// This code is contributed by koulick_sadhu
Python3
# Python3 program to implement
# the above approach
# Function to construct the array of size N
# that contains no such triplet satisfying
# the given conditions
def constructArray(N) :
# Base case
if (N == 1) :
a = []
a.append(1)
return a;
# Stores the first half
# of the array
first = constructArray(N // 2);
# Stores the last half
# of the array
last = constructArray(N - (N // 2));
# Stores the merged array
ans = [];
# Insert even numbers
for e in first :
# Insert 2 * e
ans.append(2 * e);
# Insert odd numbers
for o in last:
# Insert (2 * o - 1)
ans.append((2 * o) - 1);
return ans;
# Function to print the resultant array
def printArray(ans, N) :
# Print resultant array
print("{ ", end = "");
for i in range(N) :
# Print current element
print(ans[i], end = "");
# If i is not the last index
# of the resultant array
if (i != N - 1) :
print(", ",end = "");
print(" }", end = "");
# Driver Code
if __name__ == "__main__" :
N = 10;
# Store the resultant array
ans = constructArray(N);
printArray(ans, N);
# This code is contributed by AnkThon
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to construct the array of size N
// that contains no such triplet satisfying
// the given conditions
static List constructArray(int N)
{
// Base case
if (N == 1)
{
List a = new List(1);
a.Add(1);
return a;
}
// Stores the first half
// of the array
List first = new List();
first = constructArray(N / 2);
// Stores the last half
// of the array
List last = new List();
last = constructArray(N - N / 2);
List ans = new List();
// Insert even numbers
for(int i = 0; i < first.Count; i++)
{
// Insert 2 * first[i]
ans.Add(2 * first[i]);
}
// Insert odd numbers
for(int i = 0; i < last.Count; i++)
{
// Insert (2 * last[i] - 1)
ans.Add(2 * last[i] - 1);
}
return ans;
}
// Driver code
public static void Main()
{
int N = 10;
List answer = new List(N);
answer = constructArray(N);
Console.Write("{");
for(int i = 0; i < answer.Count; i++)
{
Console.Write(answer[i]);
Console.Write(", ");
}
Console.Write("}");
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
{ 8, 4, 6, 10, 2, 7, 3, 5, 9, 1 }
时间复杂度: O(N * log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。