Minimal worth exceeding X whose depend of divisors has totally different parity with depend of divisors of X

by admin

Given an integer X, the duty is to find out the minimal worth of Y higher than X, such that count of divisors of X and Y have different parities.

Examples:

Enter: X = 5
Output: 9
Clarification: The depend of divisors of 5 and 9 are 2 and three respectively, that are of various parities.

Enter: X = 9
Output: 10
Clarification: The counts of divisors of 9 and 10 are three and 4, that are of various parities.

 

Naive Strategy: The only strategy to unravel the issue is to iterate every quantity ranging from X + 1 till a component with depend of the divisors with parity reverse to that of X is obtained.

Time Complexity: O((1+√X)2)
Auxiliary Area: O(1)

Beneath is the implementation of the above strategy:

C++

 

#embody <bits/stdc++.h>

utilizing 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;

 

    

    

    whereas ((a & 1)

           == (divisorCount(y) & 1))

        y++;

    return y;

}

 

int most important()

{

    

    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;

 

    

    

    whereas ((a & 1) == (divisorCount(y) & 1))

        y++;

         

    return y;

}

 

public static void most important(String[] args)

{

     

    

    int x = 5;

 

    

    System.out.println(minvalue_y(x));

}

}

 

Environment friendly Strategy: The issue might be solved primarily based on the next observations:

Observe the steps under to unravel the issue:

  1. Check if X is a perfect square. If discovered to be true, print X + 1.
  2. In any other case, print (1 + ground(X))2).

Beneath is the implementation of the above strategy:

C++

 

#embody <bits/stdc++.h>

utilizing namespace std;

 

int minvalue_y(int x)

{

    

    

    int n = sqrt(x);

    if (n * n == x)

        return x + 1;

 

    return pow(n + 1, 2);

}

 

int most important()

{

    int x = 5;

    cout << minvalue_y(x) << endl;

    return 0;

}

Time Complexity: O(1)
Auxiliary Area: O(1)

 

Consideration reader! Don’t cease studying now. Pay money for all of the necessary DSA ideas with the DSA Self Paced Course at a student-friendly value and develop into trade prepared.



In the event you like GeeksforGeeks and wish to contribute, you can even write an article utilizing contribute.geeksforgeeks.org or mail your article to [email protected] See your article showing on the GeeksforGeeks most important web page and assist different Geeks.

Please Enhance this text should you discover something incorrect by clicking on the “Enhance Article” button under.




Related Posts

Leave a Comment