codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of times Array can be reduced in half when its all elements are even | Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code
function arrayDivisionByTwo ( arr , n ) { let cnt = 0 ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = Math . floor ( arr [ i ] / 2 ) ; cnt ++ ; } ans = Math . min ( ans , cnt ) ; } return ans ; } let arr = [ 8 , 12 , 40 ] ; let n = arr . length ; document . write ( arrayDivisionByTwo ( arr , n ) ) ;
Find smallest number with given digits and sum of digits | Function to print the minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and Q ; If P and Q are both zero or if Q is zero and N is not divisible by P then there is no possible integer which satisfies the given conditions ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver Code ; Function Call
function printMinInteger ( P , Q , N ) { if ( Q > P ) { swap ( P , Q ) ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { document . write ( " " ) ; return ; } let count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += Math . floor ( N / P ) ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( let i = 0 ; i < count_Q ; i ++ ) document . write ( Q ) ; for ( let i = 0 ; i < count_P ; i ++ ) document . write ( P ) ; } else { document . write ( " " ) ; } } let N = 32 ; let P = 7 ; let Q = 4 ; printMinInteger ( P , Q , N ) ;
Minimum number of sum and modulo operations using given numbers to reach target | Function to find the minimum moves to reach K from N ; Initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; Traversing through the buttons ; Iterating through all the positions ; If not visited ; Next status of lock ; Advance to next state ; Return the final dp [ target ] ; Given Input ; Function Call
function minPushes ( N , K , arr ) { let dp = new Array ( 100000 ) . fill ( - 1 ) ; dp [ N ] = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let xx = 0 ; xx < 100000 ; xx ++ ) { let x = xx ; if ( dp [ x ] == - 1 ) continue ; let next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == - 1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } let N = 99880 , K = 89 ; let arr = [ 100 , 3 ] ; document . write ( minPushes ( N , K , arr ) ) ;
Minimum number of Apples to be collected from trees to guarantee M red apples | Function to minimum no . of apples ; If we get all required apple from South ; If we required trees at East and West ; If we doesn 't have enough  red apples ; No . of red apple for gift ; No . of red apple in each tree ; No . of tree in North ; No . of tree in South ; No . of tree in West ; No . of tree in East ; Function Call
function minApples ( ) { if ( M <= S * K ) return M ; else if ( M <= S * K + E + W ) return S * K + ( M - S * K ) * K ; else return - 1 ; } M = 10 K = 15 N = 0 S = 1 W = 0 E = 0 ans = minApples ( ) document . write ( ans ) ;
Minimum increments or decrements required to signs of prefix sum array elements alternating | Function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; Case 1. neg - pos - neg ; Stores the current sign of the prefix sum of array ; Stores minimum number of operations for Case 1 ; Traverse the array ; Checking both conditions ; Update the current prefix1 to currentPrefixSum ; Case 2. pos - neg - pos ; Stores the prefix sum of array ; Stores minimum number of operations for Case 1 ; Checking both conditions ; Update the current prefix2 to currentPrefixSum ; Driver Code
function minimumOperations ( A , N ) { let cur_prefix_1 = 0 ; let parity = - 1 ; let minOperationsCase1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += Math . abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= - 1 ; } let cur_prefix_2 = 0 ; parity = 1 ; let minOperationsCase2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += Math . abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= - 1 ; } return Math . min ( minOperationsCase1 , minOperationsCase2 ) ; } let A = [ 1 , - 3 , 1 , 0 ] ; let N = A . length ; document . write ( minimumOperations ( A , N ) ) ;
Generate an array of maximum sum such that each element exceeds all elements present either on its left or right | Function to construct the array having maximum sum satisfying the given criteria ; Declaration of the array arrA [ ] and ans [ ] ; Stores the maximum sum of the resultant array ; Initialize the array arrA [ ] ; Traversing the array arrA [ ] ; Initialize the array arrB [ ] ; Assign the maximum element to the current element ; Form the first increasing till every index i ; Make the current element as the maximum element ; Forming decreasing from the index i + 1 to the index N ; Initialize the sum ; Find the total sum ; Check if the total sum is at least the sum found then make ans as ansB ; Print the final array formed ; Driver Code
function maximumSumArray ( arr , N ) { let arrA = new Array ( N ) ; let ans = new Array ( N ) ; let maxSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) arrA [ i ] = arr [ i ] ; for ( let i = 0 ; i < N ; i ++ ) { let arrB = new Array ( N ) ; let maximum = arrA [ i ] ; arrB [ i ] = maximum ; for ( let j = i - 1 ; j >= 0 ; j -- ) { arrB [ j ] = Math . min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } maximum = arrA [ i ] ; for ( let j = i + 1 ; j < N ; j ++ ) { arrB [ j ] = Math . min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } let sum = 0 ; for ( let j = 0 ; j < N ; j ++ ) sum += arrB [ j ] ; if ( sum > maxSum ) { maxSum = sum ; ans = arrB ; } } for ( let val of ans ) { document . write ( val + " " ) ; } } let A = [ 10 , 6 , 8 ] ; let N = A . length ; maximumSumArray ( A , N ) ;
Minimize product of two scores possible by at most M reductions | Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; Case 2 - R2 reduces first ; Given Input ; Function Call
function minProductUtil ( R1 , B1 , R2 , B2 , M ) { let x = Math . min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { let y = Math . min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } function minProduct ( R1 , B1 , R2 , B2 , M ) { let res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; let res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . min ( res1 , res2 ) ; } let R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; document . write ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ;
Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | Function to find maximum profit after selling K tickets ; Frequency array to store freq of every element of the array ; Modify the arr [ ] so that the array is sorted in O ( N ) ; Variable to store answer ; Traverse the array while K > 0 and j >= 0 ; If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ] ; If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; Sell tickets from counter [ j + 1 , N ] ; All elements of array are equal Send tickets from each counter 1 time until K > 0. ; Converting answer from long long to let ; Given Input ; Function Call
function maxAmount ( n , k , arr ) { let A = new Array ( 1000001 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } let j = 0 ; for ( let i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } let ans = 0 ; let mod = 1e9 + 7 ; let i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + Math . min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; let x = ans ; return x ; } let n = 5 ; let k = 3 ; let arr = [ 4 , 3 , 6 , 2 , 4 ] ; let ans = maxAmount ( n , k , arr ) ; document . write ( ans ) ;
Maximize sum of averages of subsequences of lengths lying in a given range | Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code
function maxAverage ( A , N , X , Y ) { A . sort ( function ( a , b ) { return a - b ; } ) let sum = 0 ; let res = 0 ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; let cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += sum / X ; break ; } res += sum / X ; sum = 0 ; count = 0 ; } } document . write ( res . toPrecision ( 3 ) ) } let A = [ 4 , 10 , 6 , 5 ] ; let N = A . length ; let X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ;
Lexicographically largest string possible by repeatedly appending first character of two given strings | Function to make the lexicographically largest let by merging two lets ; Stores the resultant let ; If the let word1 is lexographically greater than or equal to word ; Update the let merge ; Erase the first index of the let word1 ; Otherwise ; Update the let merge ; Erase the first index of the let word2 ; Return the final let ; Driver Code
function largestMerge ( word1 , word2 ) { let merge = " " ; while ( word1 . length != 0 word2 . length != 0 ) { if ( word1 . localeCompare ( word2 ) == 0 || ( word1 . localeCompare ( word2 ) > 0 ) ) { merge = merge + word1 [ 0 ] ; word1 = word1 . substring ( 1 ) ; } else { merge = merge + word2 [ 0 ] ; word2 = word2 . substring ( 1 ) ; } } return merge ; } let S1 = " " ; let S2 = " " ; document . write ( largestMerge ( S1 , S2 ) ) ;
Maximum rods to put horizontally such that no two rods overlap on X coordinate | Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver Code
function findMaximumPoints ( N , X , H ) { let ans = 0 ; let prev = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } ans ++ ; return ans ; } let X = [ 1 , 2 , 3 ] ; let H = [ 2 , 5 , 5 ] ; let N = X . length ; document . write ( findMaximumPoints ( N , X , H ) ) ;
Maximize the missing values in given time in HH : MM format | Function to find the maximum time by replacing ' ? ' by any digits ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code
function maxTime ( s ) { s = s . split ( " " ) if ( s [ 0 ] == ' ' ) { if ( s [ 1 ] <= ' ' s [ 1 ] == ' ' ) s [ 0 ] = ' ' ; else s [ 0 ] = ' ' ; } if ( s [ 1 ] == ' ' ) { if ( s [ 0 ] != ' ' ) { s [ 1 ] = 9 ; } else s [ 1 ] = 3 ; } if ( s [ 3 ] == ' ' ) s [ 3 ] = ' ' ; if ( s [ 4 ] == ' ' ) s [ 4 ] = ' ' ; document . write ( s . join ( " " ) ) ; } let S = " " ; maxTime ( S ) ;
Maximum GCD of two numbers possible by adding same value to them | Function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; Given Input
function maxGcd ( a , b ) { document . write ( Math . abs ( a - b ) ) ; } let a = 2231 ; let b = 343 ; maxGcd ( a , b ) ;
Count the combination of 4 s and / or 5 s required to make each Array element 0 | Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Given Input ; Function Call
function sumOfCombinationOf4OR5 ( arr , N ) { let ans = new Array ( N ) . fill ( - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } let sum = Number . MAX_SAFE_INTEGER , cnt = 0 ; for ( let j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . min ( sum , cnt + Math . floor ( ( arr [ i ] - j ) / 5 ) ) ; } cnt ++ ; } if ( sum != Number . MAX_SAFE_INTEGER ) ans [ i ] = sum ; } for ( let num of ans ) document . write ( num + " " ) ; } let arr = [ 7 , 15 , 17 , 22 ] ; let N = arr . length ; sumOfCombinationOf4OR5 ( arr , N ) ;
Find an N | Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call
function printBinaryString ( arr , N ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( i % 2 ) { document . write ( 0 ) ; } else { document . write ( 1 ) ; } } } let N = 5 , M = 3 ; let arr = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] ] ; printBinaryString ( arr , N ) ;
Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | JavaScript program for the above approach ; Function to find the maximum number of 0 s in the array A [ ] after changing the array element to A [ i ] * D + B [ i ] ; Stores the frequency of fractions needed to make each element 0 ; Stores the maximum number of 0 ; Traverse the array ; Find the numerator and the denominator ; Check if den is not equal to 0 ; Divide num and den by their gcd ; Check if num is not greater than 0 ; Check if both num and den are equal to 0 ; Increment the value of { num , den } in the map ; Update the value of ans ; Print the value of ans + cnt ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function maxZeroes ( A , B ) { let mp = new Map ( ) ; let N = A . length ; let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let num = - B [ i ] ; let den = A [ i ] ; let gc = __gcd ( num , den ) ; if ( den != 0 ) { num /= gc ; den /= gc ; } if ( num <= 0 ) { num *= - 1 ; den *= - 1 ; } if ( den == 0 && num == 0 ) cnt ++ ; if ( den != 0 ) { if ( mp . has ( ` ${ num } ${ den } ` ) ) { mp . set ( ` ${ num } ${ den } ` , mp . get ( ` ${ num } ${ den } ` ) + 1 ) } else { mp . set ( ` ${ num } ${ den } ` , 1 ) } ans = Math . max ( mp . get ( ` ${ num } ${ den } ` ) , ans ) ; } } document . write ( ans + cnt + " " ) ; } let A = [ 1 , 2 , - 1 ] ; let B = [ - 6 , - 12 , 6 ] ; maxZeroes ( A , B ) ;
Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Pointer of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code
function MinCost ( days , cost , N ) { let size = days [ N - 1 ] + 1 ; let dp = new Array ( size ) ; dp [ size - 1 ] = Math . min ( cost [ 0 ] , Math . min ( cost [ 1 ] , cost [ 2 ] ) ) ; let ptr = N - 2 ; for ( let i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { let val1 = dp [ i + 1 ] + cost [ 0 ] ; let val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; let val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . min ( val1 , Math . min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } let arr = [ 2 , 4 , 6 , 7 , 8 , 10 , 17 ] ; let cost = [ 3 , 8 , 20 ] ; let N = arr . length ; document . write ( MinCost ( arr , cost , N ) ) ;
Minimum count of elements to be inserted in Array to form all values in [ 1 , K ] using subset sum | Function to find the count of minimum elements to be inserted to form every number in a range ; Stores the count of numbers needed ; Stores the numbers upto which every numbers can be formed ; Stores the index of the array arr [ ] ; Iterate until requiredSum is less than or equal to K ; If i is less than N and requiredSum is greater than or equal to arr [ i ] ; Increment requiredSum by arr [ i ] ; Increment i by 1 ; Otherwise ; Increment count by 1 ; Increment requiredSum by requiredSum ; Return result ; Input ; Function Call
function minElements ( arr , N , K ) { let count = 0 ; let requiredNum = 1 ; let i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } let arr = [ 1 , 3 ] ; let K = 6 ; let N = arr . length ; document . write ( minElements ( arr , N , K ) + " " ) ;
Theft at World Bank | Custom comparator ; Function to find the maximum profit ; Stores the pairs of elements of B and A at the same index ; Iterate over the range [ 0 , N ] ; If current integer is perfect square ; Push the pair of B [ i ] and A [ i ] in vector V ; Sorts the vector using the custom comparator ; Stores the maximum profit ; Traverse the vector V ; If V [ i ] [ 1 ] is less than W ; Increment profit by V [ i ] [ 0 ] ; Decrement V [ i ] [ 1 ] from W ; Otherwise ; Update profit ; Return the value of profit ; Driver Code
function comp ( p1 , p2 ) { let a = p1 [ 0 ] , b = p1 [ 1 ] ; let c = p2 [ 0 ] , d = p2 [ 1 ] ; let val1 = 0 , val2 = 0 ; val1 = Math . floor ( a / b ) ; val2 = Math . floor ( c / d ) ; return val1 > val2 ; } function maximumProfit ( A , B , N , W ) { let V = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let temp = Math . sqrt ( A [ i ] ) ; if ( temp * temp == A [ i ] ) continue ; V . push ( [ B [ i ] , A [ i ] ] ) ; } V . sort ( comp ) ; let profit = 0.00 ; for ( let i = 0 ; i < V . length ; i ++ ) { if ( V [ i ] [ 1 ] <= W ) { profit += V [ i ] [ 0 ] ; W -= V [ i ] [ 1 ] ; } else { profit += V [ i ] [ 0 ] * ( W / V [ i ] [ 1 ] ) ; break ; } } return profit . toFixed ( 5 ) ; } let N = 3 ; let W = 10 ; let A = [ 4 , 5 , 7 ] ; let B = [ 8 , 5 , 4 ] ; document . write ( maximumProfit ( A , B , N , W ) + " " ) ;
Maximize the count of adjacent element pairs with even sum by rearranging the Array | Function to find maximum count pair of adjacent elements with even sum ; Stores count of odd numbers ; Stores count of even numbers ; Traverse the array arr [ ] ; If arr [ i ] % 2 is 1 ; Else ; If odd and even both are greater than 0 ; Otherwise ; Driver Code
function maximumCount ( arr , N ) { let odd = 0 ; let even = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) odd ++ ; else even ++ ; } if ( odd && even ) return N - 2 ; else return N - 1 ; } let arr = [ 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 ] ; let N = arr . length ; document . write ( maximumCount ( arr , N ) ) ;
Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code
function minPossibleValue ( N , K , X ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } let N = 7 , K = 4 , X = 1 ; document . write ( minPossibleValue ( N , K , X ) ) ;
Smallest number that can replace all | Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code
function findMissingValue ( arr , N ) { let minE = Number . MAX_VALUE , maxE = Number . MIN_VALUE ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Number . MAX_VALUE && maxE == Number . MIN_VALUE ) { document . write ( " " ) ; } else { document . write ( ( minE + maxE ) / 2 ) ; } } let arr = [ 1 , - 1 , - 1 , - 1 , 5 ] ; let N = arr . length ; findMissingValue ( arr , N ) ;
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times | Function to find the last element after performing given operations ; Length of the array ; Increment j until condition is satisfied ; In each pair every value is repeating r number of times ; Print the result according to the value of k ; Given K ; Given arr [ ] ; Function call
function findLastElement ( N , A ) { let l = A . length ; let j = 0 ; while ( N > l * ( Math . pow ( 2 , j ) ) ) { N = N - l * Math . pow ( 2 , j ) ; j += 1 ; } let k = 1 ; let r = Math . pow ( 2 , j ) ; for ( let i = 1 ; i < l ; i ++ ) { if ( N > r * i ) k += 1 ; } for ( let i = 0 ; i < l ; i ++ ) { if ( i + 1 == k ) { document . write ( A [ i ] ) ; return ; } } } let K = 7 ; let A = [ 1 , 2 , 3 ] ; findLastElement ( K , A ) ;
Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findMaxD ( arr , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . abs ( arr [ i ] - K ) ; } let D = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } let arr = [ 1 , 7 , 11 ] ; let N = arr . length ; let K = 3 ; document . write ( findMaxD ( arr , N , K ) ) ;
Maximize the number of times a character can be removed from substring 01 from given Binary String | Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Input ; Function call
function maxOperations ( S , N ) { let X = 0 ; let Y = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) break ; Y ++ ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } let S = " " ; let N = S . length ; document . write ( maxOperations ( S , N ) ) ;