codeparrot/xlcost-text-to-code huggingface dataset:
Find the last element after repeatedly removing every second element from either end alternately | Function to find the last element remaining in the array after performing the given operations ; Checks if traversal is from left to right or vice versa ; Store the elements currently present in the array ; Store the distance between 2 consecutive array elements ; Store the first element of the remaining array ; Iterate while elements are greater than 1 ; If left to right turn ; Update head ; Otherwise , check if the remaining elements are odd ; If true , update head ; Eleminate half of the array elements ; Double the steps after each turn ; Alter the turn ; Print the remaining element ; Driver code
function printLastElement ( arr , N ) { var leftTurn = true ; var remainElements = N ; var step = 1 ; var head = 1 ; while ( remainElements > 1 ) { if ( leftTurn ) { head = head + step ; } else { if ( remainElements % 2 == 1 ) head = head + step ; } remainElements = remainElements / 2 ; step = step * 2 ; leftTurn = ! leftTurn ; } document . write ( arr [ head - 1 ] ) ; } var arr = [ 2 , 3 , 5 , 6 ] ; var N = arr . length ; printLastElement ( arr , N ) ;
Distributed C candies among N boys such that difference between maximum and minimum candies received is K | Function to calculate the maximum and minimum number of candies a boy can possess ; All candies will be given to one boy ; All the candies will be given to 1 boy ; Give K candies to 1 st boy initially ; Count remaining candies ; If the last candy of remaining candies is given to the last boy , i . e Nth boy ; Increase minimum count ; Driver code
function max_min ( N , C , K ) { let maximum , minimum ; if ( N == 1 ) { maximum = minimum = C ; } else if ( K >= C ) { maximum = C ; minimum = 0 ; } else { maximum = K ; minimum = 0 ; let remain_candy = C - K ; maximum += Math . floor ( remain_candy / N ) ; minimum = Math . floor ( remain_candy / N ) ; if ( remain_candy % N == N - 1 ) { minimum ++ ; } } document . write ( " " + maximum + " " ) ; document . write ( " " + minimum ) ; } let N = 4 ; let C = 12 ; let K = 3 ; max_min ( N , C , K ) ;
Length of smallest subarray required to be removed to make remaining elements consecutive | Function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; Store the ending index of the longest prefix consecutive array ; Traverse the array to find the longest prefix consecutive sequence ; A [ 0. . . left_index ] is the prefix consecutive sequence ; Store the starting index of the longest suffix consecutive sequence ; Traverse the array to find the longest suffix consecutive sequence ; A [ right_index ... N - 1 ] is the consecutive sequence ; Store the smallest subarray required to be removed ; Check if subarray from the middle can be removed ; Update the right index s . t . A [ 0 , N - 1 ] is consecutive ; If updated_right < N , then update the minimumLength ; Print the required result ; Driver code
function shortestSubarray ( A , N ) { let i ; let left_index ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] + 1 != A [ i + 1 ] ) break ; } left_index = i ; let right_index ; for ( i = N - 1 ; i >= 1 ; i -- ) { if ( A [ i ] != A [ i - 1 ] + 1 ) break ; } right_index = i ; let updated_right ; let minLength = Math . min ( N - left_index - 1 , right_index ) ; if ( A [ right_index ] <= A [ left_index ] + 1 ) { updated_right = right_index + A [ left_index ] - A [ right_index ] + 1 ; if ( updated_right < N ) minLength = Math . min ( minLength , updated_right - left_index - 1 ) ; } document . write ( minLength ) ; } let arr = [ 1 , 2 , 3 , 7 , 4 , 3 , 5 ] ; let N = arr . length ; shortestSubarray ( arr , N ) ;
Check if a string can be split into 3 substrings such that one of them is a substring of the other two | Function to check if string S contains any character with frequency >= 3 or not ; Stores frequency of characters ; Iterate over the string ; Update the frequency of current character ; Iterate over the hash array ; If any character has frequency >= 3 ; Otherwise ; Driver Code
function freqCheck ( S , N ) { let hash = new Array ( 26 ) . fill ( 0 ) for ( let i = 0 ; i < N ; i ++ ) { hash [ S . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] += 1 } for ( let i = 0 ; i < 26 ; i ++ ) { if ( hash [ i ] > 2 ) { return " " } } return " " } let S = " " let N = S . length document . write ( freqCheck ( S , N ) )
Minimize length of a string by removing suffixes and prefixes of same characters | Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code
function minLength ( s ) { i = 0 ; j = s . length - 1 while ( i < j && s [ i ] == s [ j ] ) { d = s [ i ] while ( i <= j && s [ i ] == d ) i += 1 while ( i <= j && s [ j ] == d ) j -= 1 } return j - i + 1 } S = " " document . write ( minLength ( S ) )
Number of Binary Search Trees of height H consisting of H + 1 nodes | Javascript program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver Code
var mod = 1000000007 ; function power ( x , y ) { var res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function CountBST ( H ) { return power ( 2 , H ) ; } var H = 2 ; document . write ( CountBST ( H ) ) ;
Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1' ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
function rotateString ( n , m , s ) { var v = [ ] ; var cnt = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { v . push ( i ) ; } } for ( i = 1 ; i < v . length ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( v . length >= 2 && ( n - ( v [ v . length - 1 ] - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; var M = 1 ; var N = S . length ; rotateString ( N , M , S ) ;
Number obtained by reducing sum of digits of 2 N into a single digit | Function to find the number obtained by reducing sum of digits of 2 ^ N into a single digit ; Stores answers for different values of N ; Driver Code
function findNumber ( N ) { let ans = [ 1 , 2 , 4 , 8 , 7 , 5 ] ; return ans [ N % 6 ] ; } let N = 6 ; document . write ( findNumber ( N ) + " " ) ;
Check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other | Function to check if two given piles can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other ; If maximum of A & B exceeds the twice of minimum of A & B ; Not possible to empty the piles ; If sum of both the coins is divisible by 3 , then print Yes ; Otherwise , print " No " ; Driver Code
function canBeEmptied ( A , B ) { if ( Math . max ( A , B ) > 2 * Math . min ( A , B ) ) { document . write ( " " ) ; return ; } if ( ( A + B ) % 3 == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let A = 1 , B = 2 ; canBeEmptied ( A , B ) ;
Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Given value of N & B ; Function call to find maximum index the pointer can reach
function maximumIndex ( N , B ) { let i = 0 , j = 1 ; let cnt = 0 ; let sum = Math . floor ( N * ( N + 1 ) / 2 ) ; let flag = false ; while ( cnt < N ) { i += j ; j ++ ; cnt ++ ; if ( i == B ) { flag = true ; break ; } } if ( ! flag ) { document . write ( sum ) ; } else document . write ( sum - 1 ) ; } let N = 4 , B = 6 ; maximumIndex ( N , B ) ;
Generate longest possible array with product K such that each array element is divisible by its previous adjacent element | Function to construct longest array with product K such that each element is divisible by its previous element ; Stores the prime factors of K ; Stores the power to which primefactor i is raised ; Sort prime factors in descending order ; Stores the final array ; Multiply the last element by K ; Print the constructed array ; Driver Code
function findLongestArray ( K ) { let primefactors = [ ] ; let K_temp = K ; for ( let i = 2 ; i * i <= K ; i ++ ) { let count = 0 ; while ( K_temp % i == 0 ) { K_temp = Math . floor ( K_temp / i ) ; count ++ ; } if ( count > 0 ) primefactors . push ( [ count , i ] ) ; } if ( K_temp != 1 ) primefactors . push ( [ 1 , K_temp ] ) ; primefactors . sort ( function ( x , y ) { if ( x [ 0 ] != y [ 0 ] ) return y [ 0 ] - x [ 0 ] ; return y [ 1 ] - x [ 1 ] ; } ) ; let n = primefactors [ 0 ] [ 0 ] ; let val = primefactors [ 0 ] [ 1 ] ; let answer = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { answer [ i ] = val ; } answer [ n - 1 ] *= K ; for ( let i = 0 ; i < n ; i ++ ) { answer [ n - 1 ] = Math . floor ( answer [ n - 1 ] / val ) ; } document . write ( " " ) ; for ( let i = 0 ; i < answer . length ; i ++ ) { if ( i == answer . length - 1 ) document . write ( answer [ i ] + " " ) ; else document . write ( answer [ i ] + " " ) ; } } let K = 4 ; findLongestArray ( K ) ;
Minimum number of steps required to place all 1 s at a single index | Function to prlet minimum steps required to shift all 1 s to a single index in a binary array ; Size of array ; Used to store cumulative sum ; Initialize count ; Traverse the array in forward direction ; Steps needed to store all previous ones to ith index ; Count number of 1 s present till i - th index ; Initialize count ; Traverse the array in backward direction ; Steps needed to store all 1 s to the right of i at current index ; Count number of 1 s present after i - th index ; Print the number of steps required ; Driver code
function minsteps ( A ) { let n = A . length ; let left = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let right = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let res = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let count = A [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { left [ i ] = left [ i - 1 ] + count ; count += A [ i ] ; } count = A [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] + count ; count += A [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { res [ i ] = left [ i ] + right [ i ] ; document . write ( res [ i ] + " " ) ; } document . write ( " " ) ; } let A = [ 1 , 0 , 1 , 0 ] ; minsteps ( A ) ;
Minimize flips on K | Function to find the minimum number K - length subarrays required to be flipped to make all array elements 1 ; Stores whether an element can be flipped or not ; Store the required number of flips ; Traverse the array , A [ ] ; Find the prefix sum for the indices i > 0 ; Check if the current element is required to be flipped ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; Print the result ; Driver Code
function minimumOperations ( A , K ) { let isflipped = [ ] ; for ( let i = 0 ; i < A . length + 1 ; i ++ ) { isflipped [ i ] = 0 ; } let ans = 0 ; for ( let i = 0 ; i < A . length ; i ++ ) { if ( i > 0 ) { isflipped [ i ] += isflipped [ i - 1 ] ; isflipped [ i ] %= 2 ; } if ( A [ i ] == 0 && isflipped [ i ] == 0 ) { if ( ( A . length - i + 1 ) <= K ) { document . write ( - 1 ) ; return ; } ans ++ ; isflipped [ i ] ++ ; isflipped [ i + K ] -- ; } else if ( A [ i ] == 1 && isflipped [ i ] != 0 ) { if ( ( A . length - i + 1 ) <= K ) { document . write ( - 1 ) ; return ; } ans ++ ; isflipped [ i ] ++ ; isflipped [ i + K ] -- ; } } document . write ( ans ) ; } let arr = [ 0 , 1 , 0 ] ; let K = 1 ; minimumOperations ( arr , K ) ;
Calculate sum of scores after N days based on given conditions | Function to c sum of calculate sum of scores after n days ; Store the required sum ; Store the score on previous monday and current day respectively ; Iterate over the range [ 1 , n ] ; If the current day is monday ; Increment score of prev_monday by 1 ; Update score of current day ; Add score of current day and increment score for next day ; Print the result ; Driver code
function findScoreSum ( n ) { let total = 0 ; let prev_monday = 0 , curr_day = 0 ; for ( let day = 1 ; day <= n ; day ++ ) { if ( day % 7 == 1 ) { prev_monday ++ ; curr_day = prev_monday ; } total += curr_day ++ ; } document . write ( total ) ; } let N = 8 ; findScoreSum ( N ) ;
Calculate sum of scores after N days based on given conditions | Function to calculate sum of scores after n days ; Store the number of full weeks ; Stores the remaining days in the last week ; Store the sum of scores in the first F full weeks ; Store the sum of scores in the last week ; Prlet the result ; Driver code
function findScoreSum ( n ) { let F = n / 7 ; let D = n % 7 ; let fullWeekScore = ( 49 + 7 * F ) * F / 2 ; let lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ; document . write ( Math . floor ( fullWeekScore + lastNonFullWeekScore ) ) ; } let N = 8 ; findScoreSum ( N ) ;
Largest number up to N whose modulus with X is equal to Y modulo X | Function to print the largest number upto N whose modulus with X is same as Y % X ; Stores the required number ; Update num as the result ; Return the resultant number ; Driver code
function maximumNum ( X , Y , N ) { let num = 0 ; if ( N - N % X + Y <= N ) { num = N - N % X + Y ; } else { num = N - N % X - ( X - Y ) ; } return num ; } let X = 10 ; let Y = 5 ; let N = 15 ; document . write ( maximumNum ( X , Y , N ) ) ;
Generate an N | Function to calculate GCD of two integers ; Function to calculate GCD of a given array ; Utility function to check for all the possible combinations ; If an N - length sequence is obtained ; If GCD of the sequence is K ; Otherwise ; Add an element from the first array ; Recursively proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Add an element from the second array ; Recursive proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Function to check all the possible combinations ; Stores the subsequence ; If GCD is not equal to K for any combination ; Given arrays ; Given value of K ; Function call to generate the required subsequence
function GCD ( a , b ) { if ( b < 1 ) return a ; return GCD ( b , a % b ) ; } function GCDArr ( a ) { let ans = a [ 0 ] ; for ( let i = 0 ; i < a . length ; i ++ ) ans = GCD ( ans , a [ i ] ) ; return ans ; } function findSubseqUtil ( a , b , ans , k , i ) { if ( ans . length == a . length ) { if ( GCDArr ( ans ) == k ) { document . write ( " " ) ; let m = ans . length ; for ( let j = 0 ; j < m - 1 ; j ++ ) document . write ( ans [ j ] + " " ) ; document . write ( ans [ m - 1 ] + " " ) ; return true ; } else return false ; } ans . push ( a [ i ] ) ; let temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . splice ( ans . length - 1 , 1 ) ; ans . push ( b [ i ] ) ; temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . splice ( ans . length - 1 , 1 ) ; return false ; } function findSubseq ( A , B , K , i ) { let ans = [ ] ; findSubseqUtil ( A , B , ans , K , i ) ; if ( ans . length < 1 ) document . write ( - 1 ) ; } let A = [ 5 , 3 , 6 , 2 , 9 ] ; let B = [ 21 , 7 , 14 , 12 , 28 ] ; let K = 3 ; findSubseq ( A , B , K , 0 ) ;
Minimize count of swaps of adjacent elements required to make an array increasing | Function to count minimum number of operations required to obtain an increasing array from given array A ; Store the required result ; Traverse the array A ; If the current element is not in its correct position ; Check if it is present at index i - 1 ; Check if it is present at index i - 2 ; Otherwise , print - 1 ( Since A [ i ] can not be swapped more than twice ) ; print the result ; Given array ; Store the size of the array
function minimumOperations ( A , n ) { var cnt = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != ( i + 1 ) ) { if ( ( ( i - 1 ) >= 0 ) && A [ i - 1 ] == ( i + 1 ) ) { cnt ++ ; var t = A [ i ] ; A [ i ] = A [ i - 1 ] ; A [ i - 1 ] = t ; } else if ( ( ( i - 2 ) >= 0 ) && A [ i - 2 ] == ( i + 1 ) ) { cnt += 2 ; A [ i - 2 ] = A [ i - 1 ] ; A [ i - 1 ] = A [ i ] ; A [ i ] = i + 1 ; } else { document . write ( - 1 ) ; return ; } } } document . write ( cnt ) ; } var A = [ 7 , 3 , 2 , 1 , 4 ] ; var n = A . length ; minimumOperations ( A , n ) ;
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Set | Function to count the occurrences of X in the generated square matrix ; Store the required result ; Iterate over the range [ 1 , N ] ; Check if x is a multiple of i or not ; Check if the other multiple exists in the range or not ; Print the result ; Driver Code
function countOccurrences ( n , x ) { var count = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { if ( x % i == 0 ) { if ( x / i <= n ) count ++ ; } } document . write ( count ) ; } var N = 7 , X = 12 ; countOccurrences ( N , X ) ;
Reduce all array elements to zero by performing given operations thrice | Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Input ; Function call to make all array elements equal to 0
function ConvertArray ( arr , N ) { if ( N == 1 ) { document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + - 1 * arr [ 0 ] + " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + 1 * arr [ 0 ] + " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + - 1 * arr [ 0 ] + " " ) ; } else { document . write ( " " + 1 + " " + N + " " ) ; document . write ( " " ) ; for ( i = 0 ; i < N ; i ++ ) { document . write ( - 1 * arr [ i ] * N + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + ( N - 1 ) + " " ) ; document . write ( " " ) ; for ( i = 0 ; i < N - 1 ; i ++ ) { document . write ( arr [ i ] * ( N - 1 ) + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + N + " " + N + " " ) ; document . write ( " " ) ; document . write ( arr [ N - 1 ] * ( N - 1 ) + " " ) ; } } var arr = [ 1 , 3 , 2 , 4 ] ; var N = arr . length ; ConvertArray ( arr , N ) ;
Count pairs from an array with even product of count of distinct prime factors | js program for the above approach ; Function to calculate count of distinct prime factors of a number ; Sieve of Eratosthenes ; Function to count pairs with even product of distinct prime factors ; Stores count of distinct prime factors ; Stores the count of numbers with even prime factors in B [ ] ; Stores the count of numbers with odd prime factors in B [ ] ; Even Product Pairs ; Traverse the array B [ ] ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; Increment oddCount by 1 ; Increment evenCount by 1 ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; odd * even = even ; If count of prime factors is even ; even * odd = even even * even = even ; Driver Code
let countDistinct = Array ( 1000000 + 1 ) ; function countOfPrimefactors ( CountDistinct ) { let MAX = 1000000 ; let prime = Array ( MAX + 1 ) ; for ( let i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( let i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( let j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } } function CountEvenPair ( A , B , N , M ) { let MAX = 1000000 ; countOfPrimefactors ( countDistinct ) ; let evenCount = 0 ; let oddCount = 0 ; let evenPairs = 0 ; for ( let i = 0 ; i < M ; i ++ ) { if ( countDistinct [ B [ i ] ] == 0 ) continue ; if ( ( countDistinct [ B [ i ] ] & 1 ) != 0 ) { oddCount ++ ; } else { evenCount ++ ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( countDistinct [ A [ i ] ] == 0 ) continue ; if ( ( countDistinct [ A [ i ] ] & 1 ) != 0 ) { evenPairs += ( evenCount ) ; } else { evenPairs += evenCount + oddCount ; } } return evenPairs ; } let A = [ 1 , 2 , 3 ] ; let B = [ 4 , 5 , 6 ] ; let N = A . length ; let M = B . length ; document . write ( CountEvenPair ( A , B , N , M ) ) ;
Minimum array elements required to be subtracted from either end to reduce K to 0 | Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code
function longestSubarray ( arr , N , K ) { let um = new Map ( ) ; let sum = 0 , maxLen = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( sum == K ) maxLen = i + 1 ; if ( ! um . has ( sum ) ) um . set ( sum , i ) ; if ( um . has ( sum - K ) ) { if ( maxLen < ( i - um . get ( sum - K ) ) ) maxLen = i - um . get ( sum - K ) ; } } return maxLen ; } function minRequiredOperation ( arr , N , K ) { let TotalSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) TotalSum += arr [ i ] ; let maxLen = longestSubarray ( arr , N , TotalSum - K ) ; if ( maxLen == - 1 ) { document . write ( - 1 ) ; } else document . write ( N - maxLen ) ; } let arr = [ 1 , 3 , 1 , 1 , 2 ] ; let K = 4 ; let N = arr . length minRequiredOperation ( arr , N , K ) ;
Minimum number of digits required to be removed to make a number divisible by 4 | Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Print the result ; Driver Code
function minimumDeletions ( s ) { let n = s . length ; let ans = n ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let t = s [ i ] - ' ' ; if ( t % 2 == 0 ) { for ( let j = i - 1 ; j >= 0 ; j -- ) { let num = ( s [ j ] - ' ' ) * 10 + t ; if ( num % 4 === 0 ) { let k1 = i - j - 1 ; let k2 = n - i - 1 ; ans = Math . min ( ans , k1 + k2 ) ; } } } } if ( ans === n ) { for ( let i = 0 ; i < n ; i ++ ) { let num = s [ i ] - ' ' ; if ( num % 4 === 0 ) { ans = n - 1 ; } } } document . write ( ans ) ; } let str = " " ; minimumDeletions ( str ) ;