codeparrot/xlcost-text-to-code huggingface dataset:
Minimum removals required such that sum of remaining array modulo M is X | Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Given array ; Given size ; Given mod and x ; Function call
function findSum ( S , n , x ) { let table = new Array ( n + 1 ) ; for ( var i = 0 ; i < table . length ; i ++ ) { table [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < table . length ; i ++ ) { for ( var j = 0 ; j < table . length ; j ++ ) { table [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= x ; i ++ ) { table [ 0 ] [ i ] = Number . MAX_VALUE - 1 ; } for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= x ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return ( table [ n ] [ x ] > n ) ? - 1 : table [ n ] [ x ] ; } function minRemovals ( arr , n , m , x ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } let requied_Sum = 0 ; if ( sum % m < x ) requied_Sum = m + sum % m - x ; else requied_Sum = sum % m - x ; document . write ( findSum ( arr , n , requied_Sum ) ) ; } let arr = [ 3 , 2 , 1 , 2 ] ; let n = arr . length ; let m = 4 , x = 2 ; minRemovals ( arr , n , m , x % m ) ;
Count minimum character replacements required such that given string satisfies the given conditions | Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Given string S ; Function Call
function minChange ( s , n ) { var L = new Array ( 26 ) . fill ( 0 ) ; var R = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n / 2 ; i ++ ) { var ch = s [ i ] . charCodeAt ( 0 ) ; L [ ch - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = n / 2 ; i < n ; i ++ ) { var ch = s [ i ] . charCodeAt ( 0 ) ; R [ ch - " " . charCodeAt ( 0 ) ] ++ ; } var count = n ; for ( var ch = " " . charCodeAt ( 0 ) ; ch <= " " . charCodeAt ( 0 ) ; ch ++ ) { count = Math . min ( count , n - L [ ch - " " . charCodeAt ( 0 ) ] - R [ ch - " " . charCodeAt ( 0 ) ] ) ; } var change = parseInt ( n / 2 ) ; for ( var d = 0 ; d + 1 < 26 ; d ++ ) { change -= L [ d ] ; change += R [ d ] ; count = Math . min ( count , change ) ; } change = n / 2 ; for ( var d = 0 ; d + 1 < 26 ; d ++ ) { change -= R [ d ] ; change += L [ d ] ; count = Math . min ( change , count ) ; } return count ; } var S = " " ; var N = S . length ; document . write ( minChange ( S , N ) ) ;
Rearrange string to obtain Longest Palindromic Substring | Function to rearrange the string to get the longest palindromic substring ; Stores the length of str ; Store the count of occurrence of each character ; Traverse the string , str ; Count occurrence of each character ; Store the left half of the longest palindromic substring ; Store the right half of the longest palindromic substring ; Traverse the array , hash [ ] ; Append half of the characters to res1 ; Append half of the characters to res2 ; Reverse string res2 to make res1 + res2 palindrome ; Store the remaining characters ; Check If any odd character appended to the middle of the resultant string or not ; Append all the character which occurs odd number of times ; If count of occurrence of characters is odd ; Driver Code
function longestPalinSub ( str ) { var N = str . length ; var hash = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var res1 = [ ] ; var res2 = [ ] ; for ( var i = 0 ; i < 256 ; i ++ ) { for ( var j = 0 ; j < parseInt ( hash [ i ] / 2 ) ; j ++ ) { res1 . push ( String . fromCharCode ( i ) ) ; } for ( var j = parseInt ( ( hash [ i ] + 1 ) / 2 ) ; j < hash [ i ] ; j ++ ) { res2 . push ( String . fromCharCode ( i ) ) ; } } res2 = res2 . reverse ( ) ; var res3 = [ ] ; var f = false ; for ( var i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % 2 ) { if ( ! f ) { res1 . push ( String . fromCharCode ( i ) ) ; f = true ; } else { res3 . push ( String . fromCharCode ( i ) ) ; } } } return ( res1 . join ( ' ' ) + res2 . join ( ' ' ) + res3 . join ( ' ' ) ) ; } var str = " " ; document . write ( longestPalinSub ( str ) ) ;
Maximize subsequences having array elements not exceeding length of the subsequence | Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from key ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of arr [ ] ; Update the frequency ; Print the number of subsequences ; Given array arr [ ] ; Function Call
function No_Of_subsequences ( mp ) { var count = 0 ; var left = 0 ; mp . forEach ( ( value , key ) => { value += left ; count += ( value / key ) ; left = value % key ; } ) ; return count ; } function maximumsubsequences ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) ; } document . write ( No_Of_subsequences ( mp ) ) ; } var arr = [ 1 , 1 , 1 , 1 ] ; var N = arr . length ; maximumsubsequences ( arr , N ) ;
Check if concatenation of any permutation of given list of arrays generates the given array | Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Given target list ; Given array of list ; Function call
function check ( arr , pieces ) { var m = new Map ( ) ; for ( var i = 0 ; i < arr . length ; i ++ ) m . set ( arr [ i ] , i + 1 ) for ( var i = 0 ; i < pieces . length ; i ++ ) { if ( pieces [ i ] . length == 1 && m . get ( pieces [ i ] [ 0 ] ) != 0 ) { continue ; } else if ( pieces [ i ] . length > 1 && m . get ( pieces [ i ] [ 0 ] ) != 0 ) { var idx = m . get ( pieces [ i ] [ 0 ] ) - 1 ; idx ++ ; if ( idx >= arr . length ) return false ; for ( var j = 1 ; j < pieces [ i ] . length ; j ++ ) { if ( arr [ idx ] == pieces [ i ] [ j ] ) { idx ++ ; if ( idx >= arr . length && j < pieces [ i ] . length - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } var arr = [ 1 , 2 , 4 , 3 ] ; var pieces = [ [ 1 ] , [ 4 , 3 ] , [ 2 ] ] ; if ( check ( arr , pieces ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Modify given array to make sum of odd and even indexed elements same | Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Given array arr [ ] ; Function Call
function makeArraySumEqual ( a , N ) { let count_0 = 0 , count_1 = 0 ; let odd_sum = 0 , even_sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( let i = 0 ; i < N ; i ++ ) document . write ( a [ i ] + " " ) ; } else { if ( count_0 >= N / 2 ) { for ( let i = 0 ; i < count_0 ; i ++ ) document . write ( " " ) ; } else { let is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( let i = 0 ; i < count_1 ; i ++ ) document . write ( " " ) ; } } } let arr = [ 1 , 1 , 1 , 0 ] ; let N = arr . length ; makeArraySumEqual ( arr , N ) ;
Minimum replacement of pairs by their LCM required to reduce given array to its LCM | javascript program for the above approach ; Boolean array to set or unset prime non - prime indices ; Stores the prefix sum of the count of prime numbers ; Function to check if a number is prime or not from 0 to N ; If p is a prime ; Set its multiples as non - prime ; Function to store the count of prime numbers ; Function to count the operations to reduce the array to one element by replacing each pair with its LCM ; Generating Prime Number ; Corner Case ; Stores the prefix sum of the count of prime numbers ; Given array arr ; Function call
const maxm = 10001 ; var prime = Array ( ) ; var prime_number = Array ( ) ; function SieveOfEratosthenes ( ) { for ( i = 0 ; i < maxm ; i ++ ) prime [ i ] = true ; for ( p = 2 ; p * p < maxm ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * p ; i < maxm ; i += p ) prime [ i ] = false ; } } prime [ 0 ] = false ; prime [ 1 ] = false ; } function num_prime ( ) { prime_number [ 0 ] = 0 ; for ( i = 1 ; i <= maxm ; i ++ ) { var tmp ; if ( prime [ i ] == true ) { tmp = 1 ; } else { tmp = 0 ; } prime_number [ i ] = prime_number [ i - 1 ] + tmp ; } } function min_steps ( arr , n ) { SieveOfEratosthenes ( ) ; num_prime ( ) ; if ( n == 1 ) { document . write ( " " ) ; } else if ( n == 2 ) { document . write ( " " ) ; } else { document . write ( prime_number [ n ] - 1 + ( n - 2 ) ) ; } } prime_number . fill ( 0 ) ; var arr = [ 5 , 4 , 3 , 2 , 1 ] ; var N = arr . length ; min_steps ( arr , N ) ;
Find the winner of a game of removing any number of stones from the least indexed non | Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Given piles of stone ; Function call
function findWinner ( a , n ) { let win = 0 ; if ( n % 2 == 0 ) win = 1 ; else win = 0 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( i % 2 == 1 ) { if ( win == 0 && a [ i ] > 1 ) win = 1 ; } else { if ( win == 1 && a [ i ] > 1 ) win = 0 ; } } if ( win == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let arr = [ 1 , 1 , 1 , 2 ] ; let N = arr . length ; findWinner ( arr , N ) ;
Check if a destination is reachable from source with two movements allowed | Set 2 | Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code
function isReachable ( x1 , y1 , x2 , y2 ) { while ( x2 > x1 && y2 > y1 ) { if ( x2 > y2 ) x2 %= y2 ; else y2 %= x2 ; } if ( x2 == x1 ) return ( y2 - y1 ) >= 0 && ( y2 - y1 ) % x1 == 0 ; else if ( y2 == y1 ) return ( x2 - x1 ) >= 0 && ( x2 - x1 ) % y1 == 0 ; else return false ; } let source_x = 2 , source_y = 10 ; let dest_x = 26 , dest_y = 12 ; if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the player who wins the game by removing the last of given N cards | Function to check which player can win the game ; Driver code
function checkWinner ( N , K ) { if ( N % ( K + 1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let N = 50 ; let K = 10 ; checkWinner ( N , K ) ;
Length of longest subarray having only K distinct Prime Numbers | Javascript program for the above approach ; Function to precalculate all the prime up to 10 ^ 6 ; Initialize prime to true ; Iterate [ 2 , sqrt ( N ) ] ; If p is prime ; Mark all multiple of p as true ; Function that finds the length of longest subarray K distinct primes ; Precompute all prime up to 2 * 10 ^ 6 ; Keep track occurrence of prime ; Initialize result to - 1 ; If number is prime then increment its count and decrease k ; Decrement K ; Remove required elements till k become non - negative ; Decrease count so that it may appear in another subarray appearing after this present subarray ; Increment K ; Take the max value as length of subarray ; Return the final length ; Given array arr [ ] ; Function call
let isprime = new Array ( 2000010 ) ; function SieveOfEratosthenes ( n ) { isprime . fill ( true ) ; isprime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( isprime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) isprime [ i ] = false ; } } } function KDistinctPrime ( arr , n , k ) { SieveOfEratosthenes ( 2000000 ) ; let cnt = new Map ( ) ; let result = - 1 ; for ( let i = 0 , j = - 1 ; i < n ; ++ i ) { let x = arr [ i ] ; if ( isprime [ x ] ) { if ( cnt . has ( x ) ) cnt . set ( x , cnt . get ( x ) + 1 ) else cnt . set ( x , 1 ) ; if ( cnt . get ( x ) == 1 ) { -- k ; } } while ( k < 0 ) { x = arr [ ++ j ] ; if ( isprime [ x ] ) { if ( cnt . has ( x ) ) cnt . set ( x , cnt . get ( x ) - 1 ) else cnt . set ( x , - 1 ) ; if ( cnt . get ( x ) == 0 ) { ++ k ; } } } if ( k == 0 ) result = Math . max ( result , i - j ) ; } return result ; } let arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let K = 3 ; let N = arr . length ; document . write ( KDistinctPrime ( arr , N , K ) ) ;
Smallest composite number not divisible by first N prime numbers | Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
var MAX_SIZE = 1000005 ; function SieveOfEratosthenes ( StorePrimes ) { var IsPrime = Array ( MAX_SIZE ) . fill ( true ) ; var p , i ; for ( p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . push ( p ) ; } function Smallest_non_Prime ( StorePrimes , N ) { var x = StorePrimes [ N ] ; return x * x ; } N = 3 ; var StorePrimes = [ ] ; SieveOfEratosthenes ( StorePrimes ) ; document . write ( Smallest_non_Prime ( StorePrimes , N ) ) ;
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver code
function printSubset ( n , k ) { var answer = [ ] ; while ( n > 0 ) { var p = parseInt ( Math . log2 ( n ) ) ; answer . push ( Math . pow ( k , p ) ) ; n %= parseInt ( Math . pow ( 2 , p ) ) ; } answer . sort ( ) ; for ( var i = 0 ; i < answer . length ; i ++ ) { document . write ( answer [ i ] + " " ) ; } } var n = 5 ; var k = 4 ; printSubset ( n , k ) ;
Count total set bits in all numbers from range L to R | Returns position of leftmost set bit The rightmost position is taken as 0 ; Function that gives the position of previous leftmost set bit in n ; Function that returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n ; Use the position ; Function to count the set bits between the two numbers N and M ; Base Case ; Get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 ; Update n for next recursive call ; Function that counts the set bits between L and R ; Driver Code Given L and R ; Function call
function getLeftmostBit ( n ) { var m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } function getNextLeftmostBit ( n , m ) { var temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } function countSetBit ( n ) { var m = getLeftmostBit ( n ) ; return _countSetBit ( n , m ) ; } function _countSetBit ( n , m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( 1 << ( m + 1 ) ) - 1 ) return ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( ( n + 1 ) + countSetBit ( n ) + m * ( 1 << ( m - 1 ) ) ) ; } function countSetBits ( L , R ) { return Math . abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } var L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;
Permutation of Array such that products of all adjacent elements are even | Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; Else prlet the permutation ; Print remaining odds are even . and even elements ; Driver Code
function printPermutation ( arr , n ) { let odd = [ ] ; let even = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . push ( arr [ i ] ) ; else odd . push ( arr [ i ] ) ; } let size_odd = odd . length ; let size_even = even . length ; if ( size_odd > size_even + 1 ) document . write ( " " ) ; else { let i = 0 ; let j = 0 ; while ( i < size_odd && j < size_even ) { document . write ( odd [ i ] + " " ) ; ++ i ; document . write ( even [ j ] + " " ) ; ++ j ; } while ( i < size_odd ) { document . write ( odd [ i ] + " " ) ; ++ i ; } while ( j < size_even ) { document . write ( even [ j ] + " " ) ; } } } let arr = [ 6 , 7 , 9 , 8 , 10 , 11 ] ; let N = arr . length ; printPermutation ( arr , N ) ;
Maximize GCD of all possible pairs from 1 to N | Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver code ; Function call
function find ( n ) { document . write ( parseInt ( n / 2 , 10 ) + " " ) ; } let n = 5 ; find ( n ) ;
Count of pairs of Array elements which are divisible by K when concatenated | JavaScript program to count pairs of array elements which are divisible by K when concatenated ; Function to calculate and return the count of pairs ; Compute power of 10 modulo k ; Calculate length of a [ i ] ; Increase count of remainder ; Calculate ( a [ i ] * 10 ^ lenj ) % k ; Calculate ( k - ( a [ i ] * 10 ^ lenj ) % k ) % k ; Increase answer by count ; If a pair ( a [ i ] , a [ i ] ) is counted ; Return the count of pairs ; Driver Code
let rem = new Array ( 11 ) ; for ( var i = 0 ; i < rem . length ; i ++ ) { rem [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < rem . length ; i ++ ) { for ( var j = 0 ; j < rem . length ; j ++ ) { rem [ i ] [ j ] = 0 ; } } function countPairs ( a , n , k ) { let len = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let p = Array . from ( { length : 11 } , ( _ , i ) => 0 ) ; p [ 0 ] = 1 ; for ( let i = 1 ; i <= 10 ; i ++ ) { p [ i ] = ( p [ i - 1 ] * 10 ) % k ; } for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ; while ( x > 0 ) { len [ i ] ++ ; x = Math . floor ( x / 10 ) ; } rem [ len [ i ] ] [ a [ i ] % k ] ++ ; } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 1 ; j <= 10 ; j ++ ) { let r = ( a [ i ] * p [ j ] ) % k ; let xr = ( k - r ) % k ; ans += rem [ j ] [ xr ] ; if ( len [ i ] == j && ( r + a [ i ] % k ) % k == 0 ) ans -- ; } } return ans ; } let a = [ 4 , 5 , 2 ] ; let n = a . length , k = 2 ; document . write ( countPairs ( a , n , k ) ) ;
Count of N digit Numbers whose sum of every K consecutive digits is equal | Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Given integer N and K
function countDigitSum ( N , K ) { var l = parseInt ( Math . pow ( 10 , N - 1 ) ) , var r = parseInt ( Math . pow ( 10 , N ) - 1 ) ; var count = 0 ; for ( i = l ; i <= r ; i ++ ) { var num = i ; var digits = Array ( N ) . fill ( 0 ) ; for ( j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num = parseInt ( num / 10 ) ; } var sum = 0 , flag = 0 ; for ( j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( j = K ; j < N ; j ++ ) { if ( sum - digits [ j - K ] + digits [ j ] != sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } var N = 2 , K = 1 ; document . write ( countDigitSum ( N , K ) ) ;
Array formed using sum of absolute differences of that element with all other elements | Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize let sum to 0 ; Add the value of sum to ans ; Return the final ans ; Given array arr [ ] ; Function Call
function calculate ( arr ) { let n = arr . length ; let ans = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j < arr . length ; j ++ ) { sum += Math . abs ( arr [ i ] - arr [ j ] ) ; } ans . push ( sum ) ; } return ans ; } let arr = [ 2 , 3 , 5 , 6 ] ; document . write ( calculate ( arr ) ) ;
Minimum number of distinct powers of 2 required to express a given binary number | Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; Driver Code
function findMinimum ( s ) { var n = s . length ; var x = Array ( n + 1 ) . fill ( 0 ) ; s . reverse ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( i && x [ i - 1 ] == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = - 1 ; } else x [ i ] = 1 ; } } var c = 0 ; for ( var i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } document . write ( c ) ; } var str = " " . split ( ' ' ) ; findMinimum ( str ) ;
Split a Numeric String into Fibonacci Sequence | Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Given String ; Function call
function splitIntoFibonacciHelper ( pos , S , seq ) { if ( pos == S . length && ( seq . length >= 3 ) ) { return true ; } let num = 0 ; for ( let i = pos ; i < S . length ; i ++ ) { num = num * 10 + ( S [ i ] - ' ' ) ; if ( num > Number . MAX_VALUE ) break ; if ( S [ pos ] == ' ' && i > pos ) break ; if ( seq . length > 2 && ( num > ( seq [ seq . length - 1 ] + seq [ seq . length - 2 ] ) ) ) break ; if ( seq . length < 2 || ( num == ( seq [ seq . length - 1 ] + seq [ seq . length - 2 ] ) ) ) { seq . push ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . pop ( ) ; } } return false ; } function splitIntoFibonacci ( S ) { let seq = [ ] ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . length >= 3 ) { for ( let i = 0 ; i < seq . length ; i ++ ) document . write ( seq [ i ] + " " ) ; } else { document . write ( " " ) ; } } let S = " " ; splitIntoFibonacci ( S ) ;