What least number should be replaced for * so that number 372 * 0 is exactly divisible by 3?
View Discussion Show
Improve Article Save Article 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).
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; } Javaimport 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)); } } Python3def 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)); } } PHPfunction 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); ?> Javascriptfunction divisible(num) { let n = num.length; let sum = 0; for (let i = 0; i < n; i++) sum += (num.charAt(i)); if (sum % 3 == 0) return 0; if (n == 1) return -1; for (let i = 0; i < n; i++) if (sum % 3 == (num.charAt(i) - '0') % 3) return 1; if (n == 2) return -1; return 2; } let num = "1234"; document.write(divisible(num)); Output : 1Time 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. 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.
|