Friday, October 10, 2025
HomeData Modelling & AICheck if the square of a number is divisible by K or...

Check if the square of a number is divisible by K or not

Given two integers, X and K, the task is to find if X2 is divisible by K or not. Here, both K and X can lie in the range [1,1018].

Examples:  

Input: X = 6, K = 9 
Output: YES 
Explanation: 
Since 62 is equal to 36, which is divisible by 9.

Input: X = 7, K = 21 
Output: NO 
Explanation: 
Since 72 is equal to 49, which is not divisible by 21. 
 

Approach: 
As mentioned above, X can lie in the range [1,1018], so we can not directly check if X2 is divisible by K or not as it can cause a memory overflow. Hence the efficient approach is to first calculate the Greatest Common Divisor of X and K. After the GCD is calculated, we will take it as the maximum portion which we can divide from X and K. Reduce K by GCD and check if X is divisible by the reduced K or not.

Below is the implementation of above approach: 

C++




// C++ implementation to
// check if the square of X is
// divisible by K
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return if
// square of X is divisible
// by K
void checkDivisible(int x, int k)
{
    // Finding gcd of x and k
    int g = __gcd(x, k);
 
    // Dividing k by their gcd
    k /= g;
 
    // Check for divisibility of X
    // by reduced K
    if (x % k == 0) {
        cout << "YES\n";
    }
    else {
        cout << "NO\n";
    }
}
 
// Driver Code
int main()
{
    int x = 6, k = 9;
    checkDivisible(x, k);
 
    return 0;
}


Java




// Java implementation to
// check if the square of X is
// divisible by K
 
class GFG{
 
// Function to return if
// square of X is divisible
// by K
static void checkDivisible(int x, int k)
{
    // Finding gcd of x and k
    int g = __gcd(x, k);
 
    // Dividing k by their gcd
    k /= g;
 
    // Check for divisibility of X
    // by reduced K
    if (x % k == 0)
    {
        System.out.print("YES\n");
    }
    else
    {
        System.out.print("NO\n");
    }
}
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 6, k = 9;
    checkDivisible(x, k);
}
}
 
// This code is contributed by gauravrajput1


Python3




# Python3 implementation to
# check if the square of X is
# divisible by K
 
from math import gcd
 
# Function to return if
# square of X is divisible
# by K
def checkDivisible(x, k):
     
    # Finding gcd of x and k
    g = gcd(x, k)
 
    # Dividing k by their gcd
    k //= g
 
    # Check for divisibility of X
    # by reduced K
    if (x % k == 0):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
     
    x = 6
    k = 9
    checkDivisible(x, k);
     
# This code is contributed by Bhupendra_Singh


C#




// C# implementation to check
// if the square of X is
// divisible by K
using System;
 
class GFG{
 
// Function to return if
// square of X is divisible
// by K
static void checkDivisible(int x, int k)
{
     
    // Finding gcd of x and k
    int g = __gcd(x, k);
 
    // Dividing k by their gcd
    k /= g;
 
    // Check for divisibility of X
    // by reduced K
    if (x % k == 0)
    {
        Console.Write("YES\n");
    }
    else
    {
        Console.Write("NO\n");
    }
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 6, k = 9;
     
    checkDivisible(x, k);
}
}
 
// This code is contributed by Princi Singh


Javascript




<script>
 
// Javascript implementation to
// check if the square of X is
// divisible by K
 
// Return gcd of two numbers
function gcd(a, b)
{
    return b == 0 ? a : gcd(b, a % b);
}
 
// Function to return if
// square of X is divisible
// by K
function checkDivisible(x, k)
{
     
    // Finding gcd of x and k
    var g = gcd(x, k);
 
    // Dividing k by their gcd
    k /= g;
 
    // Check for divisibility of X
    // by reduced K
    if (x % k == 0)
    {
        document.write("YES");
    }
    else
    {
        document.write("NO");
    }
}
 
// Driver code
var x = 6, k = 9;
checkDivisible(x, k);
 
// This code is contributed by Ankita saini
    
</script>


Output: 

YES

 

Time Complexity: O(log(max(x, k)))

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 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS