codeparrot/xlcost-text-to-code huggingface dataset:
Find the point where maximum intervals overlap | Javascript implementation of above approach ; Finding maximum starting time ; Finding maximum ending time ; reating an auxiliary array ; Lazy addition ; Lazily Calculating value at index i ; Driver code
function maxOverlap ( start , end , n ) { let maxa = 0 ; for ( let i = 0 ; i < start . length ; i ++ ) { maxa = Math . max ( maxa , start [ i ] ) ; } let maxb = 0 ; for ( let i = 0 ; i < end . length ; i ++ ) { maxb = Math . max ( maxb , end [ i ] ) ; } let maxc = Math . max ( maxa , maxb ) ; let x = new Array ( maxc + 2 ) ; x . fill ( 0 ) ; let cur = 0 , idx = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } let maxy = Number . MIN_VALUE ; for ( let i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } document . write ( " " + maxy + " " + idx + " " ) ; } let start = [ 13 , 28 , 29 , 14 , 40 , 17 , 3 ] ; let end = [ 107 , 95 , 111 , 105 , 70 , 127 , 74 ] ; let n = start . length ; maxOverlap ( start , end , n ) ;
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; Create an auxiliary array of same size ; Store result in temp [ ] ; Copy temp back to arr [ ] ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver code
function rearrangeNaive ( arr , n ) { let temp = new Array ( n ) , i ; for ( i = 0 ; i < n ; i ++ ) temp [ arr [ i ] ] = i ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } function printArray ( arr , n ) { let i ; for ( i = 0 ; i < n ; i ++ ) document . write ( " " + arr [ i ] ) ; document . write ( " " ) ; } let arr = [ 1 , 3 , 0 , 2 ] ; let n = arr . length ; document . write ( " " ) ; printArray ( arr , n ) ; rearrangeNaive ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; retrieving old value and storing with the new one ; retrieving new value ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver code
function rearrange ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] += i * n ; } for ( i = 0 ; i < n ; i ++ ) { arr [ i ] = parseInt ( arr [ i ] / n ) ; } } function printArray ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( ) ; } var arr = [ 2 , 0 , 1 , 4 , 5 , 3 ] ; var n = arr . length ; document . write ( " " + " " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; document . write ( " " ) document . write ( " " + " " ) ; printArray ( arr , n ) ;
Rearrange an array in maximum minimum form | Set 1 | Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; Auxiliary array to hold modified array ; Indexes of smallest and largest elements from remaining array . ; To indicate whether we need to copy rmaining largest or remaining smallest at next position ; Store result in temp [ ] ; Copy temp [ ] to arr [ ] ; Driver code
function rearrange ( arr , n ) { let temp = new Array ( n ) ; let small = 0 , large = n - 1 ; let flag = true ; for ( let i = 0 ; i < n ; i ++ ) { if ( flag ) temp [ i ] = arr [ large -- ] ; else temp [ i ] = arr [ small ++ ] ; flag = ! flag ; } for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; rearrange ( arr , n ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Minimum number of coins needed to remove all the elements of the array based on given rules | Function to calculate minimum number of coins needed ; Consider the first element separately , add 1 to the total if it 's of type 1 ; Iterate from the second element ; If the current element is of type 2 then any Player can remove the element ; Second pointer to reach end of type 1 elements ; Increment j until arr [ j ] is equal to 1 and j is not out of bounds ; Number of type 1 elements in a continious chunk ; From next iteration i pointer will start from index of j ; Return the minimum count of coins ; Driver code
function minimumcoins ( arr , N ) { let coins = 0 ; let j = 0 ; if ( arr [ 0 ] == 1 ) coins ++ ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 2 ) continue ; j = i ; while ( j < N && arr [ j ] == 1 ) { j ++ ; } let x = ( j - i ) ; coins += Math . floor ( x / 3 ) ; i = j - 1 ; } return coins ; } let N = 8 ; let arr = [ 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 ] ; document . write ( minimumcoins ( arr , N ) ) ;
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; store maximum element of array ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; array elements back to it 's original form ; Driver program to test above function
function rearrange ( arr , n ) { let max_idx = n - 1 , min_idx = 0 ; let max_elem = arr [ n - 1 ] + 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_idx ] % max_elem ) * max_elem ; min_idx ++ ; } } for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = Math . floor ( arr [ i ] / max_elem ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; rearrange ( arr , n ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; Driver Code
function rearrange ( arr , n ) { let max_ele = arr [ n - 1 ] ; let min_ele = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] = max_ele ; max_ele -= 1 ; } else { arr [ i ] = min_ele ; min_ele += 1 ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; rearrange ( arr , n ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Find sum of all left leaves in a given Binary Tree | A binary tree Node has key , pointer to left and right children ; A utility function to check if a given node is leaf or not ; This function returns sum of all left leaves in a given binary tree ; Initialize result ; Update result if root is not NULL ; If left of root is NULL , then add key of left child ; Else recur for left child of root ; Recur for right child of root and update res ; return result ; Driver program
class Node { constructor ( k ) { this . data = k ; this . left = null ; this . right = null ; } } function isLeaf ( node ) { if ( node == null ) return false ; if ( node . left == null && node . right == null ) return true ; return false ; } function leftLeavesSum ( node ) { let res = 0 ; if ( node != null ) { if ( isLeaf ( node . left ) ) res += node . left . data ; else res += leftLeavesSum ( node . left ) ; res += leftLeavesSum ( node . right ) ; } return res ; } root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . left . right = new Node ( 12 ) ; root . left . left = new Node ( 5 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . left . right . right = new Node ( 12 ) ; root . right . right . left = new Node ( 50 ) ; document . write ( " " + leftLeavesSum ( root ) ) ;
Move all negative numbers to beginning and positive to end with constant extra space | A JavaScript program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code
function rearrange ( arr , n ) { let j = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( i != j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 ] ; let n = arr . length ; rearrange ( arr , n ) ; printArray ( arr , n ) ;
Maximize the diamonds by choosing different colour diamonds from adjacent boxes | Function to return the maximized value ; Number of rows and columns ; Creating the Dp array ; Populating the first column ; Iterating over all the rows ; Getting the ( i - 1 ) th max value ; Adding it to the current cell ; Getting the max sum from the last column ; Columns are indexed 1 - based
function maxSum ( arr ) { let m = arr . length ; let n = arr [ 0 ] . length - 1 ; let dp = new Array ( m + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( n + 1 ) . fill ( 0 ) ) ; for ( let i = 1 ; i < m ; ++ i ) dp [ i ] [ 1 ] = arr [ i ] [ 1 ] ; for ( let i = 1 ; i < n + 1 ; ++ i ) { for ( let j = 1 ; j < m ; ++ j ) { let mx = 0 ; for ( let k = 1 ; k < m ; ++ k ) { if ( k != j ) { if ( dp [ k ] [ i - 1 ] > mx ) { mx = dp [ k ] [ i - 1 ] ; } } } if ( mx && arr [ j ] [ i ] ) { dp [ j ] [ i ] = arr [ j ] [ i ] + mx ; } } } let ans = - 1 ; for ( let i = 1 ; i <= m ; ++ i ) { if ( dp [ i ] [ n ] ) { ans = Math . max ( ans , dp [ i ] [ n ] ) } ; } return ans ; } let arr = [ [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 10 , 2 , 20 , 0 ] , [ 0 , 0 , 0 , 5 , 0 ] , [ 0 , 0 , 0 , 0 , 6 ] , [ 0 , 4 , 0 , 11 , 5 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] , ] ; document . write ( maxSum ( arr ) ) ;
Move all negative elements to end in order with extra space allowed | Moves all - ve element to end of array in same order . ; Create an empty array to store result ; Traversal array and store + ve element in temp array index of temp ; If array contains all positive or all negative . ; Store - ve element in temp array ; Copy contents of temp [ ] to arr [ ] ; Driver program
function segregateElements ( arr , n ) { let temp = new Array ( n ) ; let j = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ; if ( j == n j == 0 ) return ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } let arr = [ 1 , - 1 , - 3 , - 2 , 7 , 5 , 11 , 6 ] ; let n = arr . length ; segregateElements ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Rearrange array such that even index elements are smaller and odd index elements are greater | Rearrange ; function to print array ; Driver Code
let rearrange = ( arr , n ) => { for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && arr [ i ] > arr [ i + 1 ] ) { let temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } if ( i % 2 != 0 && arr [ i ] < arr [ i + 1 ] ) { let temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } } } let printArray = ( arr , size ) => { ans = ' ' ; for ( let i = 0 ; i < size ; i ++ ) { ans += arr [ i ] + " " ; } document . write ( ans ) ; } let arr = [ 6 , 4 , 2 , 1 , 8 , 3 ] ; let n = arr . length ; document . write ( " " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; document . write ( " " ) ; document . write ( " " ) ; printArray ( arr , n ) ;
Positive elements at even and negative at odd positions ( Relative order not maintained ) | Javascript program to rearrange positive and negative numbers ; Move forward the positive pointer till negative number number not encountered ; Move forward the negative pointer till positive number number not encountered ; Swap array elements to fix their position . ; Break from the while loop when any index exceeds the size of the array ; Driver code
function rearrange ( a , size ) { let positive = 0 ; let negative = 1 ; let temp ; while ( true ) { while ( positive < size && a [ positive ] >= 0 ) positive += 2 ; while ( negative < size && a [ negative ] <= 0 ) negative += 2 ; if ( positive < size && negative < size ) { temp = a [ positive ] ; a [ positive ] = a [ negative ] ; a [ negative ] = temp ; } else break ; } } let arr = [ 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 ] ; let n = arr . length ; rearrange ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Positive elements at even and negative at odd positions ( Relative order not maintained ) | Swap function ; Print array function ; Driver code ; before modification ; out of order positive element ; find out of order negative element in remaining array ; out of order negative element ; find out of order positive element in remaining array ; after modification
function swap ( a , i , j ) { let temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } function printArray ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 ] ; let n = arr . length ; printArray ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && i % 2 == 1 ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < 0 && j % 2 == 0 ) { swap ( arr , i , j ) ; break ; } } } else if ( arr [ i ] < 0 && i % 2 == 0 ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] >= 0 && j % 2 == 1 ) { swap ( arr , i , j ) ; break ; } } } } printArray ( arr , n ) ;
Segregate even and odd numbers | Set 3 | JavaScript Implementation of the above approach ; Driver code ; Function call
function arrayEvenAndOdd ( arr , n ) { let a = [ ] ; let ind = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { a [ ind ] = arr [ i ] ; ind ++ ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 != 0 ) { a [ ind ] = arr [ i ] ; ind ++ ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } document . write ( ' ' ) ; } let arr = [ 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ] ; let n = arr . length ; arrayEvenAndOdd ( arr , n ) ;
Segregate even and odd numbers | Set 3 | Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver code
function arrayEvenAndOdd ( arr , n ) { let i = - 1 , j = 0 ; let t ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ] ; let n = arr . length ; arrayEvenAndOdd ( arr , n ) ;
Find sum of all left leaves in a given Binary Tree | A binary tree Node has key , pointer to left and right children ; Passing sum as accumulator and implementing pass by reference of sum variable ; Pass in a sum variable as an accumulator ; Check whether this node is a leaf node and is left . ; Pass true for left and false for right ; A wrapper over above recursive function ; use the above recursive function to evaluate sum ; Let us construct the Binary Tree shown in the above figure
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } class Sum { constructor ( ) { this . sum = 0 ; } } var root ; function leftLeavesSumRec ( node , isleft , summ ) { if ( node == null ) return ; if ( node . left == null && node . right == null && isleft ) summ . sum = summ . sum + node . data ; leftLeavesSumRec ( node . left , true , summ ) ; leftLeavesSumRec ( node . right , false , summ ) ; } function leftLeavesSum ( node ) { suum = new Sum ( ) ; leftLeavesSumRec ( node , false , suum ) ; return suum . sum ; } root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . left . right = new Node ( 12 ) ; root . left . left = new Node ( 5 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . left . right . right = new Node ( 12 ) ; root . right . right . left = new Node ( 50 ) ; document . write ( " " + leftLeavesSum ( root ) ) ;
Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase | Function to find the minimum cost to convert all distinct characters to either uppercase or lowercase ; Store the size of the string ; Stores the frequency of lowercase & uppercase characters respectively ; Traverse the string S ; Update uppercase frequency of s [ i ] ; Otherwise , update lowercase frequency of s [ i ] ; Stores if the i - th character should be lowercase or not ; Iterate over the range [ 0 , 25 ] ; If the character is present in the string ; Store the cost to convert every occurence of i to uppercase and lowercase ; Update result [ i ] to 1 if lowercase cost is less ; Traverse the string S ; Store the index of the character ; Convert the current character to uppercase or lowercase according to the condition ; Update s [ i ] ; Update s [ i ] ; Print the modified string ; Driver Code
function minimumCost ( str , L , U ) { var N = str . length ; var s = str . split ( " " ) ; string ans = " " ; var lowerFreq = new Array ( 26 ) . fill ( 0 ) ; var upperFreq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( s [ i ] === s [ i ] . toUpperCase ( ) ) upperFreq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; else lowerFreq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var result = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( lowerFreq [ i ] !== 0 upperFreq [ i ] !== 0 ) { var costToUpper = U * lowerFreq [ i ] ; var costToLower = L * upperFreq [ i ] ; if ( costToLower < costToUpper ) { result [ i ] = 1 ; } } } for ( var i = 0 ; i < N ; i ++ ) { var index = 0 ; if ( s [ i ] === s [ i ] . toLowerCase ( ) ) index = s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; else index = s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; if ( result [ index ] === 1 ) { s [ i ] = s [ i ] . toLowerCase ( ) ; } else { s [ i ] = s [ i ] . toUpperCase ( ) ; } } document . write ( s . join ( " " ) ) ; } var S = " " ; var L = 1 , U = 1 ; minimumCost ( S , L , U ) ;
Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | Function to check whether Matrix A [ ] [ ] can be transformed to Matrix B [ ] [ ] or not ; Traverse the matrix to perform horizontal operations ; Calculate difference ; Update next X elements ; Traverse the matrix to perform vertical operations ; Calculate difference ; Update next K elements ; A [ i ] [ j ] is not equal to B [ i ] [ j ] ; Conversion is not possible ; Conversion is possible ; Input
function Check ( A , B , M , N , X ) { for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j <= N - X ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { var diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( var k = 0 ; k < X ; k ++ ) { A [ i ] [ j + k ] = A [ i ] [ j + k ] + diff ; } } } } for ( var i = 0 ; i <= M - X ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { var diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( var k = 0 ; k < X ; k ++ ) { A [ i + k ] [ j ] = A [ i + k ] [ j ] + diff ; } } } } for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { return 0 ; } } } return 1 ; } var M = 2 , N = 2 , X = 2 ; var A = [ [ 0 , 0 ] , [ 0 , 0 ] ] ; var B = [ [ 1 , 2 ] , [ 0 , 1 ] ] ; if ( Check ( A , B , M , N , X ) ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; }
Make all array elements equal by reducing array elements to half minimum number of times | Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Given array ; Size of the array
function minOperations ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { var res = arr [ i ] ; while ( res ) { if ( mp . has ( res ) ) { mp . set ( res , mp . get ( res ) + 1 ) ; } else { mp . set ( res , 1 ) ; } res = parseInt ( res / 2 ) ; } } var mx = 1 ; mp . forEach ( ( value , key ) => { if ( value == N ) { mx = key ; } } ) ; var ans = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var res = arr [ i ] ; while ( res != mx ) { ans ++ ; res = parseInt ( res / 2 ) ; } } document . write ( ans ) ; } var arr = [ 3 , 1 , 1 , 3 ] ; var N = arr . length ; minOperations ( arr , N ) ;
Program to find largest element in an array | Method to find maximum in arr [ ] ; Initialize maximum element ; Traverse array elements from second and compare every element with current max ; Driver code
function largest ( arr ) { let i ; let max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } let arr = [ 10 , 324 , 45 , 90 , 9808 ] ; document . write ( " " + largest ( arr ) ) ;
Minimize steps required to reach the value N | Function to find the minimum steps required to reach N by either moving i steps forward or 1 steps backward ; Stores the required count ; IF total moves required is less than double of N ; Update steps ; Steps required to reach N ; Update steps ; Given value of N
function minimumsteps ( N ) { let steps = 0 ; while ( steps * ( steps + 1 ) < 2 * N ) { steps ++ ; } if ( steps * Math . floor ( ( steps + 1 ) / 2 ) == N + 1 ) { steps ++ ; } document . write ( steps ) ; } let N = 18 ; minimumsteps ( N ) ;
Program to find largest element in an array | returns maximum in arr [ ] of size n ; Driver Code
function largest ( arr , n ) { arr . sort ( ) ; return arr [ n - 1 ] ; } let arr = [ 10 , 324 , 45 , 90 , 9808 ] ; let n = arr . length ; document . write ( largest ( arr , n ) ) ;
Find the largest three distinct elements in an array | JavaScript code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
function find3largest ( arr ) { arr . sort ( ( a , b ) => a - b ) ; let check = 0 , count = 1 ; for ( let i = 1 ; i <= arr . length ; i ++ ) { if ( count < 4 ) { if ( check != arr [ arr . length - i ] ) { document . write ( arr [ arr . length - i ] + " " ) ; check = arr [ arr . length - i ] ; count ++ ; } } else break ; } } let arr = [ 12 , 45 , 1 , - 1 , 45 , 54 , 23 , 5 , 0 , - 10 ] ; find3largest ( arr ) ;