codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of array elements removed by performing the given operations | Javascript program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver Code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
var S = 0 ; function findMaxSum ( arr , n , min , k ) { var A = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { A . push ( [ arr [ i ] , min [ i ] ] ) ; } A . sort ( ( a , b ) => b [ 0 ] - a [ 0 ] ) ; var K = 0 ; for ( var i = 0 ; i < n ; i ++ ) { S += Math . max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) ; K += k ; } } var arr = [ ] , min = [ ] ; arr = [ 3 , 5 , 2 , 1 ] ; min = [ 3 , 2 , 1 , 3 ] ; var N = arr . length ; var K = 3 ; findMaxSum ( arr , N , min , K , S ) ; document . write ( S ) ;
Count smaller primes on the right of each array element | Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
function is_prime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function countSmallerPrimes ( ar , N ) { for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; for ( let j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } document . write ( count + " " ) ; } } let ar = [ 43 , 3 , 5 , 7 , 2 , 41 ] ; let N = ar . length ; countSmallerPrimes ( ar , N ) ;
Check if a string is concatenation of another given string | Function to check if a String is concatenation of another String ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the Strings ; If str1 is not concatenation of str2 ; Driver Code
function checkConcat ( str1 , str2 ) { let N = str1 . length ; let M = str2 . length ; if ( N % M != 0 ) { return false ; } for ( let i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] != str2 [ i % M ] ) { return false ; } } return true ; } let str1 = " " ; let str2 = " " ; if ( checkConcat ( str1 , str2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize length of string by replacing K pairs of distinct adjacent characters | Function to minimize the length of the String by replacing distinct pairs of adjacent characters ; Stores the length of the given String ; Stores the index of the given String ; Traverse the given String ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
function MinLen ( str , K ) { var N = str . length ; var i = 0 ; while ( i < N - 1 ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . max ( 1 , N - K ) ; } var str = " " ; var K = 1 ; document . write ( MinLen ( str , K ) ) ;
Length of smallest sequence having sum X and product Y | Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
function temp ( n , x ) { return Math . pow ( ( x * 1.0 ) / n , n ) ; } function check ( n , y , x ) { var v = temp ( n , x ) ; return v >= y ; } function find ( x , y ) { var high = parseInt ( Math . floor ( x / Math . exp ( 1.0 ) ) ) ; var low = 1 ; if ( x == y ) document . write ( 1 + " " ) ; else if ( ! check ( high , y , x ) ) document . write ( - 1 + " " ) ; else { while ( high - low > 1 ) { var mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } document . write ( high + " " ) ; } } var x = 9 , y = 8 ; find ( x , y ) ;
Print the middle nodes of each level of a Binary Tree | Javascript program for the above approach ; Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Given Tree ; Function Call
var M = new Map ( ) ; class node { constructor ( data , left , right ) { this . data = data ; this . left = left ; this . right = right ; } } ; function newnode ( d ) { var temp = new node ( d , null , null ) ; return temp ; } function dfs ( root , l ) { if ( root == null ) return ; if ( ! M . has ( l ) ) { var temp = [ ] ; temp . push ( root . data ) ; M . set ( l , temp ) ; } else { var temp = M . get ( l ) ; temp . push ( root . data ) ; M . set ( l , temp ) ; } dfs ( root . left , l + 1 ) ; dfs ( root . right , l + 1 ) ; } function printMidNodes ( root ) { M = new Map ( ) ; dfs ( root , 0 ) ; M . forEach ( ( value , key ) => { var size = value . length ; if ( size % 2 == 1 ) { document . write ( value [ parseInt ( ( size - 1 ) / 2 ) ] + " " ) ; } else { document . write ( value [ parseInt ( ( size - 1 ) / 2 ) ] + " " + value [ parseInt ( ( ( size - 1 ) / 2 ) + 1 ) ] + " " ) ; } } ) ; } var root = newnode ( 1 ) ; root . left = newnode ( 2 ) ; root . right = newnode ( 3 ) ; root . left . left = newnode ( 4 ) ; root . left . right = newnode ( 5 ) ; root . left . right . left = newnode ( 11 ) ; root . left . right . right = newnode ( 6 ) ; root . left . right . right . left = newnode ( 7 ) ; root . left . right . right . right = newnode ( 9 ) ; root . right . left = newnode ( 10 ) ; root . right . right = newnode ( 8 ) ; printMidNodes ( root ) ;
Smallest array that can be obtained by replacing adjacent pairs with their products | Function to minimize the size of array by performing given operations ; If all array elements are not same ; If all array elements are same ; Driver Code ; Function call
function minLength ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return 1 ; } } return N ; } let arr = [ 2 , 1 , 3 , 1 ] ; let N = arr . length ; document . write ( minLength ( arr , N ) ) ;
Subarrays whose sum is a perfect square | Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver Code
function PrintIndexes ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let currSubSum = 0 ; for ( let j = i ; j < N ; j ++ ) { currSubSum += arr [ j ] ; let sq = Math . floor ( Math . sqrt ( currSubSum ) ) ; if ( sq * sq == currSubSum ) { document . write ( " " + i + " " + j + " " + " " ) ; } } } } let arr = [ 65 , 79 , 81 ] ; PrintIndexes ( arr , arr . length ) ;
Count positions in Binary Matrix having equal count of set bits in corresponding row and column | Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
function countPosition ( mat ) { var n = mat . length ; var m = mat [ 0 ] . length ; var row = Array . from ( { length : n } , ( _ , i ) => 0 ) ; var col = Array . from ( { length : m } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } var count = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } var mat = [ [ 0 , 1 ] , [ 1 , 1 ] ] ; document . write ( countPosition ( mat ) ) ;
Maximum value at each level in an N | Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call
function maxAtLevel ( N , M , Value , Edges ) { let adj = new Array ( N ) ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < M ; i ++ ) { let u = Edges [ i ] [ 0 ] ; let v = Edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; } let q = [ ] ; q . push ( 0 ) ; while ( q . length > 0 ) { let count = q . length ; let maxVal = 0 ; while ( count -- > 0 ) { let temp = q [ 0 ] ; q . shift ( ) ; maxVal = Math . max ( maxVal , Value [ temp ] ) ; for ( let i = 0 ; i < adj [ temp ] . length ; i ++ ) { q . push ( adj [ temp ] [ i ] ) ; } } document . write ( maxVal + " " ) ; } } let N = 10 ; let Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 3 , 6 ] , [ 6 , 7 ] , [ 6 , 8 ] , [ 6 , 9 ] ] ; let Value = [ 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 ] ; maxAtLevel ( N , N - 1 , Value , Edges ) ;
Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
function ctTriplets ( arr , N ) { arr . sort ( ) ; let temp = [ ] ; let index = 0 ; for ( let i = 1 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } for ( let i = 0 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } let ct = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( i > 0 && i + 1 < N ) { if ( temp [ i ] < temp [ i + 1 ] && temp [ i ] < temp [ i - 1 ] ) { ct ++ ; } } } document . write ( " " + ct + " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( temp [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let N = arr . length ; ctTriplets ( arr , N ) ;
Rearrange array to make it non | Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Prlet the answer
function check ( a , n ) { let b = new Array ( n ) . fill ( 0 ) ; let minElement = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = Math . min ( minElement , a [ i ] ) ; } b . sort ( ) ; let k = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } let a = [ 4 , 3 , 6 , 6 , 2 , 9 ] ; let n = a . length ; if ( check ( a , n ) == true ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | Function to count required 1 s from the given matrix ; Stores the dimensions of the mat ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the final count ; Given matrix ; Function call
function numSpecial ( mat ) { var m = mat . length ; var n = mat [ 0 ] . length ; var rows = Array . from ( { length : m } , ( _ , i ) => 0 ) ; var cols = Array . from ( { length : n } , ( _ , i ) => 0 ) ; var i , j ; for ( i = 0 ; i < m ; i ++ ) { rows [ i ] = 0 ; for ( j = 0 ; j < n ; j ++ ) rows [ i ] += mat [ i ] [ j ] ; } for ( i = 0 ; i < n ; i ++ ) { cols [ i ] = 0 ; for ( j = 0 ; j < m ; j ++ ) cols [ i ] += mat [ j ] [ i ] ; } var cnt = 0 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 1 && rows [ i ] == 1 && cols [ j ] == 1 ) cnt ++ ; } } return cnt ; } var mat = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] ] ; document . write ( numSpecial ( mat ) + " " ) ;
Find all matrix elements which are minimum in their row and maximum in their column | Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver Code ; Function call ; If no such matrix element is found
function minmaxNumbers ( matrix , res ) { var set = new Set ( ) ; for ( var i = 0 ; i < matrix . length ; i ++ ) { var minr = 1000000000 ; for ( var j = 0 ; j < matrix [ i ] . length ; j ++ ) { minr = Math . min ( minr , matrix [ i ] [ j ] ) ; } set . add ( minr ) ; } for ( var j = 0 ; j < matrix [ 0 ] . length ; j ++ ) { var maxc = - 1000000000 ; for ( var i = 0 ; i < matrix . length ; i ++ ) { maxc = Math . max ( maxc , matrix [ i ] [ j ] ) ; } if ( set . has ( maxc ) ) { res . push ( maxc ) ; } } return res ; } var mat = [ [ 1 , 10 , 4 ] , [ 9 , 3 , 8 ] , [ 15 , 16 , 17 ] ] ; var ans = [ ] ; minmaxNumbers ( mat , ans ) ; if ( ans . length == 0 ) document . write ( " " ) ; for ( var i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ;
Node whose removal minimizes the maximum size forest from an N | JavaScript program to implement the above approach ; Function to create the graph ; Function to traverse the graph and find the minimum of Math . maximum size forest after removing a node ; Traversing every child subtree except the parent node ; Traverse all subtrees ; Update the Math . maximum size of forests ; Update the minimum of Math . maximum size of forests obtained ; Condition to find the minimum of Math . maximum size forest ; Update and store the corresponding node ; Driver Code
var mini = 105 , ans , n ; var g = Array . from ( Array ( 100 ) , ( ) => new Array ( ) ) ; var size = Array ( 100 ) . fill ( 0 ) ; function create_graph ( ) { g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; g [ 1 ] . push ( 3 ) ; g [ 3 ] . push ( 1 ) ; g [ 1 ] . push ( 4 ) ; g [ 4 ] . push ( 1 ) ; g [ 2 ] . push ( 5 ) ; g [ 5 ] . push ( 2 ) ; g [ 2 ] . push ( 6 ) ; g [ 6 ] . push ( 2 ) ; } function dfs ( node , parent ) { size [ node ] = 1 ; var mx = 0 ; g [ node ] . forEach ( y => { if ( y != parent ) { dfs ( y , node ) ; size [ node ] += size [ y ] ; mx = Math . max ( mx , size [ y ] ) ; } } ) ; mx = Math . max ( mx , n - size [ node ] ) ; if ( mx < mini ) { mini = mx ; ans = node ; } } n = 6 ; create_graph ( ) ; dfs ( 1 , - 1 ) ; document . write ( ans ) ;
Minimum non | Function to find minimum flips required to remove all 0 s from a given binary string ; Length of given string ; Stores the indices of previous 0 s ; Stores the minimum operations ; Traverse string to find minimum operations to obtain required string ; Current character ; If current character is '0' ; Update temp1 with current index , if both temp variables are empty ; Update temp2 with current index , if temp1 contains prev index but temp2 is empty ; If temp1 is not empty ; Reset temp1 to - 1 ; Increase ans ; If temp2 is not empty but temp1 is empty ; Reset temp2 to - 1 ; Increase ans ; If both temp variables are not empty ; Otherwise ; Return the answer ; Driver Code
function minOperation ( s ) { var n = s . length ; var temp1 = - 1 , temp2 = - 1 ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var curr = s [ i ] ; if ( curr == ' ' ) { if ( temp1 == - 1 && temp2 == - 1 ) { temp1 = i ; } else if ( temp1 != - 1 && temp2 == - 1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != - 1 ) { temp1 = - 1 ; ans ++ ; } else if ( temp1 == - 1 && temp2 != - 1 && i - temp2 != 1 ) { temp2 = - 1 ; ans ++ ; } } } if ( temp1 != - 1 && temp2 != - 1 ) { ans += 2 ; } else if ( temp1 != - 1 temp2 != - 1 ) { ans += 1 ; } return ans ; } var s = " " ; document . write ( minOperation ( s ) ) ;
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Given arr [ ] ; Function call
function findMinLength ( arr ) { let index = parseInt ( arr . length ) - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } let arr = [ 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( findMinLength ( arr ) ) ;
Count array elements exceeding sum of preceding K elements | Function to count array elements exceeding sum of preceding K elements ; Iterate over the array ; Update prefix sum ; Check if arr [ K ] > arr [ 0 ] + . . + arr [ K - 1 ] ; Increment count ; Check if arr [ i ] > arr [ i - K - 1 ] + . . + arr [ i - 1 ] ; Increment count ; Given array
function countPrecedingK ( a , n , K ) { let prefix = new Array ( n ) . fill ( 0 ) ; prefix [ 0 ] = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + a [ i ] ; } let ctr = 0 ; if ( prefix [ K - 1 ] < a [ K ] ) ctr ++ ; for ( let i = K + 1 ; i < n ; i ++ ) { if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) ctr ++ ; } return ctr ; } let arr = [ 2 , 3 , 8 , 10 , - 2 , 7 , 5 , 5 , 9 , 15 ] ; let N = arr . length ; let K = 2 ; document . write ( countPrecedingK ( arr , N , K ) ) ;
Count minimum moves required to convert A to B | Function to find minimum number of moves to obtained B from A ; Stores the minimum number of moves ; Absolute difference ; K is in range [ 0 , 10 ] ; Print the required moves ; Driver Code
function convertBfromA ( a , b ) { let moves = 0 ; let x = Math . abs ( a - b ) ; for ( let i = 10 ; i > 0 ; i -- ) { moves += Math . floor ( x / i ) ; x = x % i ; } document . write ( moves + " " ) ; } let A = 188 , B = 4 ; convertBfromA ( A , B ) ;
Count N digits numbers with sum divisible by K | JavaScript Program to implement the above approach ; Function to count the N digit numbers whose sum is divisible by K ; Base case ; If already computed subproblem occurred ; Store the count of N digit numbers whose sum is divisible by K ; Check if the number does not contain any leading 0. ; Recurrence relation ; Driver Code ; Stores the values of overlapping subproblems
var M = 1000 ; function countNum ( N , sum , K , st , dp ) { if ( N == 0 && sum == 0 ) { return 1 ; } if ( N < 0 ) { return 0 ; } if ( dp [ N ] [ sum ] [ st ] != - 1 ) { return dp [ N ] [ sum ] [ st ] ; } var res = 0 ; var start = st == 1 ? 0 : 1 ; for ( var i = start ; i <= 9 ; i ++ ) { res += countNum ( N - 1 , ( sum + i ) % K , K , ( st i > 0 ) , dp ) ; } return dp [ N ] [ sum ] [ st ] = res ; } var N = 2 , K = 7 ; var dp = Array . from ( Array ( M ) , ( ) => Array ( M ) ) ; for ( var i = 0 ; i < M ; i ++ ) for ( var j = 0 ; j < M ; j ++ ) dp [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; document . write ( countNum ( N , 0 , K , 0 , dp ) ) ;
Minimize Nth term of an Arithmetic progression ( AP ) | Function to find the smallest Nth term of an AP possible ; Stores the smallest Nth term ; Check if common difference of AP is an integer ; Store the common difference ; Store the First Term of that AP ; Store the Nth term of that AP ; Check if all elements of an AP are positive ; Return the least Nth term obtained ; Driver code
function smallestNth ( A , B , N ) { let res = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { for ( let j = N ; j > i ; j -- ) { if ( ( B - A ) % ( j - i ) == 0 ) { let D = ( B - A ) / ( j - i ) ; let FirstTerm = A - ( i - 1 ) * D ; let NthTerm = FirstTerm + ( N - 1 ) * D ; if ( FirstTerm > 0 ) res = Math . min ( res , NthTerm ) ; } } } return res ; } let N = 3 ; let A = 1 ; let B = 6 ; document . write ( smallestNth ( A , B , N ) ) ;
Subarray of size K with prime sum | Generate all prime numbers in the range [ 1 , 1000000 ] ; Set all numbers as prime initially ; Mark 0 and 1 as non - prime ; If current element is prime ; Mark all its multiples as non - prime ; Function to print the subarray whose sum of elements is prime ; Store the current subarray of size K ; Calculate the sum of first K elements ; Check if currSum is prime ; Store the start and last index of subarray of size K ; Iterate over remaining array ; Check if currSum ; Driver Code
function sieve ( prime ) { for ( var i = 0 ; i < 1000000 ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( var i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] ) { for ( var j = i * i ; j <= 1000000 ; j += i ) { prime [ j ] = false ; } } } } function subPrimeSum ( N , K , arr , prime ) { var currSum = 0 ; for ( var i = 0 ; i < K ; i ++ ) { currSum += arr [ i ] ; } if ( prime [ currSum ] ) { for ( var i = 0 ; i < K ; i ++ ) { document . write ( arr [ i ] + " " ) ; } return ; } var st = 1 , en = K ; while ( en < N ) { currSum += arr [ en ] - arr [ st - 1 ] ; if ( prime [ currSum ] ) { for ( var i = st ; i <= en ; i ++ ) { document . write ( arr [ i ] + " " ) ; } return ; } en ++ ; st ++ ; } } var arr = [ 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 ] var K = 4 ; var N = arr . length ; prime = Array ( 1000000 ) . fill ( 0 ) ; sieve ( prime ) ; subPrimeSum ( N , K , arr , prime ) ;
Minimum characters required to be removed to sort binary string in ascending order | Function to find the minimum count of characters to be removed to make the string sorted in ascending order ; Length of given string ; Stores the first occurrence of '1' ; Stores the last occurrence of '0' ; Traverse the string to find the first occurrence of '1' ; Traverse the string to find the last occurrence of '0' ; Return 0 if the str have only one type of character ; Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1 ; Traverse the string to count0 ; Traverse the string to count1 ; Return the minimum of count0 and count1 ; Given string str ; Function call
function minDeletion ( str ) { let n = str . length ; let firstIdx1 = - 1 ; let lastIdx0 = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { firstIdx1 = i ; break ; } } for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) { lastIdx0 = i ; break ; } } if ( firstIdx1 == - 1 lastIdx0 == - 1 ) return 0 ; let count1 = 0 , count0 = 0 ; for ( let i = 0 ; i < lastIdx0 ; i ++ ) { if ( str [ i ] == ' ' ) { count1 ++ ; } } for ( let i = firstIdx1 + 1 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { count0 ++ ; } } return Math . min ( count0 , count1 ) ; } let str = " " ; document . write ( minDeletion ( str ) ) ;
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | Function to find the maximum score with unique element in the subset ; Base Case ; Check if the previously picked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum of the two possibilities ; Given arrays ; Function Call
function maximumSum ( a , b , n , index , lastpicked ) { if ( index == n ) return 0 ; let option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) ; option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) ; return Math . max ( option1 , option2 ) ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 1 ] ; let brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ] ; let N = arr . length ; document . write ( maximumSum ( arr , brr , N , 0 , - 1 ) ) ;