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-array with all elements as perfect number is {6, 28}.Input: arr[] = {25, 100, 2, 3, 9, 1}
Output: 0
Explanation:
None of the number is a perfect number
Approach:
- Traverse the array from left to right and initialize a max_length and current_length variable with 0.
- If the current element is a perfect number then increment current_length variable and continuethe process. Otherwise, set current_length to 0.
- At each step, assign max_length as max_length = max(current_length, max_length).
- Print the value of max_length in the end as it will store the required result.
Below is the implementation of the above approach:
C++
// C++ program to find the length of the // largest sub-array of an array every // element of whose is a perfect number #include <bits/stdc++.h> using namespace std; // Function that returns true if n is perfect bool isPerfect(long long int n) { // Variable to store sum of divisors long long int sum = 1; // Find all divisors and add them for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i != n) sum = sum + i + n / i; else sum = sum + i; } } // Check if sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) return true; return false; } // Function to return the length of the // largest sub-array of an array every // element of whose is a perfect number int contiguousPerfectNumber(int arr[], int n) { int current_length = 0; int max_length = 0; for (int i = 0; i < n; i++) { // Check if arr[i] is a perfect number if (isPerfect(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 << contiguousPerfectNumber(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 perfect number import java.util.*; class GFG { // Function that returns true if n is perfect static boolean isPerfect(int n) { // Variable to store sum of divisors int sum = 1; int i; // Find all divisors and add them for ( i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i != n) sum = sum + i + n / i; else sum = sum + i; } } // Check if sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) return true; return false; } // Function to return the length of the // largest sub-array of an array every // element of whose is a perfect number static int contiguousPerfectNumber(int arr[], int n) { int current_length = 0; int max_length = 0; int i; for (i = 0; i < n; i++) { // Check if arr[i] is a perfect number if (isPerfect(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.print(contiguousPerfectNumber(arr, n)); } } //This code is contributed by chitranayal |
Python3
# Python 3 program to find the length of # the largest sub-array of an array every # element of whose is a perfect number # Function that returns true if n is perfect def isPerfect( n ): # To store sum of divisors sum = 1 # Find all divisors and add them i = 2 while i * i <= n: if n % i == 0: sum = sum + i + n / i i += 1 # check if the sum of divisors is equal to # n, then n is a perfect number return (True if sum == n and n != 1 else False) # Function to return the length of the # largest sub-array of an array every # element of whose is a perfect number def contiguousPerfectNumber(arr, n): current_length = 0 max_length = 0 for i in range(0, n, 1): # check if arr[i] is a perfect number if (isPerfect(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(contiguousPerfectNumber(arr, n)) |
C#
// C# program to find the length of the // largest sub-array of an array every // element of whose is a perfect number using System; class GFG{ // Function that returns true if n is perfect static bool isPerfect(int n) { // Variable to store sum of divisors int sum = 1; int i; // Find all divisors and add them for(i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i != n) sum = sum + i + n / i; else sum = sum + i; } } // Check if sum of divisors is equal to // n, then n is a perfect number if (sum == n && n != 1) { return true; } return false; } // Function to return the length of the // largest sub-array of an array every // element of whose is a perfect number static int contiguousPerfectNumber(int []arr, int n) { int current_length = 0; int max_length = 0; int i; for(i = 0; i < n; i++) { // Check if arr[i] is a perfect number if (isPerfect(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.Write(contiguousPerfectNumber(arr, n)); } } // This code is contributed by sapnasingh4991 |
2
Time Complexity: O(N×√N)
Auxiliary Space Complexity: O(1)
Recommended Posts:
- Length of largest subarray whose all elements Powerful number
- Length of the largest subarray with contiguous elements | Set 1
- Find the length of largest subarray in which all elements are Autobiographical Numbers
- Length of longest Subarray with equal number of odd and even elements
- Maximum length of subarray such that all elements are equal in the subarray
- Find the length of largest subarray with 0 sum
- Largest sub-array whose all elements are perfect squares
- Largest subarray with frequency of all elements same
- Largest sum contiguous subarray having only non-negative elements
- Length of the longest Subarray with only Even Elements
- Largest number that is not a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Largest factor of a given number which is a perfect square
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Length of Longest Subarray with same elements in atmost K increments
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Minimum length subarray containing all unique elements after Q operations
- Largest Divisor of a Number not divisible by a perfect square
- Largest N digit Octal number which is a Perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


