What least number should be replaced for * so that number 372 * 0 is exactly divisible by 3?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a very large number num (1 <= num <= 10^1000), print the number of digits that needs to be removed to make the number exactly divisible by 3. If it is not possible then print -1.

    Examples : 

    Input: num = "1234" Output: 1 Explanation: we need to remove one digit that is 1 or 4, to make the number divisible by 3.on Input: num = "11" Output: -1 Explanation: It is not possible to remove any digits and make it divisible by 3.

    The idea is based on the fact that a number is multiple of 3 if and only if sum of its digits is multiple of 3 (See this for details).
    One important observation used here is that the answer is at most 2 if an answer exists. So here are the only options for the function: 

    1. Sum of digits is already equal to 0 modulo 3. Thus, we don’t have to erase any digits.
    2. There exists such a digit that equals sum modulo 3. Then we just have to erase a single digit
    3. All the digits are neither divisible by 3 nor equal to sum modulo 3. So two of such digits will sum up to number, which equals sum modulo 3, (2+2) mod 3=1, (1+1) mod 3=2

    C++

    #include

    using namespace std;

    int divisible(string num)

    {

        int n = num.length();

        int sum = accumulate(begin(num),

                             end(num), 0) - '0' * 1;

        if (sum % 3 == 0)

            return 0;

        if (n == 1)

            return -1;

        for (int i = 0; i < n; i++)

            if (sum % 3 == (num[i] - '0') % 3)

                return 1;

        if (n == 2)

            return -1;

        return 2;

    }

    int main()

    {

        string num = "1234";

        cout << divisible(num);

        return 0;

    }

    Java

    import java.io.*;

    class GFG {

        static int divisible(String num)

        {

            int n = num.length();

            int sum = 0;

            for (int i = 0; i < n; i++)

                sum += (int)(num.charAt(i));

            if (sum % 3 == 0)

                return 0;

            if (n == 1)

                return -1;

            for (int i = 0; i < n; i++)

                if (sum % 3 == (num.charAt(i) - '0') % 3)

                    return 1;

            if (n == 2)

                return -1;

            return 2;

        }

        public static void main(String[] args)

        {

            String num = "1234";

            System.out.println(divisible(num));

        }

    }

    Python3

    def divisible(num):

        n = len(num)

        sum_ = 0

        for i in range(n):

            sum_ += int(num[i])

        if (sum_ % 3 == 0):

            return 0

        if (n == 1):

            return -1

        for i in range(n):

            if (sum_ % 3 == int(num[i]) % 3):

                return 1

        if (n == 2):

            return -1

        return 2

    if __name__ == '__main__':

        num = "1234"

        print(divisible(num))

    C#

    using System;

    class GFG {

        static int divisible(String num)

        {

            int n = num.Length;

            int sum = 0;

            for (int i = 0; i < n; i++)

                sum += (int)(num[i]);

            if (sum % 3 == 0)

                return 0;

            if (n == 1)

                return -1;

            for (int i = 0; i < n; i++)

                if (sum % 3 == (num[i] - '0') % 3)

                    return 1;

            if (n == 2)

                return -1;

            return 2;

        }

        public static void Main()

        {

            string num = "1234";

            Console.WriteLine(divisible(num));

        }

    }

    PHP

    function divisible($num)

    {

        $n = strlen($num);

        $sum = ($num); ($num); 0 - '0';

        if ($sum % 3 == 0)

            return 0;

        if ($n == 1)

            return -1;

        for ($i = 0; $i < $n; $i++)

            if ($sum % 3 == ($num[$i] - '0') % 3)

                return 1;        

        if ($n == 2)

            return -1;

        return 2;

    }

    $num = "1234";

    echo divisible($num);

    ?>

    Javascript

    Output : 

    1

    Time Complexity: O(N), as we are using a loop to traverse N times.

    Auxiliary Space: O(1), as we are not using any extra space.

    This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    What least number should be replace for * so that the number 67301 * 2 is exactly divisible by 9?

    So the least number that can be replaced by $ * $ so that $67301 * 2$ is exactly divisible by nine is $8$. $\therefore $ The answer is $8$. Note: Certain numbers like nine have divisibility rules. So we can find the missing digits using them.

    What least number should be replaced for * so that the number 9 * 8071 is exactly divisible by 11?

    Answer. Answer: The missing digit is 1. Then, the answer will be 83461.

    What least number should be replaced by so that the number 376 102 is exactly divisible by 9?

    So 8 is the final answer.

    What least number should be replaced for * so that the number 467 * 91 is exactly divisible by 11?

    Answer: 2 is the answer.