Given an array arr[] of length N, with values less than N, the task is to construct another array A[] of same length such that for every ith element in the array A[], arr[i] is the last index (1-based indexing) consisting of a multiple of A[i].
Examples:
Input: arr[] = {4, 1, 2, 3, 4}
Output: 2 3 5 7 2
Explanation:
A[0]: Last index which can contain a multiple of A[0] has to be A[arr[0]] = A[4].
A[1]: Last index which can contain a multiple of A[1] has to be A[arr[1]] = A[1].
A[2]: Last index which can contain a multiple of A[2] has to be A[arr[2]] = A[2].
A[3]: Last index which can contain a multiple of A[3] has to be A[arr[3]] = A[3].
A[4]: Last index which can contain a multiple of A[4] has to be A[arr[4]] = A[4].
Hence, in the final array, A[4] must be divisible by A[0] and A[1], A[2] and A[3] must not be divisible by any other array elements.
Hence, the array A[] = {2, 3, 5, 7, 2} satisfies the condition.
Input: arr[] = {0, 1, 2, 3, 4}
Output: 2 3 5 7 11
Approach: The idea is to place prime numbers as array elements in required indices satisfying the conditions. Follow the steps below to solve the problem:
- Generate all Prime Numbers using Sieve Of Eratosthenes and store it in another array.
- Initialize array A[] with {0}, to store the required array.
- Traverse the array arr[] and perform the following steps:
- Check if A[arr[i]] is non-zero but A[i] is 0. If found to be true, then assign A[i] = A[arr[i]].
- Check if A[arr[i]] and A[i] are both 0 or not. If found to be true, then assign a prime number different to already assigned array elements, to both indices arr[i] and i.
- After completing the above steps, print the elements of array A[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sieve[1000000];
void sieveOfPrimes()
{
memset (sieve, 1, sizeof (sieve));
int N = 1000000;
for ( int i = 2; i * i <= N; i++) {
if (sieve[i] == 0)
continue ;
for ( int j = i * i; j <= N; j += i)
sieve[j] = 0;
}
}
void getArray( int * arr, int N)
{
int A[N] = { 0 };
vector< int > v;
sieveOfPrimes();
for ( int i = 2; i <= 1e5; i++)
if (sieve[i])
v.push_back(i);
int j = 0;
for ( int i = 0; i < N; i++) {
int ind = arr[i];
if (A[i] != 0)
continue ;
else if (A[ind] != 0)
A[i] = A[ind];
else {
int prime = v[j++];
A[i] = prime;
A[ind] = A[i];
}
}
for ( int i = 0; i < N; i++) {
cout << A[i] << " " ;
}
}
int main()
{
int arr[] = { 4, 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
getArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int [] sieve = new int [ 10000000 ];
static void sieveOfPrimes()
{
Arrays.fill(sieve, 1 );
int N = 1000000 ;
for ( int i = 2 ; i * i <= N; i++)
{
if (sieve[i] == 0 )
continue ;
for ( int j = i * i; j <= N; j += i)
sieve[j] = 0 ;
}
}
static void getArray( int [] arr, int N)
{
int A[] = new int [N];
Arrays.fill(A, 0 );
ArrayList<Integer> v
= new ArrayList<Integer>();
sieveOfPrimes();
for ( int i = 2 ; i <= 1000000 ; i++)
if (sieve[i] != 0 )
v.add(i);
int j = 0 ;
for ( int i = 0 ; i < N; i++)
{
int ind = arr[i];
if (A[i] != 0 )
continue ;
else if (A[ind] != 0 )
A[i] = A[ind];
else {
int prime = v.get(j++);
A[i] = prime;
A[ind] = A[i];
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print( A[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 1 , 2 , 3 , 4 };
int N = arr.length;
getArray(arr, N);
}
}
|
Python3
sieve = [ 1 ] * ( 1000000 + 1 )
def sieveOfPrimes():
global sieve
N = 1000000
for i in range ( 2 , N + 1 ):
if i * i > N:
break
if (sieve[i] = = 0 ):
continue
for j in range (i * i, N + 1 , i):
sieve[j] = 0
def getArray(arr, N):
global sieve
A = [ 0 ] * N
v = []
sieveOfPrimes()
for i in range ( 2 , int ( 1e5 ) + 1 ):
if (sieve[i]):
v.append(i)
j = 0
for i in range (N):
ind = arr[i]
if (A[i] ! = 0 ):
continue
elif (A[ind] ! = 0 ):
A[i] = A[ind]
else :
prime = v[j]
A[i] = prime
A[ind] = A[i]
j + = 1
for i in range (N):
print (A[i], end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 1 , 2 , 3 , 4 ]
N = len (arr)
getArray(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int [] sieve = new int [10000000];
static void sieveOfPrimes()
{
for ( int i = 0; i < 10000000; i++)
{
sieve[i] = 1;
}
int N = 1000000;
for ( int i = 2; i * i <= N; i++)
{
if (sieve[i] == 0)
continue ;
for ( int j = i * i; j <= N; j += i)
sieve[j] = 0;
}
}
static void getArray( int [] arr, int N)
{
int [] A = new int [N];
for ( int i = 0; i < N; i++)
{
A[i] = 0;
}
List< int > v
= new List< int >();
sieveOfPrimes();
for ( int i = 2; i <= 1000000; i++)
if (sieve[i] != 0)
v.Add(i);
int j = 0;
for ( int i = 0; i < N; i++)
{
int ind = arr[i];
if (A[i] != 0)
continue ;
else if (A[ind] != 0)
A[i] = A[ind];
else {
int prime = v[j++];
A[i] = prime;
A[ind] = A[i];
}
}
for ( int i = 0; i < N; i++)
{
Console.Write( A[i] + " " );
}
}
public static void Main(String[] args)
{
int [] arr = { 4, 1, 2, 3, 4 };
int N = arr.Length;
getArray(arr, N);
}
}
|
Javascript
<script>
var sieve = Array(1000000);
function sieveOfPrimes()
{
sieve = Array(1000000).fill(1);
var N = 1000000;
for ( var i = 2; i * i <= N; i++) {
if (sieve[i] == 0)
continue ;
for ( var j = i * i; j <= N; j += i)
sieve[j] = 0;
}
}
function getArray(arr, N)
{
var A = Array(N).fill(0);
var v = [];
sieveOfPrimes();
for ( var i = 2; i <= 1e5; i++)
if (sieve[i])
v.push(i);
var j = 0;
for ( var i = 0; i < N; i++) {
var ind = arr[i];
if (A[i] != 0)
continue ;
else if (A[ind] != 0)
A[i] = A[ind];
else {
var prime = v[j++];
A[i] = prime;
A[ind] = A[i];
}
}
for ( var i = 0; i < N; i++) {
document.write( A[i] + " " );
}
}
var arr = [4, 1, 2, 3, 4];
var N = arr.length;
getArray(arr, N);
</script>
|
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
25 Mar, 2022
Like Article
Save Article