There are two arrays of integers,arr1,arr2.One move is defined as increment or decrement of one element to match in an array.Determine the minimu number of moves to match arr1 with arr2.No reordering of the digits is allowed.
Q.There are two arrays of integers,arr1,arr2.One move is defined as increment or decrement of one element to match in an array.Determine the minimu number of moves to match arr1 with arr2.No reordering of the digits is allowed.
Example :
arr1=[123,543]
arr2=[321,279]
Match arr[0]=123 with arr2[0]=321
i.increment 1 twice to get 3 (2 moves.
ii.decrement 3 twice to get 1(2 moves)
iii.4 moves are needed to match 123 with 321.
Match arr1[1]=543 with arr2[1]=279
i.decrement 5 three times to get 2 (3 moves.
ii.increment 4 three times to get 7(2 moves)
iii.increment 3 six times to get 9(6 moves).
iv.4 moves are needed to match 123 with 321.
16 total moves are needed to match the arrays arr1 and arr2.
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 19:28:44 2023
@author: shubham prajapati
"""
import math
def cal(x,y,count):
while(x>0 and y>0):
rem1=x%10
rem2=y%10
count=count+abs(rem1-rem2)
x=x//10
y=y//10
return count
arr1=eval(input("Enter an element in list1 :")) #or you can take static list i.e arr1=[123,543] ,arr2=[321,279]
arr2=eval(input("Enter an element in list2 :")) #input number of element as you have input in arr1
count,step=0,0
for i in range(0,len(arr1)):
step+=cal(arr1[i],arr2[i],count)
print("Total",step,"steps are required.")
No comments:
If you have any doubt so you can comment me then i will 100% help you ,Through comment Chat