Check if a subarray of length K with sum equal to factorial of a number exists or not
Last Updated :
12 Jul, 2021
Given an array arr[] of N integers and an integer K, the task is to find a subarray of length K with a sum of elements equal to factorial of any number. If no such subarray exists, print “-1”.
Examples:
Input: arr[] = {23, 45, 2, 4, 6, 9, 3, 32}, K = 5
Output: 2 4 6 9 3
Explanation:
Subarray {2, 4, 6, 9, 3} with sum 24 (= 4!) satisfies the required condition.
Input: arr[] = {23, 45, 2, 4, 6, 9, 3, 32}, K = 3
Output: -1
Explanation:
No such subarray of length K (= 3) exists.
Naive Approach: The simplest approach to solve the problem is to calculate the sum of all subarrays of length K and check if any of those sums is factorial of any number. If found to be true for any subarray, print that subarray. Otherwise, print “-1”.
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use the Sliding Window technique to calculate the sum of all subarrays of length K and then check if the sum is a factorial or not. Below are the steps:
- Calculate the sum of first K array elements and store the sum in a variable, say sum.
- Then traverse the remaining array and keep updating sum to get the sum of the current subarray of size K by subtracting the first element from the previous subarray and adding the current array element.
- To check whether the sum is a factorial of a number or not, divide the sum by 2, 3, and so on until it cannot be divided further. If the number reduces to 1, the sum is the factorial of a number.
- If the sum in the above step is a factorial of a number, store the starting and ending index of that subarray to print the subarray.
- After completing the above steps, if no such subarray is found, print “-1”. Otherwise, print the subarray whose start and end indices are stored.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isFactorial( int n)
{
int i = 2;
while (n != 1) {
if (n % i != 0) {
return 0;
}
n /= i;
i++;
}
return i - 1;
}
pair< int , int > sumFactorial(
vector< int > arr, int K)
{
int i, sum = 0, ans;
for (i = 0; i < K; i++) {
sum += arr[i];
}
ans = isFactorial(sum);
if (ans != 0) {
return make_pair(ans, 0);
}
for ( int j = i; j < arr.size(); j++) {
sum += arr[j] - arr[j - K];
ans = isFactorial(sum);
if (ans != 0) {
return make_pair(ans,
j - K + 1);
}
}
return make_pair(-1, 0);
}
void printRes(pair< int , int > answer,
vector< int > arr, int K)
{
if (answer.first == -1) {
cout << -1 << endl;
}
else {
int i = 0;
int j = answer.second;
while (i < K) {
cout << arr[j] << " " ;
i++;
j++;
}
}
}
int main()
{
vector< int > arr
= { 23, 45, 2, 4,
6, 9, 3, 32 };
int K = 5;
pair< int , int > answer
= sumFactorial(arr, K);
printRes(answer, arr, K);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
public static class Pair
{
int x;
int y;
Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
static int isFactorial( int n)
{
int i = 2 ;
while (n != 1 )
{
if (n % i != 0 )
{
return 0 ;
}
n /= i;
i++;
}
return i - 1 ;
}
static ArrayList<Pair> sumFactorial( int arr[],
int K)
{
ArrayList<Pair> pair = new ArrayList<>();
int i, sum = 0 , ans;
for (i = 0 ; i < K; i++)
{
sum += arr[i];
}
ans = isFactorial(sum);
if (ans != 0 )
{
Pair p = new Pair(ans, 0 );
pair.add(p);
return pair;
}
for ( int j = i; j < arr.length; j++)
{
sum += arr[j] - arr[j - K];
ans = isFactorial(sum);
if (ans != 0 )
{
Pair p = new Pair(ans, j - K + 1 );
pair.add(p);
return pair;
}
}
Pair p = new Pair(- 1 , 0 );
pair.add(p);
return pair;
}
static void printRes(ArrayList<Pair> answer,
int arr[], int K)
{
if (answer.get( 0 ).x == - 1 )
{
System.out.println( "-1" );
}
else
{
int i = 0 ;
int j = answer.get( 0 ).y;
while (i < K)
{
System.out.print(arr[j] + " " );
i++;
j++;
}
}
}
public static void main(String args[])
{
int arr[] = { 23 , 45 , 2 , 4 ,
6 , 9 , 3 , 32 };
int K = 5 ;
ArrayList<Pair> answer = new ArrayList<>();
answer = sumFactorial(arr,K);
printRes(answer, arr, K);
}
}
|
Python3
def isFactorial(n):
i = 2
while (n ! = 1 ):
if (n % i ! = 0 ):
return 0
n = n / / i
i + = 1
return i - 1
def sumFactorial(arr, K):
i, Sum = 0 , 0
while (i < K):
Sum + = arr[i]
i + = 1
ans = isFactorial( Sum )
if (ans ! = 0 ):
return (ans, 0 )
for j in range (i, len (arr)):
Sum = Sum + arr[j] - arr[j - K]
ans = isFactorial( Sum )
if (ans ! = 0 ):
return (ans, j - K + 1 )
return ( - 1 , 0 )
def printRes(answer, arr, K):
if (answer[ 0 ] = = - 1 ):
print ( - 1 )
else :
i = 0
j = answer[ 1 ]
while (i < K):
print (arr[j], end = " " )
i + = 1
j + = 1
arr = [ 23 , 45 , 2 , 4 , 6 , 9 , 3 , 32 ]
K = 5
answer = sumFactorial(arr, K)
printRes(answer, arr, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Pair
{
public int x;
public int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
static int isFactorial( int n)
{
int i = 2;
while (n != 1)
{
if (n % i != 0)
{
return 0;
}
n /= i;
i++;
}
return i - 1;
}
static List<Pair> sumFactorial( int []arr,
int K)
{
List<Pair> pair = new List<Pair>();
int i, sum = 0, ans;
for (i = 0; i < K; i++)
{
sum += arr[i];
}
ans = isFactorial(sum);
if (ans != 0)
{
Pair p = new Pair(ans, 0);
pair.Add(p);
return pair;
}
for ( int j = i; j < arr.Length; j++)
{
sum += arr[j] - arr[j - K];
ans = isFactorial(sum);
if (ans != 0)
{
Pair p = new Pair(ans, j - K + 1);
pair.Add(p);
return pair;
}
}
Pair p1 = new Pair(-1, 0);
pair.Add(p1);
return pair;
}
static void printRes(List<Pair> answer,
int []arr, int K)
{
if (answer[0].x == -1)
{
Console.WriteLine( "-1" );
}
else
{
int i = 0;
int j = answer[0].y;
while (i < K)
{
Console.Write(arr[j] + " " );
i++;
j++;
}
}
}
public static void Main(String []args)
{
int []arr = {23, 45, 2, 4,
6, 9, 3, 32};
int K = 5;
List<Pair> answer = new List<Pair>();
answer = sumFactorial(arr, K);
printRes(answer, arr, K);
}
}
|
Javascript
<script>
function isFactorial(n)
{
let i = 2;
while (n != 1) {
if (n % i != 0) {
return 0;
}
n = parseInt(n / i, 10);
i++;
}
return i - 1;
}
function sumFactorial(arr,K)
{
let i, sum = 0, ans;
for (i = 0; i < K; i++) {
sum += arr[i];
}
ans = isFactorial(sum);
if (ans != 0) {
return [ans, 0];
}
for (let j = i; j < arr.length; j++) {
sum += arr[j] - arr[j - K];
ans = isFactorial(sum);
if (ans != 0) {
return [ans, j - K + 1];
}
}
return [-1, 0];
}
function printRes(answer,arr,K)
{
if (answer[0] == -1) {
document.write(-1);
}
else {
let i = 0;
let j = answer[1];
while (i < K) {
document.write(arr[j] + " " );
i++;
j++;
}
}
}
let arr= [ 23, 45, 2, 4,
6, 9, 3, 32 ];
let K = 5;
let answer= sumFactorial(arr, K);
printRes(answer, arr, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...