Open In App

Length of largest subarray whose all elements Powerful number

Last Updated : 07 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:
Maximum length sub-array with all elements as Powerful number is {36, 4}
Input: arr[] = {25, 100, 2, 3, 9, 1} 
Output:
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++




// C++ program to find the length of the
// largest sub-array of an array every
// element of whose is a powerful number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the
// number is powerful
bool isPowerful(int n)
{
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        int power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
 
        // If only 2^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // If n is not a power of 2
    // then this loop will execute
    // repeat above process
    for (int factor = 3;
         factor <= sqrt(n);
         factor += 2) {
 
        // Find highest power of "factor"
        // that divides n
        int power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
 
        // If only factor^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers are not powerful,
    // we return false if n is not 1.
    return (n == 1);
}
 
// Function to return the length of the
// largest sub-array of an array every
// element of whose is a powerful number
int contiguousPowerfulNumber(int arr[], int n)
{
 
    int current_length = 0;
    int max_length = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If arr[i] is
        // a Powerful number
        if (isPowerful(arr[i]))
            current_length++;
        else
            current_length = 0;
 
        max_length = max(max_length,
                         current_length);
    }
 
    return max_length;
}
 
// Driver code
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




// Java program to find the length of the
// largest sub-array of an array every
// element of whose is a powerful number
import java.util.*;
 
class solution{
 
// Function to check if the
// number is powerful
static boolean isPowerful(int n)
{
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        int power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
 
        // If only 2^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // If n is not a power of 2
    // then this loop will execute
    // repeat above process
    for (int factor = 3;
        factor <= Math.sqrt(n);
         factor += 2) {
 
        // Find highest power of "factor"
        // that divides n
        int power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
 
        // If only factor^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers are not powerful,
    // we return false if n is not 1.
    return (n == 1);
}
 
// Function to return the length of the
// largest sub-array of an array every
// element of whose is a powerful number
static int contiguousPowerfulNumber(int[] arr, int n)
{
 
    int current_length = 0;
    int max_length = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If arr[i] is
        // a Powerful number
        if (isPowerful(arr[i]))
            current_length++;
        else
            current_length = 0;
 
        max_length = Math.max(max_length,
                        current_length);
    }
 
    return max_length;
}
 
// Driver code
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));
 
}
}
 
// This code is contributed by Bhupendra_Singh


Python3




# Python 3 program to find the length of
# the largest sub-array of an array every
# element of whose is a powerful number
 
import math
 
# function to check if
# the number is powerful
def isPowerful(n):
   
    # First divide the number repeatedly by 2
    while (n % 2 == 0):
   
        power = 0
        while (n % 2 == 0):
           
            n = n//2
            power = power + 1
           
            
        # If only 2 ^ 1 divides
        # n (not higher powers),
        # then return false
        if (power == 1):
            return False
       
    
    # If n is not a power of 2
    # then this loop will execute
    # repeat above process
    for factor in range(3, int(math.sqrt(n))+1, 2):
       
        # Find highest power of
        # "factor" that divides n
        power = 0
        while (n % factor == 0):
           
            n = n//factor
            power = power + 1
           
    
        # If only factor ^ 1 divides
        # n (not higher powers),
        # then return false
        if (power == 1):
            return false
       
    
     # n must be 1 now if it
     # is not a prime number.
     # Since prime numbers are
     # not powerful, we return
     # false if n is not 1.
    return (n == 1)
 
 
# Function to return the length of the
# largest sub-array of an array every
# element of whose is a powerful number
def contiguousPowerfulNumber(arr, n):
    current_length = 0
    max_length = 0
 
    for i in range(0, n, 1):
         
        # If arr[i] is a
        # Powerful number
        if (isPowerful(arr[i])):
            current_length += 1
        else:
            current_length = 0
 
        max_length = max(max_length,
                        current_length)
     
    return max_length
 
# Driver code
if __name__ == '__main__':
    arr = [1, 7, 36, 4, 6, 28, 4]
    n = len(arr)
 
    print(contiguousPowerfulNumber(arr, n))


C#




// C# program to find the length of the
// largest sub-array of an array every
// element of whose is a powerful number
using System;
 
class solution{
 
// Function to check if the
// number is powerful
static bool isPowerful(int n)
{
 
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        int power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
 
        // If only 2^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // If n is not a power of 2
    // then this loop will execute
    // repeat above process
    for (int factor = 3;
             factor <= Math.Sqrt(n);
             factor += 2) {
 
        // Find highest power of "factor"
        // that divides n
        int power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
 
        // If only factor^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers are not powerful,
    // we return false if n is not 1.
    return (n == 1);
}
 
// Function to return the length of the
// largest sub-array of an array every
// element of whose is a powerful number
static int contiguousPowerfulNumber(int[] arr, int n)
{
    int current_length = 0;
    int max_length = 0;
    for (int i = 0; i < n; i++) {
 
        // If arr[i] is
        // a Powerful number
        if (isPowerful(arr[i]))
            current_length++;
        else
            current_length = 0;
 
        max_length = Math.Max(max_length,
                              current_length);
    }
    return max_length;
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 7, 36, 4, 6, 28, 4 };
    int n = arr.Length;
    Console.WriteLine(contiguousPowerfulNumber(arr, n));
}
}
 
