Smallest subarray with product divisible by k
Last Updated :
02 Aug, 2022
Given an array of non-negative numbers, find length of smallest subarray whose product is a multiple of k.
Examples :
Input : arr[] = {1, 9, 16, 5, 4, 3, 2}
k = 720
Output: 3
The smallest subarray is {9, 16, 5} whose
product is 720.
Input : arr[] = {1, 2, 4, 5, 6}
K = 96
Output : No such subarray exists
The idea is simple. We traverse through all subarrays. For each subarray, we compute its product. If the product is multiple of k, we check if length of the subarray is smaller than current result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findsubArray( int arr[], int k)
{
int n = 7;
int res = n + 1;
for ( int i = 0; i < n; i++) {
int curr_prod = 1;
for ( int j = i; j < n; j++) {
curr_prod = curr_prod * arr[j];
if (curr_prod % k == 0 && res
> (j - i + 1))
{
res = min(res, j - i + 1);
break ;
}
}
}
return (res == n + 1) ? 0 : res;
}
int main( void )
{
int array[] = { 1, 9, 16, 5, 4, 3, 2 };
int k = 720;
int answer = findsubArray(array, k);
if (answer != 0)
cout<<answer<< "\n" ;
else
cout<< "No Such subarray exists." << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class findSubArray {
public static int findsubArray( int arr[], int k)
{
int n = arr.length;
int res = n+ 1 ;
for ( int i = 0 ; i < n; i++) {
int curr_prod = 1 ;
for ( int j = i; j < n; j++) {
curr_prod = curr_prod * arr[j];
if (curr_prod % k == 0 && res > (j-i+ 1 ))
{
res = Math.min(res, j-i+ 1 );
break ;
}
}
}
return (res == n+ 1 )? 0 : res;
}
public static void main(String[] args)
{
int array[] = { 1 , 9 , 16 , 5 , 4 , 3 , 2 };
int k = 720 ;
int answer = findsubArray(array, k);
if (answer != 0 )
System.out.println(answer);
else
System.out.println( "No Such subarray exists." );
}
}
|
Python3
def findsubArray(arr, k) :
n = len (arr)
res = n + 1
for i in range ( 0 ,n) :
curr_prod = 1
for j in range ( i, n):
curr_prod = curr_prod * arr[j]
if (curr_prod % k = = 0 and
res > (j - i + 1 )) :
res = min (res, j - i + 1 )
break
if (res = = n + 1 ) :
return 0
else :
return res
array = [ 1 , 9 , 16 , 5 , 4 , 3 , 2 ]
k = 720
answer = findsubArray(array, k)
if (answer ! = 0 ):
print (answer)
else :
print ( "No Such subarray exists." )
|
C#
using System;
public class GFG {
public static int findsubArray( int []arr, int k)
{
int n = arr.Length;
int res = n+1;
for ( int i = 0; i < n; i++) {
int curr_prod = 1;
for ( int j = i; j < n; j++) {
curr_prod = curr_prod * arr[j];
if (curr_prod % k == 0 && res > (j-i+1))
{
res = Math.Min(res, j-i+1);
break ;
}
}
}
return (res == n+1) ? 0 : res;
}
public static void Main()
{
int []array = { 1, 9, 16, 5, 4, 3, 2 };
int k = 720;
int answer = findsubArray(array, k);
if (answer != 0)
Console.WriteLine(answer);
else
Console.WriteLine( "No Such subarray"
+ " exists." );
}
}
|
PHP
<?php
function findsubArray( $arr , $k )
{
$n = 7;
$res = $n + 1;
for ( $i = 0; $i < $n ; $i ++) {
$curr_prod = 1;
for ( $j = $i ; $j < $n ; $j ++) {
$curr_prod = $curr_prod * $arr [ $j ];
if ( $curr_prod % $k == 0 &&
$res > ( $j - $i + 1))
{
$res = min( $res , $j - $i + 1);
break ;
}
}
}
return ( $res == $n + 1) ? 0 : $res ;
}
$arr = array (1, 9, 16, 5, 4, 3, 2);
$k = 720;
$answer = findsubArray( $arr , $k );
if ( $answer != 0)
echo $answer . "\n" ;
else
echo "No Such subarray exists." . "\n" ;
?>
|
Javascript
<script>
function findsubArray(arr, k)
{
let n = arr.length;
let res = n+1;
for (let i = 0; i < n; i++) {
let curr_prod = 1;
for (let j = i; j < n; j++) {
curr_prod = curr_prod * arr[j];
if (curr_prod % k == 0 && res > (j-i+1))
{
res = Math.min(res, j-i+1);
break ;
}
}
}
return (res == n+1) ? 0 : res;
}
let array = [ 1, 9, 16, 5, 4, 3, 2 ];
let k = 720;
let answer = findsubArray(array, k);
if (answer != 0)
document.write(answer);
else
document.write( "No Such subarray"
+ " exists." );
</script>
|
Time Complexity: O(n^2)
Share your thoughts in the comments
Please Login to comment...