codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cuts required to convert a palindromic string to a different palindromic string | Function to check if string is palindrome or not ; Function to check if it is possible to get result by making just one cut ; Appending last element in front ; Removing last element ; Checking whether string s2 is palindrome and different from s . ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; Return 1 if it is possible to get palindromic string in just one cut . Else we can always reached in two cuttings . ; Driver Code
function isPalindrome ( s ) { for ( let i = 0 ; i < s . length ; ++ i ) { if ( s [ i ] != s [ s . length - i - 1 ] ) { return false ; } } return true ; } function ans ( s ) { let s2 = s ; for ( let i = 0 ; i < s . length ; ++ i ) { s2 = s2 [ s2 . length - 1 ] + s2 ; s2 = s2 . substring ( 0 , s2 . length - 1 ) ; if ( ( s == null ? s2 != null : ! s == ( s2 ) ) && isPalindrome ( s2 ) ) { return true ; } } return false ; } function solve ( s ) { if ( s . length <= 3 ) { return - 1 ; } let cnt = new Array ( 25 ) ; for ( let i = 0 ; i < 25 ; i ++ ) cnt [ i ] = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( Math . max ( ... cnt ) >= ( s . length - 1 ) ) { return - 1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } let s = " " ; document . write ( solve ( s ) ) ;
Minimum cuts required to convert a palindromic string to a different palindromic string | Recursive function to find minimum number of cuts if length of String is even ; If length is odd then return 2 ; To check if half of palindromic String is itself a palindrome ; If not then return 1 ; Else call function with half palindromic String ; Function to find minimum number of cuts If length of String is odd ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; If length is even ; If length is odd ; Driver Code
function solveEven ( s ) { if ( s . length % 2 == 1 ) { return 2 ; } let ls = s . substring ( 0 , s . length / 2 ) ; let rs = s . substring ( s . length / 2 , s . length ) ; if ( ls != rs ) { return 1 ; } return solveEven ( ls ) ; } function solveOdd ( s ) { return 2 ; } function solve ( s ) { if ( s . length <= 3 ) { return - 1 ; } let cnt = new Array ( 25 ) ; for ( let i = 0 ; i < 25 ; i ++ ) cnt [ i ] = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( Math . max ( ... cnt ) >= s . length - 1 ) { return - 1 ; } if ( s . length % 2 == 0 ) { return solveEven ( s ) ; } if ( s . length % 2 == 1 ) { return solveOdd ( s ) ; } return Number . MIN_VALUE ; } let s = " " ; document . write ( solve ( s ) ) ;
Minimum changes required such that the string satisfies the given condition | Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver Code
function minChanges ( str , n ) { let count = 0 , zeros = 0 , ones = 0 ; if ( str [ 0 ] != ' ' ) { count ++ ; ones ++ ; } for ( let i = 1 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) zeros ++ ; else ones ++ ; if ( zeros > ones ) { zeros -- ; ones ++ ; count ++ ; } } return count ; } let str = " " . split ( ' ' ) ; let n = str . length ; document . write ( minChanges ( str , n ) ) ;
Count possible moves in the given direction in a grid | Function to return the count of possible steps in a single direction ; It can cover infinite steps ; We are approaching towards X = N ; We are approaching towards X = 1 ; Function to return the count of steps ; Take the minimum of both moves independently ; Update count and current positions ; driver program
function steps ( cur , x , n ) { if ( x == 0 ) return Number . MAX_VALUE ; if ( x > 0 ) return Math . abs ( ( n - cur ) / x ) ; else return Math . abs ( ( cur - 1 ) / x ) ; } function countSteps ( curx , cury , n , m , moves ) { let count = 0 ; let k = moves . length ; for ( let i = 0 ; i < k ; i ++ ) { let x = moves [ i ] [ 0 ] ; let y = moves [ i ] [ 1 ] ; let stepct = Math . min ( steps ( curx , x , n ) , steps ( cury , y , m ) ) ; count += stepct ; curx += stepct * x ; cury += stepct * y ; } return Math . floor ( count ) ; } let n = 4 , m = 5 , x = 1 , y = 1 ; let moves = [ [ 1 , 1 ] , [ 1 , 1 ] , [ 0 , - 2 ] ] ; document . write ( countSteps ( x , y , n , m , moves ) ) ;
Minimum elements to be removed such that sum of adjacent elements is always odd | Returns the minimum number of eliminations ; Stores the previous element ; Stores the new value ; Check if the previous and current values are of same parity ; Previous value is now the current value ; Return the counter variable ; Driver code
function min_elimination ( n , arr ) { let count = 0 ; let prev_val = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { let curr_val = arr [ i ] ; if ( curr_val % 2 == prev_val % 2 ) count ++ ; prev_val = curr_val ; } return count ; } let arr = [ 1 , 2 , 3 , 7 , 9 ] ; let n = arr . length ; document . write ( min_elimination ( n , arr ) ) ;
Count all possible N digit numbers that satisfy the given condition | Function to return the count of required numbers ; If N is odd then return 0 ; Driver code
function getCount ( N ) { if ( N % 2 == 1 ) return " " ; let result = " " ; for ( let i = 1 ; i <= N / 2 - 1 ; i ++ ) result += " " ; return result ; } let N = 4 ; document . write ( getCount ( N ) ) ;
Maximum number of teams that can be formed with given persons | if it possible to form a team with the given n and m ; 1 person of Type1 and 2 persons of Type2 can be chosen ; 1 person of Type2 and 2 persons of Type1 can be chosen ; Cannot from a team ; Function to return the maximum number of teams that can be formed ; To store the required count of teams formed ; Choose 2 persons of Type1 ; And 1 person of Type2 ; Choose 2 persons of Type2 ; And 1 person of Type1 ; Another team has been formed ; Driver code
function canFormTeam ( n , m ) { if ( n >= 1 && m >= 2 ) return true ; if ( m >= 1 && n >= 2 ) return true ; return false ; } function maxTeams ( n , m ) { var count = 0 ; while ( canFormTeam ( n , m ) ) { if ( n > m ) { n -= 2 ; m -= 1 ; } else { m -= 2 ; n -= 1 ; } count ++ ; } return count ; } var n = 4 , m = 5 ; document . write ( maxTeams ( n , m ) ) ;
Find the Side of the smallest Square that can contain given 4 Big Squares | Function to find the maximum of two values ; Function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; Driver program ; Get the side of the 4 small squares ; Find the smallest side ; Get the side of the 4 small squares ; Find the smallest side
function max ( a , b ) { if ( a > b ) return a ; else return b ; } function smallestSide ( a ) { a . sort ( ) ; var side1 , side2 , side3 , side4 , side11 , side12 , sideOfSquare ; side1 = a [ 0 ] + a [ 3 ] ; side2 = a [ 1 ] + a [ 2 ] ; side3 = a [ 0 ] + a [ 1 ] ; side4 = a [ 2 ] + a [ 3 ] ; side11 = max ( side1 , side2 ) ; side12 = max ( side3 , side4 ) ; sideOfSquare = max ( side11 , side12 ) ; return sideOfSquare ; } var side = Array . from ( { length : 4 } , ( _ , i ) => 0 ) ; document . write ( " " ) ; side [ 0 ] = 2 ; side [ 1 ] = 2 ; side [ 2 ] = 2 ; side [ 3 ] = 2 ; document . write ( smallestSide ( side ) ) ; document . write ( " " ) ; side [ 0 ] = 100000000000000 ; side [ 1 ] = 123450000000000 ; side [ 2 ] = 987650000000000 ; side [ 3 ] = 987654321000000 ; document . write ( smallestSide ( side ) ) ;
Rectangle with minimum possible difference between the length and the width | Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; i is a factor ; l >= sqrt ( area ) >= i ; so here l is + ve always ; Here l and b are length and breadth of the rectangle ; Driver code
function find_rectangle ( area ) { let l = 0 , b = 0 ; let M = Math . floor ( Math . sqrt ( area ) ) , ans ; for ( let i = M ; i >= 1 ; i -- ) { if ( area % i == 0 ) { l = Math . floor ( area / i ) ; b = i ; break ; } } document . write ( " " + l + " " + b ) ; } let area = 99 ; find_rectangle ( area ) ;
Rectangle with minimum possible difference between the length and the width | Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; Driver code
function find_rectangle ( are ) { for ( let i = Math . floor ( Math . ceil ( Math . sqrt ( area ) ) ) ; i <= area ; i ++ ) { if ( Math . floor ( area / i ) * i == area ) { document . write ( i + " " + Math . floor ( area / i ) ) ; return ; } } } let area = 99 ; find_rectangle ( area ) ;
Largest sub | Function to return the size of the required sub - set ; Sort the array ; HashMap to store the contents of the required sub - set ; Insert the elements satisfying the conditions ; Return the size of the set ; Driver code
function sizeSubSet ( a , k , n ) { a . sort ( function ( a , b ) { return a - b ; } ) ; let s = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % k != 0 || s . get ( a [ i ] / k ) == null ) s . set ( a [ i ] , s . get ( a [ i ] ) == null ? 1 : s . get ( a [ i ] ) + 1 ) ; } return s . size ; } let a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = a . length ; let k = 2 ; document . write ( sizeSubSet ( a , k , n ) ) ;
Minimum number of sets with numbers less than Y | Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver code
function minimumSets ( s , y ) { let cnt = 0 ; let num = 0 ; let l = s . length ; let f = false ; for ( let i = 0 ; i < l ; i ++ ) { num = num * 10 + ( s [ i ] - ' ' ) ; if ( num <= y ) f = true ; { if ( f ) cnt += 1 ; num = s [ i ] - ' ' ; f = false ; if ( num <= y ) f = true ; else num = 0 ; } } if ( f == true ) cnt += 1 ; return cnt ; } let s = " " ; let y = 30 ; document . write ( minimumSets ( s , y ) ) ;
Find the non decreasing order array from given array | Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code
function printArr ( b , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( " " + b [ i ] + " " ) ; } } function ModifiedArray ( a , n ) { let l = 0 , r = Number . MAX_VALUE ; let b = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } let a = [ 5 , 6 ] ; let n = a . length ; ModifiedArray ( a , 2 * n ) ;
Count of lines required to write the given String | Function to return the number of lines required ; If String is empty ; Initialize lines and width ; Iterate through S ; Return lines and width used ; Driver Code ; Function call to print required answer
function numberOfLines ( S , widths ) { if ( S . length == 0 ) return [ 0 , 0 ] ; let lines = 1 , width = 0 ; for ( let character of S . split ( " " ) ) { let w = widths [ character . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return [ lines , width ] ; } let S = " " ; let widths = [ 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ; let ans = numberOfLines ( S , widths ) ; document . write ( ans [ 0 ] + " " + ans [ 1 ] + " " ) ;
Check if elements of an array can be arranged satisfying the given condition | Function to return true if the elements can be arranged in the desired order ; If 2 * x is not found to pair ; Remove an occurrence of x and an occurrence of 2 * x ; Driver Code ; Function call to print required answer
function canReorder ( A , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( A [ i ] ) ) { m . set ( A [ i ] , m . get ( A [ i ] ) + 1 ) } else { m . set ( A [ i ] , 1 ) } } A . sort ( ( a , b ) => a - b ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . get ( A [ i ] ) == 0 ) continue ; if ( m . get ( 2 * A [ i ] ) ) { count += 2 ; m . set ( A [ i ] , m . get ( A [ i ] ) - 1 ) ; m . set ( 2 * A [ i ] , m . get ( 2 * A [ i ] ) - 1 ) ; } } if ( count == n ) return " " ; else return " " ; } let A = [ 4 , - 2 , 2 , - 4 ] ; let n = A . length ; document . write ( ( canReorder ( A , n ) ) ) ;
Maximum sum of all elements of array after performing given operations | Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array
function maxSubArraySum ( a , size ) { var max_so_far = Number . MIN_VALUE , max_ending_here = 0 ; for ( i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } function maxSum ( a , n ) { var S = 0 ; var S1 = maxSubArraySum ( a , n ) ; for ( i = 0 ; i < n ; i ++ ) S += a [ i ] ; return ( 2 * S1 - S ) ; } var a = [ - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 ] ; var n = a . length ; document . write ( maxSum ( a , n ) ) ;
Minimize the difference between minimum and maximum elements | Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code
function minimizeDiff ( arr , n , k ) { let max = Math . max ( ... arr ) ; let min = Math . min ( ... arr ) ; if ( ( max - min ) <= k ) { return ( max - min ) ; } let avg = Math . floor ( ( max + min ) / 2 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > avg ) { arr [ i ] -= k ; } else { arr [ i ] += k ; } } max = Math . max ( ... arr ) ; min = Math . min ( ... arr ) ; return ( max - min ) ; } let arr = [ 3 , 16 , 12 , 9 , 20 ] ; let n = 5 ; let k = 3 ; document . write ( " " + minimizeDiff ( arr , n , k ) ) ;
Maximum litres of water that can be bought with N Rupees | Javascript implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver code
function maxLitres ( budget , plastic , glass , refund ) { if ( glass - refund < plastic ) { let ans = Math . max ( ( budget - refund ) / ( glass - refund ) , 0 ) ; budget -= ans * ( glass - refund ) ; ans += Math . floor ( budget / plastic ) ; document . write ( ans ) ; } else { document . write ( Math . floor ( budget / plastic ) ) ; } } let budget = 10 , plastic = 11 , glass = 9 , refund = 8 ; maxLitres ( budget , plastic , glass , refund ) ;
Find number from given list for which value of the function is closest to A | Function to find number from given list for which value of the function is closest to A ; Stores the final index ; Declaring a variable to store the minimum absolute difference ; Finding F ( n ) ; Updating the index of the answer if new absolute difference is less than tmp ; Driver code
function leastValue ( P , A , N , a ) { let ans = - 1 ; let tmp = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let t = ( P - a [ i ] * 0.006 ) ; if ( Math . abs ( t - A ) < tmp ) { tmp = Math . abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } let N = 2 , P = 12 , A = 2005 ; let a = [ 1000 , 2000 ] ; document . write ( leastValue ( P , A , N , a ) )
Find permutation of n which is divisible by 3 but not divisible by 6 | Function to find the permutation ; length of integer ; if integer is even ; return odd integer ; rotate integer ; return - 1 in case no required permutation exists ; Driver Code
function findPermutation ( n ) { let len = Math . ceil ( Math . log10 ( n ) ) ; for ( let i = 0 ; i < len ; i ++ ) { if ( n % 2 != 0 ) { return parseInt ( n ) ; } else { n = ( n / 10 ) + ( n % 10 ) * Math . pow ( 10 , len - i - 1 ) ; continue ; } } return - 1 ; } let n = 132 ; document . write ( findPermutation ( n ) ) ;
Maximum score after flipping a Binary Matrix atmost K times | Javascript program to find the maximum score after flipping a Binary Matrix atmost K times ; Function to find maximum score of matrix ; find value of rows having first column value equal to zero ; update those rows which lead to maximum score after toggle ; Calculating answer ; check if K > 0 we can toggle if necessary . ; return max answer possible ; Driver program ; function call to print required answer
var n = 3 ; var m = 4 ; function maxMatrixScore ( A , K ) { var update = new Map ( ) ; for ( var i = 0 ; i < n ; ++ i ) { if ( A [ i ] [ 0 ] == 0 ) { var ans = 0 ; for ( var j = 1 ; j < m ; ++ j ) ans = ans + A [ i ] [ j ] * Math . pow ( 2 , m - j - 1 ) ; update . set ( ans , i ) ; } } update . forEach ( ( value , key ) => { if ( K > 0 ) { var idx = value ; for ( var j = 0 ; j < m ; ++ j ) A [ idx ] [ j ] = ( A [ idx ] [ j ] + 1 ) % 2 ; K -- ; } } ) ; var ans = 0 ; for ( var j = 0 ; j < m ; ++ j ) { var zero = 0 , one = 0 ; for ( var i = 0 ; i < n ; ++ i ) { A [ i ] [ j ] == 0 ? zero ++ : one ++ ; } if ( K > 0 && zero > one ) { ans += zero * Math . pow ( 2 , m - j - 1 ) ; K -- ; } else ans += one * Math . pow ( 2 , m - j - 1 ) ; } return ans ; } var A = [ [ 0 , 0 , 1 , 1 ] , [ 1 , 0 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] ] ; var K = 2 ; document . write ( maxMatrixScore ( A , K ) ) ;
Check if it is possible to serve customer queue with different notes | Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; driver code
function isSellingPossible ( n , a ) { let i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } let a = [ 25 , 25 , 50 , 100 ] ; let n = a . length ; if ( isSellingPossible ( n , a ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if a cell can be visited more than once in a String | Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once Return True ; Driver code
function checkIfOverlap ( str ) { let len = str . length ; let visited = new Array ( len + 1 ) ; for ( let i = 0 ; i < visited . length ; i ++ ) { visited [ i ] = 0 ; } for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) continue ; for ( let j = Math . max ( 0 , i - str [ i ] ) ; j <= Math . min ( len , i + str [ i ] ) ; j ++ ) visited [ j ] ++ ; } for ( let i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } let str = " " ; if ( checkIfOverlap ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number has digits in the given Order | Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code
function isCorrectOrder ( n ) { let flag = true ; let prev = - 1 ; let type = - 1 ; while ( n != 0 ) { if ( type === - 1 ) { if ( prev === - 1 ) { prev = n % 10 ; n = Math . floor ( n / 10 ) ; continue ; } if ( prev == n % 10 ) { flag = false ; break ; } if ( prev > n % 10 ) { type = 1 ; prev = n % 10 ; n = Math . floor ( n / 10 ) ; continue ; } prev = n % 10 ; n = Math . floor ( n / 10 ) ; } else { if ( prev == n % 10 ) { flag = false ; break ; } if ( prev < n % 10 ) { flag = false ; break ; } prev = n % 10 ; n = Math . floor ( n / 10 ) ; } } return flag ; } let n = 123454321 ; if ( isCorrectOrder ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Coin game of two corners ( Greedy Approach ) | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Find sum of odd positioned coins ; Find sum of even positioned coins ; Print even or odd coins depending upon which sum is greater . ; Driver code
function printCoins ( arr , n ) { var oddSum = 0 ; for ( var i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; var evenSum = 0 ; for ( var i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; var start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( var i = start ; i < n ; i += 2 ) document . write ( arr [ i ] + " " ) ; } var arr1 = [ 8 , 15 , 3 , 7 ] var n = arr1 . length ; printCoins ( arr1 , n ) ; document . write ( " " ) ; var arr2 = [ 2 , 2 , 2 , 2 ] var n = arr2 . length ; printCoins ( arr2 , n ) ; document . write ( " " ) ; var arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] n = arr3 . length ; printCoins ( arr3 , n ) ;
Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; driver code
function finalPos ( command , n , x , y ) { let cup , cdown , cleft , cright ; let final_x , final_y ; cup = cdown = cleft = cright = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( command [ i ] == ' ' ) cup ++ ; else if ( command [ i ] == ' ' ) cdown ++ ; else if ( command [ i ] == ' ' ) cleft ++ ; else if ( command [ i ] == ' ' ) cright ++ ; } final_x = x + ( cright - cleft ) ; final_y = y + ( cdown - cup ) ; document . write ( " " + " " + final_x + " " + final_y + " " ) ; } let command = " " ; let n = command . length ; let x = 3 , y = 4 ; finalPos ( command , n , x , y ) ;