Minimal operations to make a number magical
Given a 6 digit number, calculate the minimum number of digits that needs to be replaced in order to make the number magical. The number is considered magical if the sum of first three digits equals to the sum of last three digits. In one operation, we can choose a digit at any position and replace it with any arbitrary digit.
Examples :
Input: 123456
Output: 2
Explanation : Replace 4 with 0 and 5 with 0,
then number = 123006, where
1 + 2 + 3 = 0 + 0 + 6,
hence number of replacements
done = 2
Input: 111000
Output: 1
Explanation: Replace 0 with 3, then
number = 111030, where
1 + 1 + 1 = 0 + 3 + 0,
hence number of replacements
done = 1
Approach: The best approach will be to check with all the magical numbers and the number of replacements needed. Run a loop that generates all 6 digit numbers. Check if that number is magical, if it is then simply calculate the number of replacements needs to be done and compare with the ans, if it is smaller then make it the ans and at the end return ans.
Below is the implementation of the above approach.
C++
#include "bits/stdc++.h"
using namespace std;
int calculate(string s)
{
int ans = 6;
for ( int i = 0; i < 10; ++i) {
for ( int j = 0; j < 10; ++j) {
for ( int k = 0; k < 10; ++k) {
for ( int l = 0; l < 10; ++l) {
for ( int m = 0; m < 10; ++m) {
for ( int n = 0; n < 10; ++n) {
if (i + j + k == l + m + n) {
int c = 0;
if (i != s[0] - '0' )
c++;
if (j != s[1] - '0' )
c++;
if (k != s[2] - '0' )
c++;
if (l != s[3] - '0' )
c++;
if (m != s[4] - '0' )
c++;
if (n != s[5] - '0' )
c++;
if (c < ans)
ans = c;
}
}
}
}
}
}
}
return ans;
}
int main()
{
string s = "123456" ;
cout << calculate(s);
}
|
Java
import java.io.*;
class GFG {
static int calculate(String s)
{
int ans = 6 ;
for ( int i = 0 ; i < 10 ; ++i) {
for ( int j = 0 ; j < 10 ; ++j) {
for ( int k = 0 ; k < 10 ; ++k) {
for ( int l = 0 ; l < 10 ; ++l) {
for ( int m = 0 ; m < 10 ; ++m) {
for ( int n = 0 ; n < 10 ; ++n) {
if (i + j + k == l + m + n) {
int c = 0 ;
if (i != s.charAt( 0 ) - '0' )
c++;
if (j != s.charAt( 1 ) - '0' )
c++;
if (k != s.charAt( 2 ) - '0' )
c++;
if (l != s.charAt( 3 ) - '0' )
c++;
if (m != s.charAt( 4 ) - '0' )
c++;
if (n != s.charAt( 5 ) - '0' )
c++;
if (c < ans)
ans = c;
}
}
}
}
}
}
}
return ans;
}
static public void main (String[] args)
{
String s = "123456" ;
System.out.println(calculate(s));
}
}
|
Python3
def calculate( s):
ans = 6
for i in range ( 10 ):
for j in range ( 10 ):
for k in range ( 10 ):
for l in range ( 10 ):
for m in range ( 10 ):
for n in range ( 10 ):
if (i + j + k = = l + m + n):
c = 0
if (i ! = ord (s[ 0 ]) - ord ( '0' )):
c + = 1
if (j ! = ord (s[ 1 ]) - ord ( '0' )):
c + = 1
if (k ! = ord (s[ 2 ]) - ord ( '0' )):
c + = 1
if (l ! = ord (s[ 3 ]) - ord ( '0' )):
c + = 1
if (m ! = ord (s[ 4 ]) - ord ( '0' )):
c + = 1
if (n ! = ord (s[ 5 ]) - ord ( '0' )):
c + = 1
if (c < ans):
ans = c
return ans
if __name__ = = "__main__" :
s = "123456"
print (calculate(s))
|
C#
using System;
class GFG {
static int calculate( string s)
{
int ans = 6;
for ( int i = 0; i < 10; ++i) {
for ( int j = 0; j < 10; ++j) {
for ( int k = 0; k < 10; ++k) {
for ( int l = 0; l < 10; ++l) {
for ( int m = 0; m < 10; ++m) {
for ( int n = 0; n < 10; ++n) {
if (i + j + k == l + m + n) {
int c = 0;
if (i != s[0] - '0' )
c++;
if (j != s[1] - '0' )
c++;
if (k != s[2] - '0' )
c++;
if (l != s[3] - '0' )
c++;
if (m != s[4] - '0' )
c++;
if (n != s[5] - '0' )
c++;
if (c < ans)
ans = c;
}
}
}
}
}
}
}
return ans;
}
static public void Main ()
{
string s = "123456" ;
Console.WriteLine(calculate(s));
}
}
|
PHP
<?php
function calculate( $s )
{
$ans = 6;
for ( $i = 0; $i < 10; ++ $i ) {
for ( $j = 0; $j < 10; ++ $j ) {
for ( $k = 0; $k < 10; ++ $k ) {
for ( $l = 0; $l < 10; ++ $l ) {
for ( $m = 0; $m < 10; ++ $m ) {
for ( $n = 0; $n < 10; ++ $n ) {
if ( $i + $j + $k == $l + $m + $n ) {
$c = 0;
if ( $i != $s [0] - '0' )
$c ++;
if ( $j != $s [1] - '0' )
$c ++;
if ( $k != $s [2] - '0' )
$c ++;
if ( $l != $s [3] - '0' )
$c ++;
if ( $m != $s [4] - '0' )
$c ++;
if ( $n != $s [5] - '0' )
$c ++;
if ( $c < $ans )
$ans = $c ;
}
}
}
}
}
}
}
return $ans ;
}
$s = "123456" ;
echo calculate( $s );
?>
|
Javascript
<script>
function calculate(s)
{
let ans = 6;
for (let i = 0; i < 10; ++i)
{
for (let j = 0; j < 10; ++j)
{
for (let k = 0; k < 10; ++k)
{
for (let l = 0; l < 10; ++l)
{
for (let m = 0; m < 10; ++m)
{
for (let n = 0; n < 10; ++n)
{
if (i + j + k == l + m + n)
{
let c = 0;
if (i != s[0] - '0' )
c++;
if (j != s[1] - '0' )
c++;
if (k != s[2] - '0' )
c++;
if (l != s[3] - '0' )
c++;
if (m != s[4] - '0' )
c++;
if (n != s[5] - '0' )
c++;
if (c < ans)
ans = c;
}
}
}
}
}
}
}
return ans;
}
let s = "123456" ;
document.write(calculate(s));
</script>
|
Time complexity : O( 10^6)
Auxiliary Space : O(1)
Last Updated :
31 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...