Generate an array with product of all subarrays of length exceeding one divisible by K
Last Updated :
02 Mar, 2022
Given two positive integers N and K, the task is to generate an array of length N such that the product of every subarray of length greater than 1 must be divisible by K and the maximum element of the array must be less than K. If no such array is possible, then print -1.
Examples:
Input: N = 3, K = 20
Output: {15, 12, 5}
Explanation: All subarrays of length greater than 1 are {15, 12}, {12, 5}, {15, 12, 5} and their corresponding products are 180, 60 and 900, which are all divisible by 20.
Input: N = 4, K = 100
Output: {90, 90, 90, 90}
Approach: The given problem can be solved by the following observations:
It can be observed that by making the product of every subarray of length 2 divisible by K, the product of every subarray of length greater than 2 will automatically be divisible by K.
Therefore, the idea is to take two divisors of K, say d1 and d2, such that d1 * d2 = K and place them alternatively in the array. Follow the steps below to solve the problem:
- Initialize two integer variables d1 and d2.
- Check if K is prime. If found to be true, print -1.
- Otherwise, calculate the divisors of K and store two divisors in d1 and d2.
- After that, traverse from i = 0 to N – 1.
- Print d1 if i is even. Otherwise, print d2.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void array_divisbleby_k( int N, int K)
{
bool flag = false ;
int d1, d2;
for ( int i = 2; i * i <= K; i++) {
if (K % i == 0) {
flag = true ;
d1 = i;
d2 = K / i;
break ;
}
}
if (flag) {
for ( int i = 0; i < N; i++) {
if (i % 2 == 1) {
cout << d2 << " " ;
}
else {
cout << d1 << " " ;
}
}
}
else {
cout << -1;
}
}
int main()
{
int N = 5, K = 21;
array_divisbleby_k(N, K);
return 0;
}
|
Java
class GFG{
public static void array_divisbleby_k( int N,
int K)
{
boolean flag = false ;
int d1 = 0 , d2 = 0 ;
for ( int i = 2 ; i * i <= K; i++)
{
if (K % i == 0 )
{
flag = true ;
d1 = i;
d2 = K / i;
break ;
}
}
if (flag)
{
for ( int i = 0 ; i < N; i++)
{
if (i % 2 == 1 )
{
System.out.print(d2 + " " );
}
else
{
System.out.print(d1 + " " );
}
}
}
else
{
System.out.print(- 1 );
}
}
public static void main(String[] args)
{
int N = 5 , K = 21 ;
array_divisbleby_k(N, K);
}
}
|
Python3
def array_divisbleby_k(N, K):
flag = False
d1, d2 = 0 , 0
for i in range ( 2 , int (K * * ( 1 / 2 )) + 1 ):
if (K % i = = 0 ):
flag = True
d1 = i
d2 = K / / i
break
if (flag):
for i in range (N):
if (i % 2 = = 1 ):
print (d2, end = " " )
else :
print (d1, end = " " )
else :
print ( - 1 )
if __name__ = = "__main__" :
N = 5
K = 21
array_divisbleby_k(N, K)
|
C#
using System;
class GFG{
public static void array_divisbleby_k( int N,
int K)
{
bool flag = false ;
int d1 = 0, d2 = 0;
for ( int i = 2; i * i <= K; i++)
{
if (K % i == 0)
{
flag = true ;
d1 = i;
d2 = K / i;
break ;
}
}
if (flag)
{
for ( int i = 0; i < N; i++)
{
if (i % 2 == 1)
{
Console.Write(d2 + " " );
}
else
{
Console.Write(d1 + " " );
}
}
}
else
{
Console.Write(-1);
}
}
public static void Main( string [] args)
{
int N = 5, K = 21;
array_divisbleby_k(N, K);
}
}
|
Javascript
<script>
function array_divisbleby_k(N, K)
{
let flag = false ;
let d1 = 0, d2 = 0;
for (let i = 2; i * i <= K; i++)
{
if (K % i == 0)
{
flag = true ;
d1 = i;
d2 = K / i;
break ;
}
}
if (flag)
{
for (let i = 0; i < N; i++)
{
if (i % 2 == 1)
{
document.write(d2 + " " );
}
else
{
document.write(d1 + " " );
}
}
}
else
{
document.write(-1);
}
}
let N = 5, K = 21;
array_divisbleby_k(N, K);
</script>
|
Output:
3 7 3 7 3
Time complexity: O(N + ?K)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...