codeparrot/xlcost-text-to-code huggingface dataset:
Minimum count of numbers required with unit digit X that sums up to N | Function to calculate and return the minimum number of times a number with unit digit X needs to be added to get a sum N ; Calculate the number of additions required to get unit digit of N ; If unit digit of N cannot be obtained ; Function to return the minimum number required to represent N ; Stores unit digit of N ; Stores minimum addition of X required to obtain unit digit of N ; If unit digit of N cannot be obtained ; Otherwise ; If N is greater than or equal to ( X * times ) ; Minimum count of numbers that needed to represent N ; Representation not possible ; Driver Code
function check ( unit_digit , X ) { let times , digit ; for ( times = 1 ; times <= 10 ; times ++ ) { digit = ( X * times ) % 10 ; if ( digit == unit_digit ) return times ; } return - 1 ; } function getNum ( N , X ) { let unit_digit ; unit_digit = N % 10 ; let times = check ( unit_digit , X ) ; if ( times == - 1 ) return times ; else { if ( N >= ( times * X ) ) return times ; else return - 1 ; } } let N = 58 , X = 7 ; document . write ( getNum ( N , X ) ) ;
Minimum number of points required to cover all blocks of a 2 | Function to find the minimum number of Polets required to cover a grid ; If number of block is even ; Return the minimum polets ; Given size of grid ; Function Call
function minPolets ( n , m ) { let ans = 0 ; if ( ( n % 2 != 0 ) && ( m % 2 != 0 ) ) { ans = Math . floor ( ( n * m ) / 2 ) + 1 ; } else { ans = Math . floor ( ( n * m ) / 2 ) ; } return ans ; } let N = 5 , M = 7 ; document . write ( minPolets ( N , M ) ) ;
Largest lexicographical string with at most K consecutive elements | Function to find the largest lexicographical String with given constraints . ; Vector containing frequency of each character . ; Assigning frequency ; Empty String of String class type ; Loop to iterate over maximum priority first . ; If frequency is greater than or equal to k . ; Temporary variable to operate in - place of k . ; Concatenating with the resultant String ans . ; Handling k case by adjusting with just smaller priority element . ; Condition to verify if index j does have frequency greater than 0 ; ; If no such element is found than String can not be processed further . ; If frequency is greater than 0 and less than k . ; Here we don 't need to fix K  consecutive element criteria. ; Otherwise check for next possible element . ; Driver code
function getLargestString ( s , k ) { let frequency_array = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { frequency_array [ i ] = 0 ; } for ( let i = 0 ; i < s . length ; i ++ ) { frequency_array [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let ans = " " ; for ( let i = 25 ; i >= 0 ; ) { if ( frequency_array [ i ] > k ) { let temp = k ; let st = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } frequency_array [ i ] -= k ; let j = i - 1 ; while ( frequency_array [ j ] <= 0 && j >= 0 ) { j -- ; } if ( frequency_array [ j ] > 0 && j >= 0 ) { let str = String . fromCharCode ( j + ' ' . charCodeAt ( 0 ) ) ; ans += str ; frequency_array [ j ] -= 1 ; } else { break ; } } else if ( frequency_array [ i ] > 0 ) { let temp = frequency_array [ i ] ; frequency_array [ i ] -= temp ; let st = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } } else { i -- ; } } return ans ; } let S = " " ; let k = 3 ; document . write ( getLargestString ( S , k ) ) ;
Minimum operations to make all elements equal using the second array | Function to find the minimum operations required to make all elements of the array equal ; Minimum element of A ; Traverse through all final values ; Variable indicating whether all elements can be converted to x or not ; Total operations ; Traverse through all array elements ; All elements can 't  be converted to x ; Driver Code
function minOperations ( a , b , n ) { var minA = Math . max . apply ( Math , a ) ; ; for ( x = minA ; x >= 0 ; x -- ) { var check = true ; var operations = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( x % b [ i ] == a [ i ] % b [ i ] ) { operations += ( a [ i ] - x ) / b [ i ] ; } else { check = false ; break ; } } if ( check ) return operations ; } return - 1 ; } var N = 5 ; var A = [ 5 , 7 , 10 , 5 , 15 ] ; var B = [ 2 , 2 , 1 , 3 , 5 ] ; document . write ( minOperations ( A , B , N ) ) ;
Find the maximum sum ( a + b ) for a given input integer N satisfying the given condition | Function to return the maximum sum of a + b satisfying the given condition ; Initialize max_sum ; Consider all the possible pairs ; Check if the product is divisible by the sum ; Storing the maximum sum in the max_sum variable ; Return the max_sum value ; Driver code
function getLargestSum ( N ) { var max_sum = 0 ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = i + 1 ; j <= N ; j ++ ) { if ( i * j % ( i + j ) == 0 ) max_sum = Math . max ( max_sum , i + j ) ; } } return max_sum ; } var N = 25 ; var max_sum = getLargestSum ( N ) ; document . write ( max_sum ) ;
Maximize the sum of array after multiplying a prefix and suffix by | Kadane 's algorithm to find the maximum subarray sum ; Loop to find the maximum subarray array sum in the given array ; Function to find the maximum sum of the array by multiplying the prefix and suffix by - 1 ; Total intital sum ; Loop to find the maximum sum of the array ; Maximum value ; Driver Code
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_ending_here < 0 ) max_ending_here = 0 ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } function maxSum ( a , n ) { var S = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) S += a [ i ] ; var X = maxSubArraySum ( a , n ) ; return 2 * X - S ; } var a = [ - 1 , - 2 , - 3 ] ; var n = a . length ; var max_sum = maxSum ( a , n ) ; document . write ( max_sum ) ;
Count of interesting primes upto N | Function to check if a number is prime or not ; If n is divisible by any number between 2 and Math . sqrt ( n ) , it is not prime ; Function to check if a number is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find the number of interesting primes less than equal to N . ; Check whether the number is prime or not ; Iterate for values of b ; Check condition for a ; Return the required answer ; Driver code
function isPrime ( n ) { let flag = 1 ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { flag = 0 ; break ; } } return ( flag == 1 ? true : false ) ; } function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function countInterestingPrimes ( n ) { let answer = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) { for ( let j = 1 ; j * j * j * j <= i ; j ++ ) { if ( isPerfectSquare ( i - j * j * j * j ) ) { answer ++ ; break ; } } } } return answer ; } let N = 10 ; document . write ( countInterestingPrimes ( N ) ) ;
Maximize the number by flipping at most K bits | Function to convert decimal number n to its binary representation stored as an array arr [ ] ; Function to convert the number represented as a binary array arr [ ] into its decimal equivalent ; Function to return the maximized number by flipping atmost k bits ; Number of bits in n ; Find the binary representation of n ; To count the number of 0 s flipped ; Return the decimal equivalent of the maximized number ; Driver code
function decBinary ( arr , n ) { let k = Math . log2 ( n ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n = Math . floor ( n / 2 ) ; } } function binaryDec ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } function maxNum ( n , k ) { let l = Math . log2 ( n ) + 1 ; let a = new Array ( l ) . fill ( 0 ) ; decBinary ( a , n ) ; let cn = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( a [ i ] == 0 && cn < k ) { a [ i ] = 1 ; cn ++ ; } } return binaryDec ( a , l ) ; } let n = 4 , k = 1 ; document . write ( maxNum ( n , k ) ) ;
Find the subsequence with given sum in a superincreasing sequence | Function to find the required subsequence ; Current element cannot be a part of the required subsequence ; Include current element in the required subsequence So update the sum ; Prlet the elements of the required subsequence ; If the current element was included in the subsequence ; Driver code
function findSubSeq ( arr , n , sum ) { for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( sum < arr [ i ] ) arr [ i ] = - 1 ; else sum -= arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != - 1 ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 17 , 25 , 46 , 94 , 201 , 400 ] ; let n = arr . length ; let sum = 272 ; findSubSeq ( arr , n , sum ) ;
Find the most valued alphabet in the String | JavaScript implementation of the approach ; Function to return the maximum valued alphabet ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the result ; For every alphabet ; If current alphabet doesn 't appear  in the given String ; If the current character has the highest value so far ; Driver code
const MAX = 26 ; function maxAlpha ( str , len ) { var first = new Array ( MAX ) ; var last = new Array ( MAX ) ; for ( var i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( var i = 0 ; i < len ; i ++ ) { var index = str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; if ( first [ index ] === - 1 ) first [ index ] = i ; last [ index ] = i ; } var ans = - 1 , maxVal = - 1 ; for ( var i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] === - 1 ) continue ; if ( last [ i ] - first [ i ] > maxVal ) { maxVal = last [ i ] - first [ i ] ; ans = i ; } } return String . fromCharCode ( ans + " " . charCodeAt ( 0 ) ) ; } var str = " " ; var len = str . length ; document . write ( maxAlpha ( str , len ) ) ;
Queries for number of distinct elements from a given index till last index in an array | Function to perform queries to find number of distinct elements from a given index till last index in an array ; Check if current element already visited or not ; If not visited store current counter and increment it and mark check as 1 ; Otherwise if visited simply store current counter ; Perform queries ; Driver code
function find_distinct ( a , n , q , queries ) { let MAX = 100001 ; let check = new Array ( MAX ) . fill ( 0 ) ; let idx = new Array ( MAX ) . fill ( 0 ) ; let cnt = 1 ; let i = n - 1 ; while ( i >= 0 ) { if ( check [ a [ i ] ] == 0 ) { idx [ i ] = cnt ; check [ a [ i ] ] = 1 ; cnt ++ ; } else { idx [ i ] = cnt - 1 ; } i -- ; } for ( let i = 0 ; i < q ; i ++ ) { let m = queries [ i ] ; document . write ( idx [ m ] + " " ) ; } } let a = [ 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 ] ; let n = a . length ; let queries = [ 0 , 3 , 5 , 7 ] ; let q = queries . length ; find_distinct ( a , n , q , queries ) ;
Convert given integer X to the form 2 ^ N | Javascript implementation of the approach ; Function to return the count of operations required ; To store the powers of 2 ; Temporary variable to store x ; To store the index of smaller number larger than x ; To store the count of operations ; Stores the index of number in the form of 2 ^ n - 1 ; If x is already in the form 2 ^ n - 1 then no operation is required ; If number is less than x increase the index ; Calculate all the values ( x xor 2 ^ n - 1 ) for all possible n ; Only take value which is closer to the number ; If number is in the form of 2 ^ n - 1 then break ; Return the count of operations required to obtain the number ; Driver code
const MAX = 24 ; function countOp ( x ) { let arr = new Array ( MAX ) ; arr [ 0 ] = 1 ; for ( let i = 1 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] * 2 ; let temp = x ; let flag = true ; let ans ; let operations = 0 ; let flag2 = false ; for ( let i = 0 ; i < MAX ; i ++ ) { if ( arr [ i ] - 1 == x ) flag2 = true ; if ( arr [ i ] > x ) { ans = i ; break ; } } if ( flag2 ) return 0 ; while ( flag ) { if ( arr [ ans ] < x ) ans ++ ; operations ++ ; for ( let i = 0 ; i < MAX ; i ++ ) { let take = x ^ ( arr [ i ] - 1 ) ; if ( take <= arr [ ans ] - 1 ) { if ( take > temp ) temp = take ; } } if ( temp == arr [ ans ] - 1 ) { flag = false ; break ; } temp ++ ; operations ++ ; x = temp ; if ( x == arr [ ans ] - 1 ) flag = false ; } return operations ; } let x = 39 ; document . write ( countOp ( x ) ) ;
Minimum number of given operations required to reduce the array to 0 element | Function to return the minimum operations required ; Count the frequency of each element ; Maximum element from the array ; Find all the multiples of i ; Delete the multiples ; Increment the operations ; Driver code
function minOperations ( arr , n ) { let maxi , result = 0 ; let freq = new Array ( 1000001 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { let x = arr [ i ] ; freq [ x ] ++ ; } maxi = Math . max ( ... arr ) ; for ( let i = 1 ; i <= maxi ; i ++ ) { if ( freq [ i ] != 0 ) { for ( let j = i * 2 ; j <= maxi ; j = j + i ) { freq [ j ] = 0 ; } result ++ ; } } return result ; } let arr = [ 2 , 4 , 2 , 4 , 4 , 4 ] ; let n = arr . length ; document . write ( minOperations ( arr , n ) ) ;
Minimum LCM and GCD possible among all possible sub | Function to return minimum GCD among all subarrays ; Minimum GCD among all sub - arrays will be the GCD of all the elements of the array ; Function to return minimum LCM among all subarrays ; Minimum LCM among all sub - arrays will be the minimum element from the array ; Driver code
function __gcd ( a , b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } function minGCD ( arr , n ) { var minGCD = 0 ; for ( i = 0 ; i < n ; i ++ ) minGCD = __gcd ( minGCD , arr [ i ] ) ; return minGCD ; } function minLCM ( arr , n ) { var minLCM = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) minLCM = Math . min ( minLCM , arr [ i ] ) ; return minLCM ; } var arr = [ 2 , 66 , 14 , 521 ] ; var n = arr . length ; document . write ( " " + minLCM ( arr , n ) + " " + minGCD ( arr , n ) ) ;
Form lexicographically smallest string with minimum replacements having equal number of 0 s , 1 s and 2 s | Function that returns the modified lexicographically smallest String after performing minimum number of given operations ; Stores the initial frequencies of characters 0 s , 1 s and 2 s ; Stores number of processed characters upto that point of each type ; Required number of characters of each type ; If the current type has already reqd number of characters , no need to perform any operation ; Process all 3 cases ; Check for 1 first ; Else 2 ; Here we need to check processed [ 1 ] only for 2 since 0 is less than 1 and we can replace it anytime ; Here we can replace 2 with 0 and 1 anytime ; keep count of processed characters of each type ; Driver Code
function formStringMinOperations ( s ) { var count = new Array ( 3 ) . fill ( 0 ) ; for ( const c of s ) { count += 1 ; } var processed = new Array ( 3 ) . fill ( 0 ) ; var reqd = parseInt ( s . length / 3 ) ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( count [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === reqd ) { continue ; } if ( s [ i ] === " " && count [ 0 ] > reqd && processed [ 0 ] >= reqd ) { if ( count [ 1 ] < reqd ) { s [ i ] = " " ; count [ 1 ] ++ ; count [ 0 ] -- ; } else if ( count [ 2 ] < reqd ) { s [ i ] = " " ; count [ 2 ] ++ ; count [ 0 ] -- ; } } if ( s [ i ] === " " && count [ 1 ] > reqd ) { if ( count [ 0 ] < reqd ) { s [ i ] = " " ; count [ 0 ] ++ ; count [ 1 ] -- ; } else if ( count [ 2 ] < reqd && processed [ 1 ] >= reqd ) { s [ i ] = " " ; count [ 2 ] ++ ; count [ 1 ] -- ; } } if ( s [ i ] === " " && count [ 2 ] > reqd ) { if ( count [ 0 ] < reqd ) { s [ i ] = " " ; count [ 0 ] ++ ; count [ 2 ] -- ; } else if ( count [ 1 ] < reqd ) { s [ i ] = " " ; count [ 1 ] ++ ; count [ 2 ] -- ; } } processed [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } return s . join ( " " ) ; } var s = " " ; document . write ( formStringMinOperations ( s . split ( " " ) ) ) ;
Minimum number of adjacent swaps for arranging similar elements together | Function to find minimum swaps ; visited array to check if value is seen already ; If the arr [ i ] is seen first time ; stores the number of swaps required to find the correct position of current element 's partner ; Increment count only if the current element has not been visited yet ( if is visited , means it has already been placed at its correct position ) ; If current element 's partner is found ; driver code
function findMinimumAdjacentSwaps ( arr , N ) { let visited = Array ( N + 1 ) . fill ( false ) ; let minimumSwaps = 0 ; for ( let i = 0 ; i < 2 * N ; i ++ ) { if ( visited [ arr [ i ] ] == false ) { visited [ arr [ i ] ] = true ; let count = 0 ; for ( let j = i + 1 ; j < 2 * N ; j ++ ) { if ( visited [ arr [ j ] ] == false ) count ++ ; else if ( arr [ i ] == arr [ j ] ) minimumSwaps += count ; } } } return minimumSwaps ; } let arr = [ 1 , 2 , 3 , 3 , 1 , 2 ] ; let N = arr . length ; N = Math . floor ( N / 2 ) ; document . write ( findMinimumAdjacentSwaps ( arr , N ) ) ;
Largest palindromic number by permuting digits | function to check if a number can be permuted to form a palindrome number ; counts the occurrence of number which is odd ; if occurrence is odd ; if number exceeds 1 ; function to print the largest palindromic number by permuting digits of a number ; string length ; map that marks the occurrence of a number ; check the possibility of a palindromic number ; string array that stores the largest permuted palindromic number ; pointer of front ; greedily start from 9 to 0 and place the greater number in front and odd in the middle ; if the occurrence of number is odd ; place one odd occurring number in the middle ; decrease the count ; place the rest of numbers greedily ; if all numbers occur even times , then place greedily ; place greedily at front ; 2 numbers are placed , so decrease the count ; increase placing position ; print the largest string thus formed ; driver code
function possibility ( m , length , s ) { var countodd = 0 ; for ( var i = 0 ; i < length ; i ++ ) { if ( m . get ( s . charCodeAt ( i ) - 48 ) & 1 ) countodd ++ ; if ( countodd > 1 ) return false ; } return true ; } function largestPalindrome ( s ) { var l = s . length ; var m = new Map ( ) ; for ( var i = 0 ; i < l ; i ++ ) { if ( m . has ( s . charCodeAt ( i ) - 48 ) ) m . set ( s . charCodeAt ( i ) - 48 , m . get ( s . charCodeAt ( i ) - 48 ) + 1 ) ; else m . set ( s . charCodeAt ( i ) - 48 , 1 ) ; } if ( possibility ( m , l , s ) == false ) { document . write ( " " ) ; return ; } var largest = new Array ( l ) ; var front = 0 ; for ( var i = 9 ; i >= 0 ; i -- ) { if ( m . has ( i ) & 1 ) { largest [ Math . floor ( l / 2 ) ] = String . fromCharCode ( i + 48 ) ; m . set ( i , m . get ( i ) - 1 ) ; while ( m . get ( i ) > 0 ) { largest [ front ] = String . fromCharCode ( i + 48 ) ; largest [ l - front - 1 ] = String . fromCharCode ( i + 48 ) ; m . set ( i , m . get ( i ) - 2 ) ; front ++ ; } } else { while ( m . get ( i ) > 0 ) { largest [ front ] = String . fromCharCode ( i + 48 ) ; largest [ l - front - 1 ] = String . fromCharCode ( i + 48 ) ; m . set ( i , m . get ( i ) - 2 ) ; front ++ ; } } } for ( var i = 0 ; i < l ; i ++ ) document . write ( largest [ i ] ) ; } var s = " " ; largestPalindrome ( s ) ;
Minimum Swaps for Bracket Balancing | Function to calculate swaps required ; Keep track of ' [ ' ; To count number of encountered ' [ ' ; To track position of next ' [ ' in pos ; To store result ; Increment count and move p to next position ; We have encountered an unbalanced part of string ; Increment sum by number of swaps required i . e . position of next ' [ ' - current position ; Reset count to 1 ; Driver Code
function swapCount ( s ) { let pos = [ ] ; for ( let i = 0 ; i < s . length ; ++ i ) if ( s [ i ] == ' ' ) pos . push ( i ) ; let count = 0 ; let p = 0 ; let sum = 0 ; let S = s . split ( ' ' ) ; for ( let i = 0 ; i < s . length ; ++ i ) { if ( S [ i ] == ' ' ) { ++ count ; ++ p ; } else if ( S [ i ] == ' ' ) -- count ; if ( count < 0 ) { sum += pos [ p ] - i ; let temp = S [ i ] ; S [ i ] = S [ pos [ p ] ] ; S [ pos [ p ] ] = temp ; ++ p ; count = 1 ; } } return sum ; } let s = " " ; document . write ( swapCount ( s ) + " " ) ; s = " " ; document . write ( swapCount ( s ) ) ;
Minimum Cost to cut a board into squares | method returns minimum cost to break board into m * n squares ; sort the horizontal cost in reverse order ; sort the vertical cost in reverse order ; initialize current width as 1 ; loop until one or both cost array are processed ; increase current horizontal part count by 1 ; increase current vertical part count by 1 ; loop for horizontal array , if remains ; loop for vertical array , if remains ; Driver Code
function minimumCostOfBreaking ( X , Y , m , n ) { let res = 0 ; X . sort ( ) ; X . reverse ( ) ; Y . sort ( ) ; Y . reverse ( ) ; let hzntl = 1 , vert = 1 ; let i = 0 , j = 0 ; while ( i < m && j < n ) { if ( X [ i ] > Y [ j ] ) { res += X [ i ] * vert ; hzntl ++ ; i ++ ; } else { res += Y [ j ] * hzntl ; vert ++ ; j ++ ; } } let total = 0 ; while ( i < m ) total += X [ i ++ ] ; res += total * vert ; total = 0 ; while ( j < n ) total += Y [ j ++ ] ; res += total * hzntl ; return res ; } let m = 6 , n = 4 ; let X = [ 2 , 1 , 3 , 1 , 4 ] ; let Y = [ 4 , 1 , 2 ] ; document . write ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) ;
Minimize the count of characters to be added or removed to make String repetition of same substring | Function to find the minimum of the three numbers ; Function to find the minimum number operations required to convert string str1 to str2 using the operations ; Stores the results of subproblems ; Fill dp [ ] [ ] in bottom up manner ; If str1 is empty , then insert all characters of string str2 ; Minimum operations is j ; If str2 is empty , then remove all characters of string str2 ; Minimum operations is i ; If the last characters are same , then ignore last character ; If the last character is different , then find the minimum ; Perform one of the insert , remove and the replace ; Return the minimum number of steps required ; Function to find the minimum number of steps to modify the string such that first half and second half becomes the same ; Stores the minimum number of operations required ; Traverse the given string S ; Find the minimum operations ; Update the ans ; Print the result ; Driver Code
function getMin ( x , y , z ) { return Math . min ( Math . min ( x , y ) , z ) ; } function editDistance ( str1 , str2 , m , n ) { let dp = new Array ( m + 1 ) . fill ( new Array ( n + 1 ) ) ; for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else { dp [ i ] [ j ] = 1 + getMin ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } } } return dp [ m ] [ n ] ; } function minimumSteps ( S , N ) { let ans = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { let S1 = S . substring ( 0 , i ) ; let S2 = S . substring ( i ) ; let count = editDistance ( S1 , S2 , S1 . length , S2 . length ) ; ans = Math . min ( ans , count ) ; } document . write ( ans - 1 ) ; } let S = " " ; let N = S . length ; minimumSteps ( S , N ) ;