Check if a given integer is the product of K consecutive integers
Given two positive integers N and K, the task is to check if the given integer N can be expressed as the product of K consecutive integers or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 210, K = 3
Output: Yes
Explanation: 210 can be expressed as 5 * 6 * 7.
Input: N = 780, K =4
Output: No
Approach: The given problem can be solved by using Sliding Window Technique. Follow the steps below to solve the problem:
- Initialize two integers, say Kthroot and product, to store the Kth root of the integer N and the product of K consecutive integers respectively.
- Store the product of integers over the range [1, K] in the variable product.
- Otherwise, iterate over the range [2, Kthroot] and perform the following steps:
- If the value of the product is equal to N, then print “Yes” and break out of the loop.
- Update the value of product as (product*(i + K – 1)) / (i – 1).
- After completing the above steps, if none of the above cases satisfy, then print “No” as N cannot be expressed as the product of K consecutive integers.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
string checkPro( int n, int k)
{
double exp = 1.0 / k;
int KthRoot = ( int ) pow (n, exp );
int product = 1;
for ( int i = 1; i < k + 1; i++)
{
product = product * i;
}
if (product == n)
return "Yes" ;
else
{
for ( int j = 2; j < KthRoot + 1; j++)
{
product = product * (j + k - 1);
product = product / (j - 1);
if (product == n)
return "Yes" ;
}
}
return "No" ;
}
int main()
{
int N = 210;
int K = 3;
cout << checkPro(N, K);
return 0;
}
|
Java
public class GFG {
static String checkPro( int n, int k){
double exp = 1.0 / k ;
int KthRoot = ( int )Math.pow(n, exp);
int product = 1 ;
for ( int i = 1 ; i < k + 1 ; i++){
product = product * i;
}
if (product == n)
return "Yes" ;
else {
for ( int j = 2 ; j < KthRoot + 1 ; j++) {
product = product * (j + k - 1 ) ;
product = product / (j - 1 ) ;
if (product == n)
return "Yes" ;
}
}
return "No" ;
}
public static void main (String[] args) {
int N = 210 ;
int K = 3 ;
System.out.println(checkPro(N, K));
}
}
|
Python3
def checkPro(n, k):
KthRoot = int (n * * ( 1 / k))
product = 1
for i in range ( 1 , k + 1 ):
product = product * i
print (product)
if (product = = N):
return ( "Yes" )
for i in range ( 2 , KthRoot + 1 ):
product = product * (i + k - 1 )
product = product / (i - 1 )
print (product)
if (product = = N):
return ( "Yes" )
return ( "No" )
N = 210
K = 3
print (checkPro(N, K))
|
C#
using System;
class GFG{
static string checkPro( int n, int k)
{
double exp = 1.0 / k ;
int KthRoot = ( int )Math.Pow(n, exp);
int product = 1 ;
for ( int i = 1; i < k + 1; i++)
{
product = product * i;
}
if (product == n)
return "Yes" ;
else
{
for ( int j = 2; j < KthRoot + 1; j++)
{
product = product * (j + k - 1);
product = product / (j - 1);
if (product == n)
return "Yes" ;
}
}
return "No" ;
}
static public void Main()
{
int N = 210;
int K = 3;
Console.WriteLine(checkPro(N, K));
}
}
|
Javascript
<script>
function checkPro(n , k) {
var exp = 1.0 / k;
var KthRoot = parseInt( Math.pow(n, exp));
var product = 1;
for (i = 1; i < k + 1; i++) {
product = product * i;
}
if (product == n)
return "Yes" ;
else {
for (j = 2; j < KthRoot + 1; j++) {
product = product * (j + k - 1);
product = product / (j - 1);
if (product == n)
return "Yes" ;
}
}
return "No" ;
}
var N = 210;
var K = 3;
document.write(checkPro(N, K));
</script>
|
Time Complexity: O(K + N(1/K))
Auxiliary Space: O(1)
Last Updated :
08 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...