Chef has a safe with N N dials. The i i-th dial ...
बनाया गया: 26 मार्च 2025
बनाया गया: 26 मार्च 2025
Chef has a safe with
N
N dials. The
i
i-th dial initially shows the digit
A
i
A
i
.
To unlock the safe, the digit shown on the
i
i-th dial should be
B
i
B
i
.
In order to achieve this, Chef can choose a dial and perform one of the following two moves on it:
UP move: Increases the number on the chosen dial by
1
DOWN move: Decreases the number on the chosen dial by
1
In order to not seem too suspicious, Chef thinks he has to use exactly
K
K moves to unlock the safe.
Is it possible for Chef to unlock the safe while performing exactly
K
K moves in the process?
Note that Chef is allowed to reach the target digits even before
K
K moves: however, even if he does, he must still continue making moves till he has made exactly
K
K moves - it is not allowed to stop before that.
See the example cases below for more clarity.
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of three lines of input.
The first line contains two space-separated integers
N
N and
K
K — the number of dials and the exact number of moves allowed.
The second line contains
N
N space-separated integers
A
1
,
A
2
,
…
,
A
N
A
1
,A
2
,…,A
N
— the initial digits on the dials.
The third line contains
N
N space-separated integers
B
1
,
B
2
,
…
,
B
N
B
1
,B
2
,…,B
N
— the target digits on the dials.
Output Format
For each test case, print the answer on a new line: YES if Chef can unlock the safe after using exactly
K
K moves, and NO otherwise.
You may print each character of the answer in either uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).
Constraints
1
≤
T
≤
1
0
5
1≤T≤10
5
1
≤
N
≤
2
⋅
1
0
5
1≤N≤2⋅10
5
0
≤
K
≤
1
0
9
0≤K≤10
9
1
≤
A
i
,
B
i
≤
9
1≤A
i
,B
i
≤9
The sum of
N
N over all test cases does not exceed
2
⋅
1
0
5
2⋅10
5
.
Sample 1:
Input
Output
6
3 2
1 2 3
2 1 4
3 7
1 2 1
2 1 9
2 1
5 6
5 6
2 9
5 6
5 6
4 10
4 6 1 7
6 5 8 9
4 12
4 6 1 7
6 5 8 9
No
Yes
No
Yes
No
Yes
Explanation:
Test case
1
1: All three dials are different, but Chef wants to unlock in two moves. This is not possible, since only one dial can be changed in one move - so after two moves at least one dial will be incorrect.
7
K=7 moves is as follows:
B
i
A
i
=B
i
for every
i
i initially. However, we need to perform a move, which will break that equality.
B
i
A
i
=B
i
for every
i
i initially, and need to make
9
9 moves. Simply perform an UP move on the first dial
9
9 times, and it will end up back where it started.
accepted
Accepted
4
total-Submissions
Submissions
9
accuracy
Accuracy
44.44
Did you like the problem statement?
2 users found this helpful
More Info
Time limit1 secs
Memory limit1.5 GB
Source Limit50000 Bytes
code in java without comments
Here is the Java code for solving the problem:
javaimport java.util.Scanner; public class SafeUnlock { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-- > 0) { int N = sc.nextInt(); int K = sc.nextInt(); int[] A = new int[N]; int[] B = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } for (int i = 0; i < N; i++) { B[i] = sc.nextInt(); } int totalMoves = 0; for (int i = 0; i < N; i++) { int diff = Math.abs(A[i] - B[i]); totalMoves += Math.min(diff, 10 - diff); } if (totalMoves <= K && (K - totalMoves) % 2 == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } sc.close(); } }
This code reads multiple test cases, calculates the number of moves required to transform the digits of the dials, and checks if exactly K
moves can be performed to unlock the safe.