Minimum number of given moves required to make N divisible by 25
Given a number N(1 ? N ? 1018) without leading zeros. The task is to find the minimum number of moves required to make N divisible by 25. At each move, one can swap any two adjacent digits and make sure that at any time number must not contain any leading zeros. If it is not possible to make N divisible by 25 then print -1.
Examples:
Input: N = 7560
Output: 1
swap(5, 6) and N becomes 7650 which is divisible by 25
Input: N = 100
Output: 0
Approach: Iterate over all pairs of digits in the number. Let the first digit in the pair is at position i and the second is at position j. Let’s place these digits to the last two positions in the number. But, now the number can contain a leading zero. Find the leftmost non-zero digit and move it to the first position. Then if the current number is divisible by 25 try to update the answer with the number of swaps. The minimum number of swaps across all of these operations is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minMoves( long long n)
{
string s = to_string(n);
int ans = INT_MAX;
int len = s.size();
for ( int i = 0; i < len; ++i) {
for ( int j = 0; j < len; ++j) {
if (i == j)
continue ;
string t = s;
int cur = 0;
for ( int k = i; k < len - 1; ++k) {
swap(t[k], t[k + 1]);
++cur;
}
for ( int k = j - (j > i); k < len - 2; ++k) {
swap(t[k], t[k + 1]);
++cur;
}
int pos = -1;
for ( int k = 0; k < len; ++k) {
if (t[k] != '0' ) {
pos = k;
break ;
}
}
for ( int k = pos; k > 0; --k) {
swap(t[k], t[k - 1]);
++cur;
}
long long nn = atoll(t.c_str());
if (nn % 25 == 0)
ans = min(ans, cur);
}
}
if (ans == INT_MAX)
return -1;
return ans;
}
int main()
{
long long n = 509201;
cout << minMoves(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minMoves( int n)
{
String s = Integer.toString(n);
int ans = Integer.MAX_VALUE;
int len = s.length();
for ( int i = 0 ; i < len; ++i)
{
for ( int j = 0 ; j < len; ++j)
{
if (i == j)
continue ;
char t [] = s.toCharArray();
int cur = 0 ;
for ( int k = i; k < len - 1 ; ++k)
{
swap(t, k, k + 1 );
++cur;
}
for ( int k = j - ((j > i)? 1 : 0 ); k < len - 2 ; ++k)
{
swap(t, k, k + 1 );
++cur;
}
int pos = - 1 ;
for ( int k = 0 ; k < len; ++k)
{
if (t[k] != '0' )
{
pos = k;
break ;
}
}
for ( int k = pos; k > 0 ; --k)
{
swap(t, k, k - 1 );
++cur;
}
long nn = Integer.parseInt(String.valueOf(t));
if (nn % 25 == 0 )
ans = Math.min(ans, cur);
}
}
if (ans == Integer.MAX_VALUE)
return - 1 ;
return ans;
}
static void swap( char t [], int i, int j)
{
char temp = t[i];
t[i] = t[j];
t[j] = temp;
}
public static void main (String[] args)
{
int n = 509201 ;
System.out.println(minMoves(n));
}
}
|
Python3
import sys
def minMoves(n):
s = str (n);
ans = sys.maxsize;
len1 = len (s);
for i in range (len1):
for j in range (len1):
if (i = = j):
continue ;
t = s;
cur = 0 ;
list1 = list (t);
for k in range (i,len1 - 1 ):
e = list1[k];
list1[k] = list1[k + 1 ];
list1[k + 1 ] = e;
cur + = 1 ;
t = ''.join(list1);
list1 = list (t);
for k in range (j - (j > i),len1 - 2 ):
e = list1[k];
list1[k] = list1[k + 1 ];
list1[k + 1 ] = e;
cur + = 1 ;
t = ''.join(list1);
pos = - 1 ;
for k in range (len1):
if (t[k] ! = '0' ):
pos = k;
break ;
for k in range (pos, 0 , - 1 ):
e = list1[k];
list1[k] = list1[k + 1 ];
list1[k + 1 ] = e;
cur + = 1 ;
t = ''.join(list1);
nn = int (t);
if (nn % 25 = = 0 ):
ans = min (ans, cur);
if (ans = = sys.maxsize):
return - 1 ;
return ans;
n = 509201 ;
print (minMoves(n));
|
C#
using System;
class GFG
{
static int minMoves( int n)
{
string s = n.ToString();
int ans = Int32.MaxValue;
int len = s.Length;
for ( int i = 0; i < len; ++i)
{
for ( int j = 0; j < len; ++j)
{
if (i == j)
continue ;
char [] t = s.ToCharArray();
int cur = 0;
for ( int k = i; k < len - 1; ++k)
{
swap(t, k, k + 1);
++cur;
}
for ( int k = j - ((j > i)? 1 : 0 ); k < len - 2; ++k)
{
swap(t, k, k + 1);
++cur;
}
int pos = -1;
for ( int k = 0; k < len; ++k)
{
if (t[k] != '0' )
{
pos = k;
break ;
}
}
for ( int k = pos; k > 0; --k)
{
swap(t, k, k - 1);
++cur;
}
int nn = Convert.ToInt32( new String(t));
if (nn % 25 == 0)
ans = Math.Min(ans, cur);
}
}
if (ans == Int32.MaxValue)
return -1;
return ans;
}
static void swap( char []t, int i, int j)
{
char temp = t[i];
t[i] = t[j];
t[j] = temp;
}
static void Main()
{
int n = 509201;
Console.WriteLine(minMoves(n));
}
}
|
PHP
<?php
function minMoves( $n )
{
$s = strval ( $n );
$ans = PHP_INT_MAX;
$len = strlen ( $s );
for ( $i = 0; $i < $len ; ++ $i )
{
for ( $j = 0; $j < $len ; ++ $j )
{
if ( $i == $j )
continue ;
$t = $s ;
$cur = 0;
for ( $k = $i ; $k < $len - 1; ++ $k )
{
$e = $t [ $k ];
$t [ $k ]= $t [ $k + 1];
$t [ $k +1]= $e ;
++ $cur ;
}
for ( $k = $j - ( $j > $i );
$k < $len - 2; ++ $k )
{
$e = $t [ $k ];
$t [ $k ] = $t [ $k + 1];
$t [ $k + 1] = $e ;
++ $cur ;
}
$pos = -1;
for ( $k = 0; $k < $len ; ++ $k )
{
if ( $t [ $k ] != '0' )
{
$pos = $k ;
break ;
}
}
for ( $k = $pos ; $k > 0; -- $k )
{
$e = $t [ $k ];
$t [ $k ] = $t [ $k + 1];
$t [ $k + 1] = $e ;
++ $cur ;
}
$nn = intval ( $t );
if ( $nn % 25 == 0)
$ans = min( $ans , $cur );
}
}
if ( $ans == PHP_INT_MAX)
return -1;
return $ans ;
}
$n = 509201;
echo minMoves( $n );
?>
|
Javascript
<script>
function minMoves(n)
{
let s = n.toString();
let ans = Number.MAX_VALUE;
let len = s.length;
for (let i = 0; i < len; ++i)
{
for (let j = 0; j < len; ++j)
{
if (i == j)
continue ;
let t = s.split( '' );
let cur = 0;
for (let k = i; k < len - 1; ++k)
{
swap(t, k, k + 1);
++cur;
}
for (let k = j - ((j > i)? 1 : 0 ); k < len - 2; ++k)
{
swap(t, k, k + 1);
++cur;
}
let pos = -1;
for (let k = 0; k < len; ++k)
{
if (t[k] != '0' )
{
pos = k;
break ;
}
}
for (let k = pos; k > 0; --k)
{
swap(t, k, k - 1);
++cur;
}
let nn = parseInt(t.join( "" ));
if (nn % 25 == 0)
ans = Math.min(ans, cur);
}
}
if (ans == Number.MAX_VALUE)
return -1;
return ans;
}
function swap(t, i, j)
{
let temp = t[i];
t[i] = t[j];
t[j] = temp;
}
let n = 509201;
document.write(minMoves(n));
</script>
|
Time Complexity: O(|n|3)
Auxiliary Space: O(1)
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...