Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider a -slot combination lock where each slot contains a dial numbered with the ten sequential decimal integers in the inclusive range from 0 to 9. In one operation, you can choose a slot and rotate the dial by one click, either in the positive direction (to increase the displayed number by ) or the negative direction (to decrease the displayed number by ). Note that, due to the cyclical nature of the dial, the next number after 9 is 0 and the number before 0 is 9). For example, if the number 0 is currently displayed on the dial, you can rotate the dial to either 1(positive direction) or 9 (negative direction) in a single operation.
Given the initial configuration of numbers in each slot and some desired configuration of numbers, determine the minimum number of operations you must perform to change the lock's slots to the desired configuration.

Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class lock {

    public static void main(String[] args) {
     
        Scanner s=new Scanner(System.in);
        int n,m,sum=0;
        int n1[]=new int[5];
        int n2[]=new int[5];
        for(int i=0;i<5;i++)
            n1[i]=s.nextInt();
        for(int j=0;j<5;j++)
            n2[j]=s.nextInt();
        for(int k=0;k<5;k++)
        {
            n=Math.abs(n2[k]-n1[k]);
            m=Math.abs((9-n2[k])+(n1[k]+1));
            if(n<=m)
                sum=sum+n;
            else
                sum=sum+m;
            n=0;m=0;
        }
        System.out.println(sum);
    }
}


What I have tried:

This formula,which gives me the output 13,the correct output is 9.
Java
n=Math.abs(n2[k]-n1[k]);
m=Math.abs((9-n2[k])+(n1[k]+1));
Posted
Updated 28-Aug-16 5:18am
v4

1 solution

Try with
Java
for(int k=0;k<5;k++)
{
    n=Math.abs(n2[k]-n1[k]);
    m= 10- n;
    if(n<=m)
        sum=sum+n;
    else
        sum=sum+m;
    n=0;m=0;
}


Quote:
This works too,but I cannot understand where is my code going wrong. Thanks for the help.

Your code is going wrong because m is wrong.
Since a wheel is 10 positions, and n is the number of clicks one way, the other way is 10-n and your m is not doing that because n2[k]-n1[k] is positive or negative.

Quote:
My n2[k]-n1[k] is always positive as I have used Math.abs and if you check the formula,it perfectly fits.

n2[k]-n1[k] is positive because embedded in abs(), look at m, it is embedded in abs() with something else, the problem is the something else that is in the abs().
Said otherwize,
C++
m=10-Math.abs(n2[k]-n1[k]);
// and
m=Math.abs((9-n2[k])+(n1[k]+1));
// which is
m=Math.abs(10-(n2[k]-n1[k]));

are 2 different things
 
Share this answer
 
v5
Comments
Anurita Srivastava 29-Aug-16 14:21pm    
This works too,but I cannot understand where is my code going wrong. Thanks for the help.
Anurita Srivastava 2-Sep-16 13:11pm    
My n2[k]-n1[k] is always positive as I have used Math.abs and if you check the formula,it perfectly fits.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900