Minimum number of operations to convert array A to array B by adding an integer into a subarray | Function to find the minimum number of operations in which array A can be converted to array B ; Loop to iterate over the array ; if both elements are equal then move to next element ; Calculate the difference between two elements ; loop while the next pair of elements have same difference ; Increase the number of operations by 1 ; Prvar the number of operations required ; Driver Code
function checkArray ( a , b , n ) { var operations = 0 ; var i = 0 ; while ( i < n ) { if ( a [ i ] - b [ i ] == 0 ) { i ++ ; continue ; } var diff = a [ i ] - b [ i ] ; i ++ ; while ( i < n && a [ i ] - b [ i ] == diff ) { i ++ ; } operations ++ ; } document . write ( operations ) ; } var a = [ 3 , 7 , 1 , 4 , 1 , 2 ] ; var b = [ 3 , 7 , 3 , 6 , 3 , 2 ] ; var size = a . length ; checkArray ( a , b , size ) ;