Open In App

Minimum value exceeding X whose count of divisors has different parity with count of divisors of X

Last Updated : 17 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count divisors of n
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;
}
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
int minvalue_y(int x)
{
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
 
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1)
           == (divisorCount(y) & 1))
        y++;
    return y;
}
 
// Driver Code
int main()
{
    // Given X
    int x = 5;
 
    // Function call
    cout << minvalue_y(x) << endl;
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count divisors of n
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;
}
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
 
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1) == (divisorCount(y) & 1))
        y++;
         
    return y;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given X
    int x = 5;
 
    // Function call
    System.out.println(minvalue_y(x));
}
}
 
// This code is contributed by chitranayal


C#




// C# program for the above approach
using System;
  
class GFG{
  
// Function to count divisors of n
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;
}
  
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
      
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
  
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1) == (divisorCount(y) & 1))
        y++;
          
    return y;
}
  
// Driver Code
public static void Main()
{
      
    // Given X
    int x = 5;
  
    // Function call
    Console.WriteLine(minvalue_y(x));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3




# Python program for the above approach
 
# Function to count divisors of n
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;
 
# Function to find the minimum
# value exceeding x whose count
# of divisors has different parity
# with count of divisors of X
def minvalue_y(x):
   
    # Divisor count of x
    a = divisorCount(x);
    y = x + 1;
 
    # Iterate from x + 1 and
    # check for each element
    while ((a & 1) == (divisorCount(y) & 1)):
        y += 1;
 
    return y;
 
# Driver Code
if __name__ == '__main__':
   
    # Given X
    x = 5;
 
    # Function call
    print(minvalue_y(x));
 
# This code is contributed by 29AjayKumar


Javascript




<script>
 
// javascript program of the above approach
 
// Function to count divisors of n
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 to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
function minvalue_y(x)
{
     
    // Divisor count of x
    let a = divisorCount(x);
    let y = x + 1;
 
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1) == (divisorCount(y) & 1))
        y++;
         
    return y;
}
 
    // Driver Code
     
     // Given X
    let x = 5;
 
    // Function call
    document.write(minvalue_y(x));
 
// This code is contributed by target_2.
</script>


Output: 

9

 

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:

  1. Check if X is a perfect square. If found to be true, print X + 1.
  2. Otherwise, print (1 + floor(√X))2).

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
int minvalue_y(int x)
{
    // Check if x is
    // perfect square
    int n = sqrt(x);
    if (n * n == x)
        return x + 1;
 
    return pow(n + 1, 2);
}
 
// Driver Code
int main()
{
    int x = 5;
    cout << minvalue_y(x) << endl;
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.sqrt(x);
    if (n * n == x)
        return x + 1;
  
    return (int)Math.pow(n + 1, 2);
}
   
// Driver Code
public static void main(String[] args)
{
    int x = 5;
      
    System.out.print(minvalue_y(x));
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the above approach
 
# Function to find the minimum
# value exceeding x whose count
# of divisors has different parity
# with count of divisors of X
def minvalue_y(x):
     
    # Check if x is
    # perfect square
    n = int(pow(x, 1 / 2))
     
    if (n * n == x):
        return x + 1
         
    return(pow(n + 1, 2))
 
# Driver Code
if __name__ == '__main__':
     
    x = 5
 
    print(minvalue_y(x))
 
# This code is contributed by Rajput-Ji


C#




// C# program for the above approach
using System;
     
class GFG{
     
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.Sqrt(x);
    if (n * n == x)
        return x + 1;
         
    return (int)Math.Pow(n + 1, 2);
}
    
// Driver Code
public static void Main()
{
    int x = 5;
     
    Console.WriteLine(minvalue_y(x));
}
}
 
// This code is contributed by code_hunt


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
function minvalue_y(x)
{
      
    // Check if x is
    // perfect square
    let n = Math.floor(Math.sqrt(x));
    if (n * n == x)
        return x + 1;
   
    return Math.floor(Math.pow(n + 1, 2));
}
 
// Driver code
 
    let x = 5;
       
    document.write(minvalue_y(x));
     
</script>


Output: 

9

 

Time Complexity: O(logx) for input x because using inbuilt sqrt function
Auxiliary Space: O(1)

 



Similar Reads

Parity of count of letters whose position and frequency have same parity
Given a string S of lowercase English characters, find out whether the number of the letters whose frequencies in the string have the same parity as their positions in the English alphabet is odd or even (i.e., they have an odd frequency in the string and their position is at an odd number in the English alphabet or have even frequency and their po
8 min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
Given four integers N, M, X and Y, the task is to construct a N * M matrix such that each cell consists of a value in the range [0, X] such that sum of any two adjacent cells should be less than or equal to Y and the total sum of the matrix should be maximum. Example: Input: N = 3, M = 3, X = 5, Y = 3 Output: 3 0 3 0 3 0 3 0 3 Explanation: All the
7 min read
Minimum operations required to modify the array such that parity of adjacent elements is different
Given an array A[], the task is to find the minimum number of operations required to convert the array into B[] such that for every index in B (except the last) parity(b[i]) != parity(b[i + 1]) where parity(x) = x % 3. Below is the operation to be performed: Any element from the set {1, 2} can be added to any element of the array. Examples: Input:
9 min read
Minimum integer that can be obtained by swapping adjacent digits of different parity
Given an integer N, the task is to find the minimum integer that can be obtained from the given integer such that the adjacent digits of different parity can be swapped any no of times. Two digits of different parity means that they will have different remainders when divided by two.Examples: Input: N = 64432 Output: 36442 Explanation: Swap the 4th
7 min read
Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
Given a binary array arr[] of length N, the task is to find the minimum flips required in the array such that XOR of a consecutive sub-arrays of size K have different parity.Examples: Input: arr[] = {0, 1, 0, 1, 1}, K = 2 Output: 2 Explanation: For the above given array XOR of consecutive sub-array of size 2 is: {(0, 1): 1, (1, 0): 1, (0, 1): 1, (1
9 min read
Check if a given number N has at least one odd divisor not exceeding N - 1
Given a positive integer N, the task is to check if the given number N has at least 1 odd divisor from the range [2, N - 1] or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: N = 10Output: YesExplanation:10 has 5 as the odd divisor. Therefore, print Yes. Input: N = 8Output: No Approach: The idea to solve the give
5 min read
Count the nodes in the given tree whose weight is even parity
Given a tree and the weights of all the nodes, the task is to count the number of nodes whose weights are even parity i.e. whether the count of set bits in them is even.Examples: Input: Output: 3 WeightBinary RepresentationParity50101Even101010Even111011Odd81000Odd60110Even Approach: Perform dfs on the tree and for every node, check if its weight i
6 min read
Minimize the count of adjacent pairs with different parity
Given an array arr of size N containing some integers from the range [1, N] and -1 in the remaining indices, the task is to replace -1 by the remaining integers from [1, N] such that the count of pairs of adjacent elements with different parity is minimized. Examples: Input: arr = {-1, 5, -1, 2, 3} Output: 2 Explanation: After replacing the element
10 min read
Count of all subsequences having adjacent elements with different parity
Given an array arr[] of size N, the task is to find the number of non-empty subsequences from the given array such that no two adjacent elements of the subsequence have the same parity. Examples: Input: arr[] = [5, 6, 9, 7] Output: 9 Explanation: All such subsequences of given array will be {5}, {6}, {9}, {7}, {5, 6}, {6, 7}, {6, 9}, {5, 6, 9}, {5,
9 min read
Practice Tags :