Count subarrays having product equal to the power of a given Prime Number
Given an array arr[] of size N and an integer M, the task is to count the number of subarrays having product of its elements equal to the power of M, where M is a prime number.
Examples:
Input: arr[] = {2, 2, 2, 2}, M = 2
Output: 10
Explanation: All possible non-empty subarrays having product equal to the power of M = (4 * (4 + 1)) / 2 = 10
Input: arr[] = {1, 1, 1, 3}, M = 3
Output: 10
Naive Approach: The simplest approach is to generate all possible subarrays of the array arr[] and for each subarray, check if their product is a power of M or not. If found to be true, then increment the count of such subarrays by 1. Finally, print the count obtained.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is based upon the fact that for the product of any subarray to be equal to the power of M, all the elements in the subarray must also a power of M, since M is a prime number. Follow the steps below to solve the given problem:
- Initialize variable, say ans, to store the count of required subarrays
- Initialize a variable, say cnt, to store the count of consecutive numbers that are a power of M.
- Traverse the array over the range of indices [0, N – 1] using a variable, say i, and perform the following operations:
- If arr[i] is a power of M. Increment cnt by 1. Update ans = ans + (cnt * (cnt – 1)) / 2
- Otherwise, update cnt = 0.
- After completing the above steps, print the value of cnt.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower( int m, int y)
{
int res1 = log (y) / log (m);
double res2 = log (y) / log (m);
return (res1 == res2);
}
int numSub( int arr[], int n, int m)
{
int ans = 0;
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (isPower(m, arr[i])) {
cnt++;
ans += (cnt * (cnt - 1)) / 2;
}
else {
cnt = 0;
}
}
return ans;
}
int main()
{
int arr[] = { 1, 1, 1, 3 };
int m = 3;
int n = sizeof (arr) / sizeof (arr[0]);
cout << numSub(arr, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPower( int m, int y)
{
int res1 = ( int )Math.log(y) / ( int )Math.log(m);
double res2 = ( int )Math.log(y) / ( int )Math.log(m);
return (res1 == res2);
}
static int numSub( int arr[], int n, int m)
{
int ans = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
if (isPower(m, arr[i])) {
cnt++;
ans += (cnt * (cnt - 1 )) / 2 ;
}
else {
cnt = 0 ;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 3 };
int m = 3 ;
int n = arr.length;
System.out.println(numSub(arr, n, m));
}
}
|
Python3
from math import log
def isPower(m, y):
res1 = log(y) / / log(m)
res2 = log(y) / / log(m)
return (res1 = = res2)
def numSub(arr, n, m):
ans = 0
cnt = 0
for i in range (n):
if (isPower(m, arr[i])):
cnt + = 1
ans + = (cnt * (cnt - 1 )) / / 2
else :
cnt = 0
return ans
if __name__ = = '__main__' :
arr = [ 1 , 1 , 1 , 3 ]
m = 3
n = len (arr)
print (numSub(arr, n, m))
|
C#
using System;
class GFG{
static bool isPower( int m, int y)
{
int res1 = ( int )Math.Log(y) / ( int )Math.Log(m);
double res2 = ( int )Math.Log(y) / ( int )Math.Log(m);
return (res1 == res2);
}
static int numSub( int [] arr, int n, int m)
{
int ans = 0;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (isPower(m, arr[i]))
{
cnt++;
ans += (cnt * (cnt - 1)) / 2;
}
else
{
cnt = 0;
}
}
return ans;
}
public static void Main()
{
int [] arr = { 1, 1, 1, 3 };
int m = 3;
int n = arr.Length;
Console.Write(numSub(arr, n, m));
}
}
|
Javascript
<script>
function isPower(m, y)
{
let res1 = parseInt(Math.log(y) / Math.log(m));
let res2 = Math.log(y) / Math.log(m);
return (res1 == res2);
}
function numSub(arr, n, m)
{
let ans = 0;
let cnt = 0;
for (let i = 0; i < n; i++) {
if (isPower(m, arr[i])) {
cnt++;
ans += parseInt((cnt * (cnt - 1)) / 2);
}
else {
cnt = 0;
}
}
return ans;
}
let arr = [ 1, 1, 1, 3 ];
let m = 3;
let n = arr.length;
document.write(numSub(arr, n, m));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...