Given an integer X, the task is to determine the minimum value of Y greater than X, such that count of divisors of X and Y have different parities.
Examples:
Input: X = 5
Output: 9
Explanation: The count of divisors of 5 and 9 are 2 and 3 respectively, which are of different parities.
Input: X = 9
Output: 10
Explanation: The counts of divisors of 9 and 10 are 3 and 4, which are of different parities.
Naive Approach: The simplest approach to solve the problem is to iterate each number starting from X + 1 until an element with count of the divisors with parity opposite to that of X is obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int divisorCount( int n)
{
int x = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (i == n / i)
x++;
else
x += 2;
}
}
return x;
}
int minvalue_y( int x)
{
int a = divisorCount(x);
int y = x + 1;
while ((a & 1)
== (divisorCount(y) & 1))
y++;
return y;
}
int main()
{
int x = 5;
cout << minvalue_y(x) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int divisorCount( int n)
{
int x = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i == n / i)
x++;
else
x += 2 ;
}
}
return x;
}
static int minvalue_y( int x)
{
int a = divisorCount(x);
int y = x + 1 ;
while ((a & 1 ) == (divisorCount(y) & 1 ))
y++;
return y;
}
public static void main(String[] args)
{
int x = 5 ;
System.out.println(minvalue_y(x));
}
}
|
C#
using System;
class GFG{
static int divisorCount( int n)
{
int x = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i == n / i)
x++;
else
x += 2;
}
}
return x;
}
static int minvalue_y( int x)
{
int a = divisorCount(x);
int y = x + 1;
while ((a & 1) == (divisorCount(y) & 1))
y++;
return y;
}
public static void Main()
{
int x = 5;
Console.WriteLine(minvalue_y(x));
}
}
|
Python3
def divisorCount(n):
x = 0 ;
for i in range ( 1 , n):
if (n % i = = 0 ):
if (i = = n / / i):
x + = 1 ;
else :
x + = 2 ;
if (i * i > n):
break ;
return x;
def minvalue_y(x):
a = divisorCount(x);
y = x + 1 ;
while ((a & 1 ) = = (divisorCount(y) & 1 )):
y + = 1 ;
return y;
if __name__ = = '__main__' :
x = 5 ;
print (minvalue_y(x));
|
Javascript
<script>
function divisorCount(n)
{
let x = 0;
for (let i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i == n / i)
x++;
else
x += 2;
}
}
return x;
}
function minvalue_y(x)
{
let a = divisorCount(x);
let y = x + 1;
while ((a & 1) == (divisorCount(y) & 1))
y++;
return y;
}
let x = 5;
document.write(minvalue_y(x));
</script>
|
Time Complexity: O((1+√X)2)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved based on the following observations:
Follow the steps below to solve the problem:
- Check if X is a perfect square. If found to be true, print X + 1.
- Otherwise, print (1 + floor(√X))2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minvalue_y( int x)
{
int n = sqrt (x);
if (n * n == x)
return x + 1;
return pow (n + 1, 2);
}
int main()
{
int x = 5;
cout << minvalue_y(x) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minvalue_y( int x)
{
int n = ( int )Math.sqrt(x);
if (n * n == x)
return x + 1 ;
return ( int )Math.pow(n + 1 , 2 );
}
public static void main(String[] args)
{
int x = 5 ;
System.out.print(minvalue_y(x));
}
}
|
Python3
def minvalue_y(x):
n = int ( pow (x, 1 / 2 ))
if (n * n = = x):
return x + 1
return ( pow (n + 1 , 2 ))
if __name__ = = '__main__' :
x = 5
print (minvalue_y(x))
|
C#
using System;
class GFG{
static int minvalue_y( int x)
{
int n = ( int )Math.Sqrt(x);
if (n * n == x)
return x + 1;
return ( int )Math.Pow(n + 1, 2);
}
public static void Main()
{
int x = 5;
Console.WriteLine(minvalue_y(x));
}
}
|
Javascript
<script>
function minvalue_y(x)
{
let n = Math.floor(Math.sqrt(x));
if (n * n == x)
return x + 1;
return Math.floor(Math.pow(n + 1, 2));
}
let x = 5;
document.write(minvalue_y(x));
</script>
|
Time Complexity: O(logx) for input x because using inbuilt sqrt function
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Sep, 2022
Like Article
Save Article