// This code is contributed by gauravrajput1


Javascript




<script>
 
// Javascript program to find the length of the
// largest sub-array of an array every
// element of whose is a powerful number
 
// Function to check if the
// number is powerful
function isPowerful(n)
{
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        let power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
   
        // If only 2^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
   
    // If n is not a power of 2
    // then this loop will execute
    // repeat above process
    for (let factor = 3;
        factor <= Math.sqrt(n);
         factor += 2) {
   
        // Find highest power of "factor"
        // that divides n
        let power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
   
        // If only factor^1 divides n
        // (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
   
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers are not powerful,
    // we return false if n is not 1.
    return (n == 1);
}
   
// Function to return the length of the
// largest sub-array of an array every
// element of whose is a powerful number
function contiguousPowerfulNumber(arr, n)
{
   
    let current_length = 0;
    let max_length = 0;
   
    for (let i = 0; i < n; i++) {
   
        // If arr[i] is
        // a Powerful number
        if (isPowerful(arr[i]))
            current_length++;
        else
            current_length = 0;
   
        max_length = Math.max(max_length,
                        current_length);
    }
   
    return max_length;
 
// Driver Code
     
    let arr = [ 1, 7, 36, 4, 6, 28, 4 ];
    let n = arr.length;
   
    document.write(contiguousPowerfulNumber(arr, n));
                   
</script>


Output: 

2

 

Time Complexity: O(N×√N) 
Auxiliary Space Complexity: O(1)
 



Similar Reads

Length of largest subarray whose all elements are Perfect Number
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 Perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors. Examples: Input: arr[] = {1, 7, 36, 4, 6, 28, 4} Output: 2 Explanation: Maximum length sub-
7 min read
Count the nodes in the given tree whose weight is a powerful number
Given a tree, and the weights of all the nodes, the task is to count the number of nodes whose weight is a Powerful Number. A number n is said to be Powerful Number if, for every prime factor p of it, p2 also divides it. Example: Input: Output: 3 Explanation: 4, 16 and 25 are powerful weights in the tree. Approach: To solve the problem mentioned ab
9 min read
Maximum length of subarray such that all elements are equal in the subarray
Given an array arr[] of N integers, the task is to find the maximum length subarray that contains similar elements. Examples: Input: arr[] = {1, 2, 3, 4, 5, 5, 5, 5, 5, 2, 2, 1, 1} Output: 5 Explanation: The subarray {5, 5, 5, 5, 5} has maximum length 5 with identical elements. Input: arr[] = {1, 2, 3, 4} Output: 1 Explanation: All identical elemen
5 min read
Length of longest Powerful number subsequence in an Array
Given an array arr[] containing non-negative integers of length N, the task is to print the length of the longest subsequence of Powerful numbers in the array. A number n is said to be Powerful Number if, for every prime factor p of it, p2 also divides it. Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 2 Explanation: Longest Powerful numbe
8 min read
Find the length of largest subarray in which all elements are Autobiographical Numbers
Given an array arr[] of integers, our task is to find the length of the largest subarray such that all the elements of the sub-array are Autobiographical Number. An Autobiographical Number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. For example, 21200
8 min read
Maximize product of min value of subarray and sum of subarray over all subarrays of length K
Given an array arr[] of N integers, the task is to find the maximum possible value of (min * sum) among all possible subarrays having K elements, where min denotes the smallest integer of the subarray and sum denotes the sum of all elements of the subarray. Example: Input: arr[] = {1, 2, 3, 2}, K = 3Output: 14Explanation: For the subarray {2, 3, 2}
7 min read
Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equal
Given two integers M and N, the task is to find the number of N-length arrays possible having non-equal adjacent elements lying in the range [1, M] having elements at first and last indices equal. Examples: Input: N = 3, M = 3Output: 6Explanation:The possible arrays are {1, 2, 1}, {1, 3, 1}, {2, 1, 2}, {2, 3, 2}, {3, 1, 3}, {3, 2, 3}. Input: N = 5,
7 min read
Length of the largest subarray with contiguous elements | Set 1
Given an array of distinct integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence. Examples: Input: arr[] = {10, 12, 11}; Output: Length of the longest contiguous subarray is 3 Input: arr[] = {14, 12, 11, 20}; Output: Length of the longest contiguous subarray is 2 Input: arr[] = {1, 56, 58
7 min read
Length of the largest subarray with contiguous elements | Set 2
Given an array of integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence. In the previous post, we have discussed a solution that assumes that elements in given array are distinct. Here we discuss a solution that works even if the input array has duplicates. Examples: Input: arr[] = {10, 1
7 min read
Length of largest common subarray in all the rows of given Matrix
Given a matrix mat[][] of size N×M where each row of the matrix is a permutation of the elements from [1, M], the task is to find the maximum length of the subarray present in each row of the matrix. Examples: Input: mat[][] = {{1, 2, 3, 4, 5}, {2, 3, 4, 1, 5}, {5, 2, 3, 4, 1}, {1, 5, 2, 3, 4}}Output: 3Explanation: In each row, {2, 3, 4} is the lon
10 min read