codeparrot/xlcost-text-to-code huggingface dataset:
Minimum characters required to be removed to make frequency of each character unique | Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the String ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
function minCntCharDeletionsfrequency ( str , N ) { let mp = new Map ( ) ; let pq = [ ] ; let cntChar = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( str [ i ] ) ) { mp . set ( str [ i ] , mp . get ( str [ i ] ) + 1 ) ; } else { mp . set ( str [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { pq . push ( value ) ; } pq . sort ( function ( a , b ) { return b - a ; } ) ; while ( pq . length != 0 ) { let frequent = pq [ 0 ] ; pq . shift ( ) ; if ( pq . length == 0 ) { return cntChar ; } if ( frequent == pq [ 0 ] ) { if ( frequent > 1 ) { pq . push ( frequent - 1 ) ; pq . sort ( function ( a , b ) { return b - a ; } ) ; } cntChar ++ ; } } return cntChar ; } let str = " " ; let N = str . length ; document . write ( minCntCharDeletionsfrequency ( str . split ( " " ) , N ) ) ;
Replace specified matrix elements such that no two adjacent elements are equal | Function to check if current position is safe or not ; Directions for adjacent cells ; Check if any adjacent cell is same ; Current index is valid ; Recursive function for backtracking ; Free cell ; All positions covered ; If position is valid for 1 ; If position is valid for 2 ; Recursive call for next unoccupied position ; If above conditions fails ; Function to print valid matrix ; Driver code ; Give dimensions ; Function call
function issafe ( v , i , j , n , m , ch ) { let rowN = [ 1 , - 1 , 0 , 0 ] ; let colN = [ 0 , 0 , 1 , - 1 ] ; for ( let k = 0 ; k < 4 ; k ++ ) { if ( i + rowN [ k ] >= 0 && i + rowN [ k ] < n && j + colN [ k ] >= 0 && j + colN [ k ] < m && v [ i + rowN [ k ] ] [ j + colN [ k ] ] == ch ) { return false ; } } return true ; } function place ( v , n , m ) { let i = 0 , j = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( v [ i ] [ j ] == ' ' ) { break ; } } if ( j != m ) { break ; } } if ( i == n && j == m ) { return true ; } if ( issafe ( v , i , j , n , m , ' ' ) ) { v [ i ] [ j ] = ' ' ; if ( place ( v , n , m ) ) { return true ; } v [ i ] [ j ] = ' ' ; } if ( issafe ( v , i , j , n , m , ' ' ) ) { v [ i ] [ j ] = ' ' ; if ( place ( v , n , m ) ) { return true ; } v [ i ] [ j ] = ' ' ; } return false ; } function printMatrix ( arr , n , m ) { place ( arr , n , m ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , ] ; let n = 4 , m = 4 ; printMatrix ( arr , n , m ) ;
Program for Mean and median of an unsorted array | Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code ; Function call
function findMean ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return sum / n ; } function findMedian ( a , n ) { a . sort ( ) ; if ( n % 2 != 0 ) return a [ n / 2 ] ; return ( a [ Math . floor ( ( n - 1 ) / 2 ) ] + a [ n / 2 ] ) / 2 ; } let a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] let n = a . length ; document . write ( " " + findMean ( a , n ) + " " ) ; document . write ( " " + findMedian ( a , n ) ) ;
Find a pair with sum N having minimum absolute difference | Function to find the value of X and Y having minimum value of abs ( X - Y ) ; If N is an odd number ; If N is an even number ; Driver Code
function findXandYwithminABSX_Y ( N ) { if ( N % 2 == 1 ) { document . write ( ( N / 2 ) + " " + ( N / 2 + 1 ) ) ; } else { document . write ( ( N / 2 - 1 ) + " " + ( N / 2 + 1 ) ) ; } } let N = 12 ; findXandYwithminABSX_Y ( N ) ;
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | Function to find the maximum sum subsequence ; Base Case ; If current state is already calculated then use it ; If current element is positive ; Update ans and recursively call with update value of flag ; Else current element is negative ; Update ans and recursively call with update value of flag ; Return maximum sum subsequence ; Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs ; Create auxiliary array dp [ ] [ ] ; Initialize dp [ ] [ ] ; Function Call ; Given array arr [ ] ; Function call
function findMax ( a , dp , i , flag ) { if ( i == a . length ) { return 0 ; } if ( dp [ i ] [ flag ] != - 1 ) { return dp [ i ] [ flag ] ; } let ans ; if ( flag == 0 ) { ans = Math . max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) ) ; } else { ans = Math . max ( findMax ( a , dp , i + 1 , 1 ) , - 1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) ) ; } return dp [ i ] [ flag ] = ans ; } function findMaxSumUtil ( arr , N ) { let dp = new Array ( N ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( findMax ( arr , dp , 0 , 0 ) ) ; } let arr = [ 1 , 2 , 3 , 4 , 1 , 2 ] ; let N = arr . length ; findMaxSumUtil ( arr , N ) ;
Find the player to last modify a string such that even number of consonants and no vowels are left in the string | Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the string ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Given string s ; Function Call
function findWinner ( s ) { var vowels_count = 0 , consonants_count = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] === " " s [ i ] === " " s [ i ] === " " s [ i ] === " " s [ i ] === " " ) { vowels_count ++ ; } else { consonants_count ++ ; } } if ( vowels_count === 0 ) { if ( consonants_count % 2 === 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } else if ( vowels_count === 1 && consonants_count % 2 !== 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s = " " ; findWinner ( s ) ;
Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | Function to find lexicographically smallest permutation of [ 1 , A ] having B integers greater than all the previous elements ; Initial array ; Resultant array ; Append the first ( B - 1 ) elements to the resultant array ; Append the maximum from the initial array ; Copy the remaining elements ; Return the answer ; Driver Code
function getSmallestArray ( A , B ) { let arr = [ ] ; for ( let i = 0 ; i < A ; i ++ ) arr [ i ] = i + 1 ; let ans = [ ] ; for ( let i = 0 ; i < A ; i ++ ) ans . push ( 0 ) ; for ( let i = 0 ; i < B - 1 ; i ++ ) ans [ i ] = arr [ i ] ; ans [ B - 1 ] = arr [ A - 1 ] ; for ( let i = B ; i < A ; i ++ ) ans [ i ] = arr [ i - 1 ] ; return ans ; } let A = 3 ; let B = 2 ; let ans = getSmallestArray ( A , B ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] , " " ) ;
k smallest elements in same order using O ( 1 ) extra space | Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; Find largest from top n - element ; If largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver code
function printSmall ( arr , n , k ) { for ( let i = k ; i < n ; ++ i ) { let max_var = arr [ k - 1 ] ; let pos = k - 1 ; for ( let j = k - 2 ; j >= 0 ; j -- ) { if ( arr [ j ] > max_var ) { max_var = arr [ j ] ; pos = j ; } } if ( max_var > arr [ i ] ) { let j = pos ; while ( j < k - 1 ) { arr [ j ] = arr [ j + 1 ] ; j ++ ; } arr [ k - 1 ] = arr [ i ] ; } } for ( let i = 0 ; i < k ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] ; let n = 10 ; let k = 5 ; printSmall ( arr , n , k ) ;
Find k pairs with smallest sums in two arrays | Function to find k pairs with least sum such that one elemennt of a pair is from arr1 [ ] and other element is from arr2 [ ] ; Stores current index in arr2 [ ] for every element of arr1 [ ] . Initially all values are considered 0. Here current index is the index before which all elements are considered as part of output . ; Initialize current pair sum as infinite ; To pick next pair , traverse for all elements of arr1 [ ] , for every element , find corresponding current element in arr2 [ ] and pick minimum of all formed pairs . ; Check if current element of arr1 [ ] plus element of array2 to be used gives minimum sum ; Update index that gives minimum ; update minimum sum ; Driver code
function kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) { if ( k > n1 * n2 ) { document . write ( " " ) ; return ; } let index2 = new Array ( n1 ) ; index2 . fill ( 0 ) ; while ( k > 0 ) { let min_sum = Number . MAX_VALUE ; let min_index = 0 ; for ( let i1 = 0 ; i1 < n1 ; i1 ++ ) { if ( index2 [ i1 ] < n2 && arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) { min_index = i1 ; min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] ; } } document . write ( " " + arr1 [ min_index ] + " " + arr2 [ index2 [ min_index ] ] + " " ) ; index2 [ min_index ] ++ ; k -- ; } } let arr1 = [ 1 , 3 , 11 ] ; let n1 = arr1 . length ; let arr2 = [ 2 , 4 , 8 ] ; let n2 = arr2 . length ; let k = 4 ; kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) ;
Maximize count of subsets having product of smallest element and size of the subset at least X | Comparator function to return the greater of two numbers ; Function to return the maximum count of subsets possible which satisfy the above condition ; Sort the array in descending order ; Stores the count of subsets ; Stores the size of the current subset ; Check for the necessary conditions ; Driver Code
function comp ( a , b ) { return a > b ; } function maxSubset ( arr , N , X ) { arr = arr . sort ( ( a , b ) => b - a ) ; let counter = 0 ; let sz = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sz ++ ; if ( arr [ i ] * sz >= X ) { counter ++ ; sz = 0 ; } } return counter ; } let arr = [ 7 , 11 , 2 , 9 , 5 ] ; let N = arr . length ; let X = 10 ; document . write ( maxSubset ( arr , N , X ) ) ;
Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array | Function to find the maximum value possible of the given expression from all permutations of the array ; Stores the minimum value from the array ; Return the answer ; Driver Code
function maximumModuloValue ( A , n ) { var mn = Number . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { mn = Math . min ( A [ i ] , mn ) ; } return mn ; } var A = [ 7 , 10 , 12 ] ; var n = A . length ; document . write ( maximumModuloValue ( A , n ) ) ;
Final Matrix after incrementing submatrices by K in range given by Q queries | Javascript program for the above approach ; Query data type ; Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix mat [ ] [ ] by adding elements of aux [ ] [ ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the final matrix by adding mat and aux matrix at each cell ; Function that prints matrix mat [ ] ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the final answer ; Print the updated matrix ; Given Matrix ; Given Queries ; Function Call
let N = 3 ; let M = 4 ; class query { constructor ( x1 , x2 , y1 , y2 , k ) { this . x1 = x1 ; this . x2 = x2 ; this . y1 = y1 ; this . y2 = y2 ; this . K = k ; } } function updateQuery ( from_x , from_y , to_x , to_y , k , aux ) { aux [ from_x ] [ from_y ] += k ; if ( to_x + 1 < N ) aux [ to_x + 1 ] [ from_y ] -= k ; if ( to_x + 1 < N && to_y + 1 < M ) aux [ to_x + 1 ] [ to_y + 1 ] += k ; if ( to_y + 1 < M ) aux [ from_x ] [ to_y + 1 ] -= k ; } function updateMatrix ( mat , aux ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } } for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 1 ; j < N ; j ++ ) { aux [ j ] [ i ] += aux [ j - 1 ] [ i ] ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { mat [ i ] [ j ] += aux [ i ] [ j ] ; } } } function printMatrix ( mat ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function matrixQuery ( mat , Q , q ) { let aux = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { aux [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { aux [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < Q ; i ++ ) { updateQuery ( q [ i ] . x1 , q [ i ] . x2 , q [ i ] . y1 , q [ i ] . y2 , q [ i ] . K , aux ) ; } updateMatrix ( mat , aux ) ; printMatrix ( mat ) ; } let mat = [ [ 1 , 0 , 1 , 2 ] , [ 0 , 2 , 4 , 1 ] , [ 1 , 2 , 1 , 0 ] ] ; let Q = 1 ; let q = [ new query ( 0 , 0 , 1 , 1 , 2 ) ] ; matrixQuery ( mat , Q , q ) ;
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; sort the array ; start from second last element as the largest element is at last ; if the element is not equal to largest element ; Driver program to test above function
function print2largest ( arr , arr_size ) { let i , first , second ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } arr . sort ( ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { document . write ( " " + arr [ i ] ) ; return ; } } document . write ( " " ) ; } let arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; let n = arr . length ; print2largest ( arr , n ) ;
Count of nested polygons that can be drawn by joining vertices internally | Function that counts the nested polygons inside another polygons ; Stores the count ; Child polygons can only existss if parent polygon has sides > 5 ; Get next nested polygon ; Return the count ; Given side of polygon ; Function Call
function countNestedPolygons ( sides ) { var count = 0 ; while ( sides > 5 ) { sides /= 2 ; count += 1 ; } return count ; } var N = 12 ; document . write ( countNestedPolygons ( N ) ) ;
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; finding the largest element ; Now find the second largest element ; Driver program to test above function
function print2largest ( arr , arr_size ) { let i ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } let largest = second = - 2454635434 ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > largest ) { largest = arr [ i ] ; } } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > second && arr [ i ] < largest ) { second = arr [ i ] ; } } if ( second == - 2454635434 ) { document . write ( " " ) ; } else { document . write ( " " + second ) ; return ; } } let arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; let n = arr . length ; print2largest ( arr , n ) ;
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver program to test above function
function print2largest ( arr , arr_size ) { let i ; let largest = second = - 2454635434 ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > largest ) { second = largest ; largest = arr [ i ] } else if ( arr [ i ] != largest && arr [ i ] > second ) { second = arr [ i ] ; } } if ( second == - 2454635434 ) { document . write ( " " ) ; } else { document . write ( " " + second ) ; return ; } } let arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; let n = arr . length ; print2largest ( arr , n ) ;
Queries to count characters having odd frequency in a range [ L , R ] | Function to print the number of characters having odd frequencies for each query ; A function to construct the [ ] arr and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ;  ; Driver Code
function queryResult ( prefix , Q ) { let l = Q [ 0 ] ; let r = Q [ 1 ] ; if ( l == 0 ) { let xorval = prefix [ r ] ; document . write ( countSetBits ( xorval ) + " " ) ; } else { let xorval = prefix [ r ] ^ prefix [ l - 1 ] ; document . write ( countSetBits ( xorval ) + " " ) ; } } function calculateCount ( S , Q , m ) { let n = S . length ; let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = ( 1 << ( S [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ) ; } let prefix = new Array ( n ) ; let x = 0 ; for ( let i = 0 ; i < n ; i ++ ) { x ^= arr [ i ] ; prefix [ i ] = x ; } for ( let i = 0 ; i < m ; i ++ ) { queryResult ( prefix , Q [ i ] ) ; } } function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let S = " " ; let Q = [ [ 2 , 4 ] , [ 0 , 3 ] , [ 0 , 12 ] ] ; calculateCount ( S , Q , 3 ) ;
Count of Binary Strings possible as per given conditions | Function to generate maximum possible strings that can be generated ; Maximum possible strings ; Driver Code
function countStrings ( A , B , K ) { let X = Math . floor ( ( A + B ) / ( K + 1 ) ) ; return ( Math . min ( A , Math . min ( B , X ) ) * ( K + 1 ) ) ; } let N = 101 , M = 231 , K = 15 ; document . write ( countStrings ( N , M , K ) ) ;
Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Given matrix size ; Function call
function UniqueMatrix ( N ) { let element_value = 1 ; let i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( let f = element_value ; f < element_value + N ; f ++ ) { document . write ( f + " " ) ; } element_value += N ; } else { for ( let k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { document . write ( k + " " ) ; } element_value += N ; } document . write ( " " ) ; i = i + 1 ; } } let N = 4 ; UniqueMatrix ( N ) ;
Find sum of all nodes of the given perfect binary tree | function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; initialize ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code
function sumNodes ( l ) { let leafNodeCount = Math . pow ( 2 , l - 1 ) ; let vec = [ ] ; for ( let i = 1 ; i <= l ; i ++ ) { vec . push ( [ ] ) ; } for ( let i = 1 ; i <= leafNodeCount ; i ++ ) { vec [ l - 1 ] . push ( i ) ; } for ( let i = l - 2 ; i >= 0 ; i -- ) { let k = 0 ; while ( k < vec [ i + 1 ] . length - 1 ) { vec [ i ] . push ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) ; k += 2 ; } } let sum = 0 ; for ( let i = 0 ; i < l ; i ++ ) { for ( let j = 0 ; j < vec [ i ] . length ; j ++ ) { sum += vec [ i ] [ j ] ; } } return sum ; } let l = 3 ; document . write ( sumNodes ( l ) ) ;
Count of unique palindromic strings of length X from given string | Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array ; Count the frequency in the string ; Store frequency of the char ; check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; decrease the value of the char we put on the position i and n - i ; different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
function findways ( s , x ) { if ( x > s . length ) return 0 ; var n = s . length ; var freq = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; ++ i ) freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var se = [ ] ; for ( var i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . push ( freq [ i ] ) ; var ans = 1 ; for ( var i = 0 ; i < x / 2 ; ++ i ) { var count = 0 ; se . forEach ( u => { if ( u >= 2 ) count ++ ; } ) ; if ( count == 0 ) return 0 ; else ans = ans * count ; se . sort ( ( a , b ) => a - b ) var val = se [ se . length - 1 ] se . pop ( ) ; if ( val > 2 ) se . push ( val - 2 ) ; } if ( x % 2 != 0 ) { var count = 0 ; se . forEach ( u => { if ( u > 0 ) count ++ ; } ) ; ans = ans * count ; } return ans ; } var s = " " ; var x = 2 ; document . write ( findways ( s , x ) ) ;
Min operations to reduce N to 1 by multiplying by A or dividing by B | Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable ' c ' ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Given n , a and b ; Function Call
function findIfPossible ( n , a , b ) { if ( b % a != 0 ) { if ( n == 1 ) return 0 ; else if ( n % b != 0 ) return - 1 ; else return n / b ; } let c = b / a ; let x = 0 , y = 0 ; while ( n % b == 0 ) { n = n / b ; x ++ ; } while ( n % c == 0 ) { n = n / c ; y ++ ; } if ( n == 1 ) { let total_steps = x + ( 2 * y ) ; return total_steps ; } else return - 1 ; } let n = 48 ; let a = 3 , b = 12 ; document . write ( findIfPossible ( n , a , b ) ) ;
Find two numbers with sum N such that neither of them contains digit K | JavaScript implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break string D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
function removeLeadingZeros ( str ) { var i = 0 ; var n = str . length ; while ( str [ i ] == ' ' && i < n ) i ++ ; return str ; } function findPairs ( sum , K ) { var A , B ; A = " " ; B = " " ; var N = ( sum . toString ( ) ) ; var n = N . length ; for ( var i = 0 ; i < n ; i ++ ) { var D = N [ i ] - ' ' ; if ( D == K ) { var D1 , D2 ; D1 = parseInt ( D / 2 ) ; D2 = parseInt ( D / 2 ) + D % 2 ; A = A + String . fromCharCode ( D1 + ' ' . charCodeAt ( 0 ) ) ; B = B + String . fromCharCode ( D2 + ' ' . charCodeAt ( 0 ) ) ; } else { A = A + String . fromCharCode ( D + ' ' . charCodeAt ( 0 ) ) ; B = B + ' ' ; } } A = removeLeadingZeros ( A ) ; B = removeLeadingZeros ( B ) ; document . write ( A + " " + B ) ; } var N = 33673 ; var K = 3 ; findPairs ( N , K ) ;