Length of largest subarray whose all elements Powerful number
Last Updated :
07 Mar, 2022
Given an array arr[] of integer elements, the task is to find the length of the largest sub-array of arr[] such that all the elements of the sub-array are Powerful number.
A number n is said to be Powerful Number if, for every prime factor p of it, p2 also divides it.
Examples:
Input: arr[] = {1, 7, 36, 4, 6, 28, 4}
Output:2
Maximum length sub-array with all elements as Powerful number is {36, 4}
Input: arr[] = {25, 100, 2, 3, 9, 1}
Output: 2
Maximum length sub-array with all elements as Powerful number is {25, 100} or {9, 1}
Approach:
- Traverse the array from left to right. Initialize a max_length and current_length variable with 0.
- If the current element is a Powerful number then increment current_length variable and continue. Otherwise, set current_length = 0.
- At each step, assign max_length as max_length = max(current_length, max_length).
- Print the value of max_length in the end.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerful( int n)
{
while (n % 2 == 0) {
int power = 0;
while (n % 2 == 0) {
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for ( int factor = 3;
factor <= sqrt (n);
factor += 2) {
int power = 0;
while (n % factor == 0) {
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
int contiguousPowerfulNumber( int arr[], int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++) {
if (isPowerful(arr[i]))
current_length++;
else
current_length = 0;
max_length = max(max_length,
current_length);
}
return max_length;
}
int main()
{
int arr[] = { 1, 7, 36, 4, 6, 28, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << contiguousPowerfulNumber(arr, n);
return 0;
}
|
Java
import java.util.*;
class solution{
static boolean isPowerful( int n)
{
while (n % 2 == 0 ) {
int power = 0 ;
while (n % 2 == 0 ) {
n /= 2 ;
power++;
}
if (power == 1 )
return false ;
}
for ( int factor = 3 ;
factor <= Math.sqrt(n);
factor += 2 ) {
int power = 0 ;
while (n % factor == 0 ) {
n = n / factor;
power++;
}
if (power == 1 )
return false ;
}
return (n == 1 );
}
static int contiguousPowerfulNumber( int [] arr, int n)
{
int current_length = 0 ;
int max_length = 0 ;
for ( int i = 0 ; i < n; i++) {
if (isPowerful(arr[i]))
current_length++;
else
current_length = 0 ;
max_length = Math.max(max_length,
current_length);
}
return max_length;
}
public static void main(String args[])
{
int []arr = { 1 , 7 , 36 , 4 , 6 , 28 , 4 };
int n = arr.length;
System.out.println(contiguousPowerfulNumber(arr, n));
}
}
|
Python3
import math
def isPowerful(n):
while (n % 2 = = 0 ):
power = 0
while (n % 2 = = 0 ):
n = n / / 2
power = power + 1
if (power = = 1 ):
return False
for factor in range ( 3 , int (math.sqrt(n)) + 1 , 2 ):
power = 0
while (n % factor = = 0 ):
n = n / / factor
power = power + 1
if (power = = 1 ):
return false
return (n = = 1 )
def contiguousPowerfulNumber(arr, n):
current_length = 0
max_length = 0
for i in range ( 0 , n, 1 ):
if (isPowerful(arr[i])):
current_length + = 1
else :
current_length = 0
max_length = max (max_length,
current_length)
return max_length
if __name__ = = '__main__' :
arr = [ 1 , 7 , 36 , 4 , 6 , 28 , 4 ]
n = len (arr)
print (contiguousPowerfulNumber(arr, n))
|
C#
using System;
class solution{
static bool isPowerful( int n)
{
while (n % 2 == 0) {
int power = 0;
while (n % 2 == 0) {
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for ( int factor = 3;
factor <= Math.Sqrt(n);
factor += 2) {
int power = 0;
while (n % factor == 0) {
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
static int contiguousPowerfulNumber( int [] arr, int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++) {
if (isPowerful(arr[i]))
current_length++;
else
current_length = 0;
max_length = Math.Max(max_length,
current_length);
}
return max_length;
}
public static void Main(String []args)
{
int []arr = { 1, 7, 36, 4, 6, 28, 4 };
int n = arr.Length;
Console.WriteLine(contiguousPowerfulNumber(arr, n));
}
}
|
Javascript
<script>
function isPowerful(n)
{
while (n % 2 == 0) {
let power = 0;
while (n % 2 == 0) {
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for (let factor = 3;
factor <= Math.sqrt(n);
factor += 2) {
let power = 0;
while (n % factor == 0) {
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
function contiguousPowerfulNumber(arr, n)
{
let current_length = 0;
let max_length = 0;
for (let i = 0; i < n; i++) {
if (isPowerful(arr[i]))
current_length++;
else
current_length = 0;
max_length = Math.max(max_length,
current_length);
}
return max_length;
}
let arr = [ 1, 7, 36, 4, 6, 28, 4 ];
let n = arr.length;
document.write(contiguousPowerfulNumber(arr, n));
</script>
|
Time Complexity: O(N×√N)
Auxiliary Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...