Maximum number of Perfect Numbers present in a subarray of size K
Last Updated :
14 Sep, 2021
Given an array arr[ ] consisting of N integers, the task is to determine the maximum number of perfect Numbers in any subarray of size K.
Examples:
Input: arr[ ] = {28, 2, 3, 6, 496, 99, 8128, 24}, K = 4
Output: 3
Explanation: The sub-array {6, 496, 99, 8128} has 3 perfect numbers which is maximum.
Input: arr[ ]= {1, 2, 3, 6}, K=2
Output: 1
Naive Approach: The approach is to generate all possible subarrays of size K and for each subarray, count the number of elements that are a Perfect Number. Print the maximum count obtained for any subarray.
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above approach, convert the given array arr[ ] into a binary array where the ith element is 1 if it is a Perfect Number. Otherwise, the ith element is 0. Therefore, the problem reduces to finding the maximum sum subarray of size K in the binary array using the Sliding Window technique. Follow the steps below to solve the problem:
- Traverse the array and for each element of the array arr[], check if it is a Perfect Number or not.
- If arr[i] is a Perfect Number then convert arr[i] equal to 1. Otherwise, convert arr[i] equal to 0.
- To check if a number is a perfect number or not:
- Initialize a variable sum to store the sum of divisors.
- Traverse every number in the range [1, arr[i] – 1] and check if it is a divisor of arr[i] or not. Add all the divisors.
- If the sum of all the divisors is equal to arr[i], then the number is a perfect number. Otherwise, the number is not a Perfect Number.
- Compute the sum of the first subarray of size K in the modified array.
- Using the sliding window technique, find the maximum sum of a subarray from all possible subarrays of size K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPerfect( int N)
{
int sum = 1;
for ( int i = 2; i < sqrt (N); i++)
{
if (N % i == 0) {
if (i == N / i)
{
sum += i;
}
else
{
sum += i + N / i;
}
}
}
if (sum == N && N != 1)
return 1;
return 0;
}
int maxSum( int arr[], int N, int K)
{
if (N < K)
{
cout << "Invalid" ;
return -1;
}
int res = 0;
for ( int i = 0; i < K; i++)
{
res += arr[i];
}
int curr_sum = res;
for ( int i = K; i < N; i++)
{
curr_sum += arr[i] - arr[i - K];
res = max(res, curr_sum);
}
return res;
}
int max_PerfectNumbers( int arr[], int N, int K)
{
for ( int i = 0; i < N; i++)
{
arr[i] = isPerfect(arr[i]) ? 1 : 0;
}
return maxSum(arr, N, K);
}
int main()
{
int arr[] = { 28, 2, 3, 6, 496, 99, 8128, 24 };
int K = 4;
int N = sizeof (arr) / sizeof (arr[0]);
cout << max_PerfectNumbers(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int isPerfect( int N)
{
int sum = 1 ;
for ( int i = 2 ;
i < Math.sqrt(N); i++)
{
if (N % i == 0 )
{
if (i == N / i)
{
sum += i;
}
else
{
sum += i + N / i;
}
}
}
if (sum == N && N != 1 )
return 1 ;
return 0 ;
}
static int maxSum( int arr[],
int N, int K)
{
if (N < K)
{
System.out.print( "Invalid" );
return - 1 ;
}
int res = 0 ;
for ( int i = 0 ; i < K; i++)
{
res += arr[i];
}
int curr_sum = res;
for ( int i = K; i < N; i++)
{
curr_sum += arr[i] - arr[i - K];
res = Math.max(res, curr_sum);
}
return res;
}
static int max_PerfectNumbers( int arr[],
int N, int K)
{
for ( int i = 0 ; i < N; i++)
{
arr[i] = isPerfect(arr[i]) ==
1 ? 1 : 0 ;
}
return maxSum(arr, N, K);
}
public static void main(String[] args)
{
int arr[] = { 28 , 2 , 3 , 6 , 496 ,
99 , 8128 , 24 };
int K = 4 ;
int N = arr.length;
System.out.print(max_PerfectNumbers(arr,
N, K));
}
}
|
Python3
def isPerfect(N):
sum = 1
for i in range ( 2 , N):
if i * i > N:
break
if (N % i = = 0 ):
if (i = = N / / i):
sum + = i
else :
sum + = i + N / / i
if ( sum = = N and N ! = 1 ):
return 1
return 0
def maxSum(arr, N, K):
if (N < K):
print ( "Invalid" )
return - 1
res = 0
for i in range (K):
res + = arr[i]
curr_sum = res
for i in range (K, N):
curr_sum + = arr[i] - arr[i - K]
res = max (res, curr_sum)
return res
def max_PerfectNumbers(arr, N, K):
for i in range (N):
if isPerfect(arr[i]):
arr[i] = 1
else :
arr[i] = 0
return maxSum(arr, N, K)
if __name__ = = '__main__' :
arr = [ 28 , 2 , 3 , 6 ,
496 , 99 , 8128 , 24 ]
K = 4
N = len (arr)
print (max_PerfectNumbers(arr, N, K))
|
C#
using System;
class GFG{
static int isPerfect( int N)
{
int sum = 1;
for ( int i = 2;
i < Math.Sqrt(N); i++)
{
if (N % i == 0)
{
if (i == N / i)
{
sum += i;
}
else
{
sum += i + N / i;
}
}
}
if (sum == N && N != 1)
return 1;
return 0;
}
static int maxSum( int []arr,
int N, int K)
{
if (N < K)
{
Console.Write( "Invalid" );
return -1;
}
int res = 0;
for ( int i = 0; i < K; i++)
{
res += arr[i];
}
int curr_sum = res;
for ( int i = K; i < N; i++)
{
curr_sum += arr[i] - arr[i - K];
res = Math.Max(res, curr_sum);
}
return res;
}
static int max_PerfectNumbers( int []arr,
int N, int K)
{
for ( int i = 0; i < N; i++)
{
arr[i] = isPerfect(arr[i]) ==
1 ? 1 : 0;
}
return maxSum(arr, N, K);
}
public static void Main(String[] args)
{
int []arr = {28, 2, 3, 6, 496,
99, 8128, 24};
int K = 4;
int N = arr.Length;
Console.Write(max_PerfectNumbers(arr,
N, K));
}
}
|
Javascript
<script>
function isPerfect(N)
{
let sum = 1;
for (let i = 2;
i < Math.sqrt(N); i++)
{
if (N % i == 0)
{
if (i == N / i)
{
sum += i;
}
else
{
sum += i + N / i;
}
}
}
if (sum == N && N != 1)
return 1;
return 0;
}
function maxSum(arr, N, K)
{
if (N < K)
{
document.write( "Invalid" );
return -1;
}
let res = 0;
for (let i = 0; i < K; i++)
{
res += arr[i];
}
let curr_sum = res;
for (let i = K; i < N; i++)
{
curr_sum += arr[i] - arr[i - K];
res = Math.max(res, curr_sum);
}
return res;
}
function max_PerfectNumbers(arr, N, K)
{
for (let i = 0; i < N; i++)
{
arr[i] = isPerfect(arr[i]) ==
1 ? 1 : 0;
}
return maxSum(arr, N, K);
}
let arr = [ 28, 2, 3, 6, 496,
99, 8128, 24 ];
let K = 4;
let N = arr.length;
document.write(max_PerfectNumbers(arr, N, K));
</script>
|
Output:
3
Time Complexity: O( N * sqrt(N) )
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...