Construct an array of first N natural numbers having no triplet (i, j, k) such that a[i] + a[j] = 2* a[k] where i < j< k
Last Updated :
11 Jun, 2021
Given a positive integer N, the task is to construct an array a[] using first N natural numbers which contains no such triplet (i, j, k) satisfying a[k] * 2 = a[i] + a[j] and i < j < k.
Examples:
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 }
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Recursively find the first (N / 2) elements of the resultant array and the last (N / 2) elements of the resultant array.
- Merge both halves of the array such that the first half of the array contains even numbers and the last half of the array contains the odd numbers.
- Finally, print the resultant array.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > constructArray( int N)
{
if (N == 1) {
return { 1 };
}
vector< int > first
= constructArray(N / 2);
vector< int > last
= constructArray(N - (N / 2));
vector< int > ans;
for ( auto e : first) {
ans.push_back(2 * e);
}
for ( auto o : last) {
ans.push_back((2 * o) - 1);
}
return ans;
}
void printArray(vector< int > ans, int N)
{
cout << "{ " ;
for ( int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) {
cout << ", " ;
}
}
cout << " }" ;
}
int main()
{
int N = 10;
vector< int > ans
= constructArray(N);
printArray(ans, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static ArrayList<Integer> constructArray( int N)
{
if (N == 1 )
{
ArrayList<Integer> a = new ArrayList<Integer>( 1 );
a.add( 1 );
return a;
}
ArrayList<Integer> first = new ArrayList<Integer>(N);
first = constructArray(N / 2 );
ArrayList<Integer> last = new ArrayList<Integer>(N);
last = constructArray(N - N / 2 );
ArrayList<Integer> ans = new ArrayList<Integer>(N);
for ( int i = 0 ; i < first.size(); i++)
{
ans.add( 2 * first.get(i));
}
for ( int i = 0 ; i < last.size(); i++)
{
ans.add( 2 * last.get(i) - 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 10 ;
ArrayList<Integer> answer = new ArrayList<Integer>(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( "}" );
}
}
|
Python3
def constructArray(N) :
if (N = = 1 ) :
a = []
a.append( 1 )
return a;
first = constructArray(N / / 2 );
last = constructArray(N - (N / / 2 ));
ans = [];
for e in first :
ans.append( 2 * e);
for o in last:
ans.append(( 2 * o) - 1 );
return ans;
def printArray(ans, N) :
print ( "{ " , end = "");
for i in range (N) :
print (ans[i], end = "");
if (i ! = N - 1 ) :
print ( ", " ,end = "");
print ( " }" , end = "");
if __name__ = = "__main__" :
N = 10 ;
ans = constructArray(N);
printArray(ans, N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > constructArray( int N)
{
if (N == 1)
{
List< int > a = new List< int >(1);
a.Add(1);
return a;
}
List< int > first = new List< int >();
first = constructArray(N / 2);
List< int > last = new List< int >();
last = constructArray(N - N / 2);
List< int > ans = new List< int >();
for ( int i = 0; i < first.Count; i++)
{
ans.Add(2 * first[i]);
}
for ( int i = 0; i < last.Count; i++)
{
ans.Add(2 * last[i] - 1);
}
return ans;
}
public static void Main()
{
int N = 10;
List< int > answer = new List< int >(N);
answer = constructArray(N);
Console.Write( "{" );
for ( int i = 0; i < answer.Count; i++)
{
Console.Write(answer[i]);
Console.Write( ", " );
}
Console.Write( "}" );
}
}
|
Javascript
<script>
function constructArray(N)
{
if (N == 1)
{
let a = [];
a.push(1);
return a;
}
let first = [];
first = constructArray(parseInt(N / 2, 10));
let last = [];
last = constructArray(N - parseInt(N / 2, 10));
let ans = [];
for (let i = 0; i < first.length; i++)
{
ans.push(2 * first[i]);
}
for (let i = 0; i < last.length; i++)
{
ans.push(2 * last[i] - 1);
}
return ans;
}
let N = 10;
let answer = [];
answer = constructArray(N);
document.write( "{" );
for (let i = 0; i < answer.length; i++)
{
document.write(answer[i]);
document.write( ", " );
}
document.write( "}" );
</script>
|
Output:
{ 8, 4, 6, 10, 2, 7, 3, 5, 9, 1 }
Time Complexity : O(N * log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...