Check Divisibility by 999

Last Updated : 22 Jul, 2026

Given a large integer n represented as a string, check whether it is divisible by 999. You are not allowed to directly divide the number by 999 or compute n % 999. Return true if n is divisible by 999; otherwise, return false.

Examples: 

Input: n = "1998"
Output: true
Explanation: 1998 is divisible by 999.

Input: n = "99999999"
Output: false
Explanation: 99999999 is not divisible by 999.

Try It Yourself
redirect icon

[Naive Approach] Convert to Integer (Causes Overflow) - O(n) Time and O(1) Space

The idea is to convert the given string into an integer and then check whether it is divisible by 999 using the modulo operator. This approach is only suitable for small numbers since very large integers cannot fit into standard data types.

C++
#include <iostream>
#include <string>
using namespace std;

bool isDivisible999(string &n)
{

    // Convert the string into an integer.
    long long num = 0;

    for (char ch : n)
        num = num * 10 + (ch - '0');

    // Check divisibility by 999.
    return num % 999 == 0;
}

int main()
{
    string n = "1998";

    if (isDivisible999(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.util.*;

public class GFG {
    // Method to check if a number represented as a string
    // is divisible by 999
    public static boolean isDivisible999(String n)
    {
        // Convert the string into an integer.
        long num = 0;
        for (char ch : n.toCharArray())
            num = num * 10 + (ch - '0');
        // Check divisibility by 999.
        return num % 999 == 0;
    }

    public static void main(String[] args)
    {
        String n = "1998";
        if (isDivisible999(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
"""
Function to check if a number represented as a string is divisible by 999
"""


def isDivisible999(n):
    # Convert the string into an integer.
    num = 0
    for ch in n:
        num = num * 10 + (ord(ch) - ord('0'))
    # Check divisibility by 999.
    return num % 999 == 0


if __name__ == '__main__':
    n = "1998"
    if isDivisible999(n):
        print('true')
    else:
        print('false')
C#
using System;

public class GFG {
    // Method to check if a number represented as a string
    // is divisible by 999
    public static bool isDivisible999(string n)
    {
        // Convert the string into an integer.
        long num = 0;
        foreach(char ch in n) num = num * 10 + (ch - '0');
        // Check divisibility by 999.
        return num % 999 == 0;
    }

    public static void Main()
    {
        string n = "1998";
        if (isDivisible999(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// Function to check if a number represented as a string is
// divisible by 999
function isDivisible999(n)
{
    // Convert the string into an integer.
    let num = 0;
    for (let ch of n) {
        num = num * 10
              + (ch.charCodeAt(0) - "0".charCodeAt(0));
    }
    // Check divisibility by 999.
    return num % 999 === 0;
}

// Driver Code
let n = "1998";
if (isDivisible999(n)) {
    console.log("true");
}
else {
    console.log("false");
}

Output
true

[Expected Approach] Repeated 3-Digit Group Reduction - O(n) Time and O(n) Space

The idea is to repeatedly divide the number into groups of three digits, add all the groups, and replace the original number with the obtained sum. Continue this process until the remaining value has at most three digits, then check whether it is 0 or 999.

Let us understand with an example:
Input: n = "1998"

  • Since its length is not a multiple of 3, pad one leading zero to get "001998".
  • Split the string into 3-digit groups: "001" and "998". Their sum is 1 + 998 = 999.
  • Replace n with "999". Now the length is at most 3, so stop further reductions.
  • Convert "999" to an integer and check whether it is 0 or 999.
  • Since the value is 999, return true.

How does this work? 

Let us consider 235764, we can write it as 235764 = 2*105 + 3*104 + 5*103 + 7*102 + 6*10 + 4
The idea is based on below observation: Remainder of 103 divided by 999 is 1 For i > 3, 10i % 999 = 10i-3 % 999
Let us see how we use above fact. Remainder of 2*105 + 3*104 + 5*103 + 7*102 + 6*10 + 4
Remainder with 999 can be written as : 2*100 + 3*10 + 5*1 + 7*100 + 6*10 + 4
The above expression is basically sum of groups of size 3.
Since the sum is divisible by 999, answer is yes.

C++
#include <iostream>
#include <string>
using namespace std;

bool isDivisible999(string &n)
{

    // A single zero is divisible by every non-zero number.
    if (n == "0")
        return true;

    // Keep reducing until the value has at most three digits.
    while (n.length() > 3)
    {

        // Pad leading zeros so that the length becomes a multiple of 3.
        while (n.length() % 3 != 0)
            n = "0" + n;

        int groupSum = 0;

        // Add all 3-digit groups.
        for (int i = 0; i < (int)n.length(); i += 3)
        {
            int group = (n[i] - '0') * 100 + (n[i + 1] - '0') * 10 + (n[i + 2] - '0');

            groupSum += group;
        }

        // Continue with the obtained sum.
        n = to_string(groupSum);
    }

    // Convert the remaining number into an integer.
    int value = stoi(n);

    // Check divisibility without using the original number.
    return (value == 0 || value == 999);
}

int main()
{
    string n = "1998";

    if (isDivisible999(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static boolean isDivisible999(String n)
    {

        // A single zero is divisible by every non-zero
        // number.
        if (n.equals("0"))
            return true;

        // Keep reducing until the value has at most three
        // digits.
        while (n.length() > 3) {

            // Pad leading zeros so that the length becomes
            // a multiple of 3.
            while (n.length() % 3 != 0)
                n = "0" + n;

            int groupSum = 0;

            // Add all 3-digit groups.
            for (int i = 0; i < n.length(); i += 3) {
                int group = (n.charAt(i) - '0') * 100
                            + (n.charAt(i + 1) - '0') * 10
                            + (n.charAt(i + 2) - '0');

                groupSum += group;
            }

            // Continue with the obtained sum.
            n = String.valueOf(groupSum);
        }

        // Convert the remaining number into an integer.
        int value = Integer.parseInt(n);

        // Check divisibility without using the original
        // number.
        return (value == 0 || value == 999);
    }

    public static void main(String[] args)
    {
        String n = "1998";

        if (isDivisible999(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
def isDivisible999(n):

    # A single zero is divisible by every non-zero number.
    if n == "0":
        return True

    # Keep reducing until the value has at most three digits.
    while len(n) > 3:

        # Pad leading zeros so that the length becomes a multiple of 3.
        while len(n) % 3 != 0:
            n = "0" + n

        groupSum = 0

        # Add all 3-digit groups.
        for i in range(0, len(n), 3):
            group = (int(n[i]) * 100) + (int(n[i + 1]) * 10) + int(n[i + 2])

            groupSum += group

        # Continue with the obtained sum.
        n = str(groupSum)

    # Convert the remaining number into an integer.
    value = int(n)

    # Check divisibility without using the original number.
    return (value == 0 or value == 999)


if __name__ == "__main__":
    n = "1998"

    if isDivisible999(n):
        print("true")
    else:
        print("false")
C#
using System;

class GFG {
    static bool isDivisible999(string n)
    {

        // A single zero is divisible by every non-zero
        // number.
        if (n == "0")
            return true;

        // Keep reducing until the value has at most three
        // digits.
        while (n.Length > 3) {

            // Pad leading zeros so that the length becomes
            // a multiple of 3.
            while (n.Length % 3 != 0)
                n = "0" + n;

            int groupSum = 0;

            // Add all 3-digit groups.
            for (int i = 0; i < n.Length; i += 3) {
                int group = (n[i] - '0') * 100
                            + (n[i + 1] - '0') * 10
                            + (n[i + 2] - '0');

                groupSum += group;
            }

            // Continue with the obtained sum.
            n = groupSum.ToString();
        }

        // Convert the remaining number into an integer.
        int value = int.Parse(n);

        // Check divisibility without using the original
        // number.
        return (value == 0 || value == 999);
    }

    static void Main()
    {
        string n = "1998";

        if (isDivisible999(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
function isDivisible999(n)
{

    // A single zero is divisible by every non-zero number.
    if (n === "0")
        return true;

    // Keep reducing until the value has at most three
    // digits.
    while (n.length > 3) {

        // Pad leading zeros so that the length becomes a
        // multiple of 3.
        while (n.length % 3 !== 0)
            n = "0" + n;

        let groupSum = 0;

        // Add all 3-digit groups.
        for (let i = 0; i < n.length; i += 3) {
            const group
                = (n[i].charCodeAt(0) - "0".charCodeAt(0))
                      * 100
                  + (n[i + 1].charCodeAt(0)
                     - "0".charCodeAt(0))
                        * 10
                  + (n[i + 2].charCodeAt(0)
                     - "0".charCodeAt(0));

            groupSum += group;
        }

        // Continue with the obtained sum.
        n = groupSum.toString();
    }

    // Convert the remaining number into an integer.
    const value = parseInt(n, 10);

    // Check divisibility without using the original number.
    return (value === 0 || value === 999);
}

// Driver Code
const n = "1998";

if (isDivisible999(n))
    console.log("true");
else
    console.log("false");

Output
true

Comment