Count Subarray of size K in given Array with given LCM
Last Updated :
09 Dec, 2022
Given an array arr[] of length N, the task is to find the number of subarrays where the least common multiple (LCM) of the subarray is equal to K and the size of that subarray is S.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 6, S = 2
Output: 1
Explanation: {1, 2, 3 }, {2, 3}, {6}
There are 3 subarrays that can be generated from the main array with each having its LCM as 6. Out of which only {2, 3} is the length of 2.
Input: arr[] = {3, 6, 2, 8, 4}, K = 6, S = 2
Output: 2
Explanation: {3, 6}, {6, 2}
There are only 2 subarrays having LCM as 6 and length as 2.
Approach: Implement the idea below to solve the problem
Maintain two loops, so as to calculate LCM starting from each index of arr[]. When the LCM get’s equal to K, check the length of the subarray.
Steps were taken to solve the problem:
- Initialize count = 0, to count the number of subarrays.
- Maintain a loop to iterate through each index of array arr[].
- Initialize LCM = arr[i], then again start a loop from that index to the end of array arr[].
- Find the LCM of the lcm calculated till now for the current subarray and arr[j] as LCM =( a * b ) / GCD(a, b).
- When LCM gets equal to given K and the size of the subarray is equal to S, increment in count variable by 1.
- Whenever LCM gets greater than k, Break the inner loop. As the LCM is going to increase or stay same, it is never going to decrease.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int LCM( int a, int b)
{
long prod = a * b;
return prod / __gcd(a, b);
}
int subarrayEqualsLCMSize( int arr[], int k, int S, int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
int lcm = arr[i];
for ( int j = i; j < N; j++) {
lcm = LCM(lcm, arr[j]);
if (lcm == k && j - i + 1 == S)
count++;
if (lcm > k)
break ;
}
}
return count;
}
int main()
{
int arr[] = { 3, 2, 6, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 6, S = 2;
cout << subarrayEqualsLCMSize(arr, K, S, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
static int LCM( int a, int b)
{
int prod = a * b;
return prod / gcd(a, b);
}
static int subarrayEqualsLCMSize( int [] arr, int k,
int S, int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
int lcm = arr[i];
for ( int j = i; j < N; j++) {
lcm = LCM(lcm, arr[j]);
if (lcm == k && j - i + 1 == S) {
count++;
}
if (lcm > k) {
break ;
}
}
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 6 , 8 , 4 };
int N = arr.length;
int K = 6 , S = 2 ;
System.out.print(
subarrayEqualsLCMSize(arr, K, S, N));
}
}
|
Python
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def LCM(a, b):
prod = a * b
return prod / gcd(a, b)
def subarrayEqualsLCMSize(arr, k, S, N):
count = 0
for i in range ( 0 , N):
lcm = arr[i]
for j in range (i, N):
lcm = LCM(lcm, arr[j])
if (lcm = = k and j - i + 1 = = S):
count + = 1
if (lcm > k):
break
return count
arr = [ 3 , 2 , 6 , 8 , 4 ]
N = len (arr)
K = 6
S = 2
print (subarrayEqualsLCMSize(arr, K, S, N))
|
C#
using System;
public class GFG {
static int GCD( int num1, int num2)
{
int Remainder;
while (num2 != 0) {
Remainder = num1 % num2;
num1 = num2;
num2 = Remainder;
}
return num1;
}
public static int LCM( int a, int b)
{
long prod = a * b;
return ( int )(prod / GCD(a, b));
}
public static int
subarrayEqualsLCMSize( int [] arr, int k, int S, int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
int lcm = arr[i];
for ( int j = i; j < N; j++) {
lcm = LCM(lcm, arr[j]);
if (lcm == k && j - i + 1 == S)
count++;
if (lcm > k)
break ;
}
}
return count;
}
static public void Main()
{
int [] arr = { 3, 2, 6, 8, 4 };
int N = arr.Length;
int K = 6, S = 2;
Console.WriteLine(
subarrayEqualsLCMSize(arr, K, S, N));
}
}
|
Javascript
function GCD(num1, num2)
{
let Remainder;
while (num2 != 0) {
Remainder = num1 % num2;
num1 = num2;
num2 = Remainder;
}
return num1;
}
function LCM( a, b)
{
let prod = a * b;
return Math.floor(prod / GCD(a, b));
}
function
subarrayEqualsLCMSize(arr, k, S, N)
{
let count = 0;
for (let i = 0; i < N; i++) {
let lcm = arr[i];
for (let j = i; j < N; j++) {
lcm = LCM(lcm, arr[j]);
if (lcm == k && j - i + 1 == S)
count++;
if (lcm > k)
break ;
}
}
return count;
}
let arr = [ 3, 2, 6, 8, 4 ];
let N = arr.length;
let K = 6, S = 2;
console.log(subarrayEqualsLCMSize(arr, K, S, N));
|
Time Complexity: O(N2 * Log N)
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...