Count of digits to be removed to make a number divisible by 25
Last Updated :
03 Oct, 2023
Given a number N, the task is to find the minimum number of digits that needs to be removed from the number so that the number will become divisible by 25.
Input: N = 71345
Output: 3
Explanation: After removing 1, 3 and 4, the number becomes 75 and it is divisible by 25.
Input: N = 32505
Output: 1
Explanation: After removing 5 from last, number becomes 3250 and it is divisible by 25.
Approach: A number is divisible by 25 if its last two digits are “00” or the number formed by its last two digits is divisible by 25, as explained in Check if a large number is divisible by 25 or not. Now, in this problem, check this condition for all possible pairs in N and find the minimum number of digits that need to be removed. If any pair of elements is found to satisfy the above condition, then a number can be formed having these two elements as the last digits, and then it will be a multiple of 25.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDigits( int n)
{
vector< char > str;
int i = 0;
while (n != 0) {
int rem = n % 10;
char ch = (rem + '0' );
str.push_back(ch);
n /= 10;
}
reverse(str.begin(), str.end());
int ans = INT_MAX;
int N = str.size();
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int num = (str[i] - '0' )
* 10
+ (str[j] - '0' );
if (num % 25 == 0) {
int a = j - i - 1;
int b = N - (j + 1);
ans = min(ans, a + b);
}
}
}
return ans;
}
int main()
{
int n = 71345;
int ans = minDigits(n);
if (ans == INT_MAX) {
cout << -1;
}
else {
cout << ans;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minDigits( int n)
{
Vector<Character> str = new Vector<Character>();
int i = 0 ;
while (n != 0 ) {
int rem = n % 10 ;
char ch = ( char ) (rem + '0' );
str.add(ch);
n /= 10 ;
}
Collections.reverse(str);
int ans = Integer.MAX_VALUE;
int N = str.size();
for (i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
int num = (str.get(i) - '0' )
* 10
+ (str.get(j) - '0' );
if (num % 25 == 0 ) {
int a = j - i - 1 ;
int b = N - (j + 1 );
ans = Math.min(ans, a + b);
}
}
}
return ans;
}
public static void main(String[] args)
{
int n = 71345 ;
int ans = minDigits(n);
if (ans == Integer.MAX_VALUE) {
System.out.print(- 1 );
}
else {
System.out.print(ans);
}
}
}
|
Python3
def minDigits(n):
str = []
i = 0
while (n ! = 0 ):
rem = n % 10
ch = chr (rem + ord ( '0' ))
str .append(ch)
n = (n / / 10 )
str .reverse()
ans = 10 * * 9
N = len ( str )
for i in range (N):
for j in range (i + 1 , N):
num = ( ord ( str [i]) - ord ( '0' )) * 10 + ( ord ( str [j]) - ord ( '0' ))
if (num % 25 = = 0 ):
a = j - i - 1
b = N - (j + 1 )
ans = min (ans, a + b)
return ans
n = 71345
ans = minDigits(n)
if (ans = = 10 * * 9 ):
print ( - 1 )
else :
print (ans)
|
C#
using System;
using System.Collections;
class GFG
{
static int minDigits( int n)
{
ArrayList str = new ArrayList();
while (n != 0) {
int rem = n % 10;
char ch = ( char )(rem + '0' );
str.Add(ch);
n /= 10;
}
str.Reverse();
int ans = Int32.MaxValue;
int N = str.Count;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int num = (( char )str[i] - '0' )
* 10
+ (( char )str[j] - '0' );
if (num % 25 == 0) {
int a = j - i - 1;
int b = N - (j + 1);
ans = Math.Min(ans, a + b);
}
}
}
return ans;
}
public static void Main()
{
int n = 71345;
int ans = minDigits(n);
if (ans == Int32.MaxValue) {
Console.Write(-1);
}
else {
Console.Write(ans);
}
}
}
|
Javascript
<script>
function minDigits(n)
{
let str = [];
let i = 0;
while (n != 0) {
let rem = n % 10;
let ch = String.fromCharCode(rem + '0' .charCodeAt(0));
str.push(ch);
n = Math.floor(n / 10)
}
str.reverse();
let ans = Number.MAX_VALUE;
let N = str.length;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
let num = (str[i].charCodeAt(0) - '0' .charCodeAt(0))
* 10
+ (str[j].charCodeAt(0) - '0' .charCodeAt(0));
if (num % 25 == 0) {
let a = j - i - 1;
let b = N - (j + 1);
ans = Math.min(ans, a + b);
}
}
}
return ans;
}
let n = 71345;
let ans = minDigits(n);
if (ans == Number.MAX_VALUE) {
document.write(-1);
}
else {
document.write(ans);
}
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...