Minimum and maximum number of digits required to be removed to make a given number divisible by 3
Given a numeric string S, the task is to find the minimum and the maximum number of digits that must be removed from S such that it is divisible by 3. If it is impossible to do so, then print “-1”.
Examples:
Input: S = "12345" Output: Minimum: 0 Maximum: 4 Explanation: The given number 12345 is divisible by 3. Therefore, the minimum digits required to be removed to make it divisible by 3 is 0. After removing digits 1, 2, 4 and 5, only 3 remains, which is divisible by 3. Therefore, the maximum number of digits required to be removed is 4.
Input: S = "44" Output: Minimum: -1 Maximum: -1
Approach: The problem can be solved based on the observation that for any number to be divisible by 3, the sum of digits must also be divisible by 3. Follow the steps below to solve this problem:
- Insert each digit of the given string into an array, say arr[], as (S[i] – ‘0’) % 3.
- Find the count of 0s, 1s, and 2s in the array arr[].
- Then, calculate the sum of digits, i.e. (arr[0] % 3) + (arr[1] % 3) + (arr[2] % 3)….. Update sum = sum % 3.
- Depending on the value of sum, the following three cases arise:
- If sum = 0: Minimum number of digits required to be removed is 0.
- If sum = 1: Those digits should be removed whose sum gives a remainder 1 on dividing by 3. Therefore, the following situations need to be considered:
- If the count of 1s is greater than or equal to 1, then the minimum number of digits required to be removed is 1.
- If the count of 2s is greater than or equal to 2, then the minimum number of digits required to be removed will be 2 [Since (2 + 2) % 3 = 1].
- If sum = 2: Those digits should be removed whose sum gives a remainder 2 on dividing by 3. Therefore, the following situations arise:
- If the count of 2s is greater than or equal to 1, then the minimum number of digits required to be removed will be 1.
- Otherwise, if the count of 1s is greater than or equal to 2, then the minimum number of digits to be removed will be 2 [(1 + 1) % 3 = 2].
- For finding the maximum number of digits to be removed, the following three cases need to be considered:
- If the count of 0s is greater than or equal to 1, then the maximum digits required to be removed will be (number of digits – 1).
- If both the count of 1s and 2s are greater than or equal to 1, then the maximum digits required to be removed will be (number of digits – 2) [(1+2) % 3 = 0].
- If the count of either 1s or 2s is greater than or equal to 3, then maximum digits required to be removed will be (number of digits – 3) [(1+1+1) % 3 = 0, (2+2+2) % 3 = 0].
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum and // minimum number of digits to be // removed to make str divisible by 3 void minMaxDigits(string str, int N) { // Convert the string into // array of digits int arr[N]; for ( int i = 0; i < N; i++) arr[i] = (str[i] - '0' ) % 3; // Count of 0s, 1s, and 2s int zero = 0, one = 0, two = 0; // Traverse the array for ( int i = 0; i < N; i++) { if (arr[i] == 0) zero++; if (arr[i] == 1) one++; if (arr[i] == 2) two++; } // Find the sum of digits % 3 int sum = 0; for ( int i = 0; i < N; i++) { sum = (sum + arr[i]) % 3; } // Cases to find minimum number // of digits to be removed if (sum == 0) { cout << 0 << ' ' ; } if (sum == 1) { if (one && N > 1) cout << 1 << ' ' ; else if (two > 1 && N > 2) cout << 2 << ' ' ; else cout << -1 << ' ' ; } if (sum == 2) { if (two && N > 1) cout << 1 << ' ' ; else if (one > 1 && N > 2) cout << 2 << ' ' ; else cout << -1 << ' ' ; } // Cases to find maximum number // of digits to be removed if (zero > 0) cout << N - 1 << ' ' ; else if (one > 0 && two > 0) cout << N - 2 << ' ' ; else if (one > 2 || two > 2) cout << N - 3 << ' ' ; else cout << -1 << ' ' ; } // Driver Code int main() { string str = "12345" ; int N = str.length(); // Function Call minMaxDigits(str, N); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to find the maximum and // minimum number of digits to be // removed to make str divisible by 3 static void minMaxDigits(String str, int N) { // Convert the string into // array of digits int arr[] = new int [N]; for ( int i = 0 ; i < N; i++) arr[i] = (str.charAt(i) - '0' ) % 3 ; // Count of 0s, 1s, and 2s int zero = 0 , one = 0 , two = 0 ; // Traverse the array for ( int i = 0 ; i < N; i++) { if (arr[i] == 0 ) zero++; if (arr[i] == 1 ) one++; if (arr[i] == 2 ) two++; } // Find the sum of digits % 3 int sum = 0 ; for ( int i = 0 ; i < N; i++) { sum = (sum + arr[i]) % 3 ; } // Cases to find minimum number // of digits to be removed if (sum == 0 ) { System.out.print( 0 + " " ); } if (sum == 1 ) { if ((one != 0 ) && (N > 1 )) System.out.print( 1 + " " ); else if (two > 1 && N > 2 ) System.out.print( 2 + " " ); else System.out.print(- 1 + " " ); } if (sum == 2 ) { if (two != 0 && N > 1 ) System.out.print( 1 + " " ); else if (one > 1 && N > 2 ) System.out.print( 2 + " " ); else System.out.print(- 1 + " " ); } // Cases to find maximum number // of digits to be removed if (zero > 0 ) System.out.print(N - 1 + " " ); else if (one > 0 && two > 0 ) System.out.print(N - 2 + " " ); else if (one > 2 || two > 2 ) System.out.print(N - 3 + " " ); else System.out.print(- 1 + " " ); } // Driver code public static void main(String[] args) { String str = "12345" ; int N = str.length(); // Function Call minMaxDigits(str, N); } } // This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approach # Function to find the maximum and # minimum number of digits to be # removed to make str divisible by 3 def minMaxDigits( str , N): # Convert the string into # array of digits arr = [ 0 ] * N for i in range (N): arr[i] = ( ord ( str [i]) - ord ( '0' )) % 3 # Count of 0s, 1s, and 2s zero = 0 one = 0 two = 0 # Traverse the array for i in range (N): if (arr[i] = = 0 ): zero + = 1 if (arr[i] = = 1 ): one + = 1 if (arr[i] = = 2 ): two + = 1 # Find the sum of digits % 3 sum = 0 for i in range (N): sum = ( sum + arr[i]) % 3 # Cases to find minimum number # of digits to be removed if ( sum = = 0 ): print ( "0" , end = " " ) if ( sum = = 1 ): if (one and N > 1 ): print ( "1" , end = " " ) elif (two > 1 and N > 2 ): print ( "2" , end = " " ) else : print ( "-1" , end = " " ) if ( sum = = 2 ): if (two and N > 1 ): print ( "1" , end = " " ) elif (one > 1 and N > 2 ): print ( "2" , end = " " ) else : print ( "-1" , end = " " ) # Cases to find maximum number # of digits to be removed if (zero > 0 ): print (N - 1 , end = " " ) elif (one > 0 and two > 0 ): print (N - 2 , end = " " ) elif (one > 2 or two > 2 ): print (N - 3 , end = " " ) else : print ( "-1" , end = " " ) # Driver Code str = "12345" N = len ( str ) # Function Call minMaxDigits( str , N) # This code is contributed by susmitakundugoaldanga |
C#
// C# program for the above approach using System; class GFG{ // Function to find the maximum and // minimum number of digits to be // removed to make str divisible by 3 static void minMaxDigits( string str, int N) { // Convert the string into // array of digits int [] arr = new int [N]; for ( int i = 0; i < N; i++) arr[i] = (str[i] - '0' ) % 3; // Count of 0s, 1s, and 2s int zero = 0, one = 0, two = 0; // Traverse the array for ( int i = 0; i < N; i++) { if (arr[i] == 0) zero++; if (arr[i] == 1) one++; if (arr[i] == 2) two++; } // Find the sum of digits % 3 int sum = 0; for ( int i = 0; i < N; i++) { sum = (sum + arr[i]) % 3; } // Cases to find minimum number // of digits to be removed if (sum == 0) { Console.Write(0 + " " ); } if (sum == 1) { if ((one != 0) && (N > 1)) Console.Write(1 + " " ); else if (two > 1 && N > 2) Console.Write(2 + " " ); else Console.Write(-1 + " " ); } if (sum == 2) { if (two != 0 && N > 1) Console.Write(1 + " " ); else if (one > 1 && N > 2) Console.Write(2 + " " ); else Console.Write(-1 + " " ); } // Cases to find maximum number // of digits to be removed if (zero > 0) Console.Write(N - 1 + " " ); else if (one > 0 && two > 0) Console.Write(N - 2 + " " ); else if (one > 2 || two > 2) Console.Write(N - 3 + " " ); else Console.Write(-1 + " " ); } // Driver code public static void Main() { string str = "12345" ; int N = str.Length; // Function Call minMaxDigits(str, N); } } // This code is contributed by code_hunt |
Javascript
<script> // Javascript program for the above approach // Function to find the maximum and // minimum number of digits to be // removed to make str divisible by 3 function minMaxDigits(str, N) { // Convert the string into // array of digits let arr = []; for (let i = 0; i < N; i++) arr[i] = (str[i] - '0' ) % 3; // Count of 0s, 1s, and 2s let zero = 0, one = 0, two = 0; // Traverse the array for (let i = 0; i < N; i++) { if (arr[i] == 0) zero++; if (arr[i] == 1) one++; if (arr[i] == 2) two++; } // Find the sum of digits % 3 let sum = 0; for (let i = 0; i < N; i++) { sum = (sum + arr[i]) % 3; } // Cases to find minimum number // of digits to be removed if (sum == 0) { document.write(0 + " " ); } if (sum == 1) { if ((one != 0) && (N > 1)) document.write(1 + " " ); else if (two > 1 && N > 2) document.write(2 + " " ); else document.write(-1 + " " ); } if (sum == 2) { if (two != 0 && N > 1) document.write(1 + " " ); else if (one > 1 && N > 2) document.write(2 + " " ); else document.write(-1 + " " ); } // Cases to find maximum number // of digits to be removed if (zero > 0) document.write(N - 1 + " " ); else if (one > 0 && two > 0) document.write(N - 2 + " " ); else if (one > 2 || two > 2) document.write(N - 3 + " " ); else document.write(-1 + " " ); } // Driver code let str = "12345" ; let N = str.length; // Function Call minMaxDigits(str, N); // This code is contributed by avijitmondal1998 </script> |
Output:
0 4
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)
Please Login to comment...