codeparrot/xlcost-text-to-code huggingface dataset:
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Javascript program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver Code
function maxLengthOf1s ( arr , n ) { let prefix = new Array ( n ) ; prefix . fill ( 0 ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } let suffix = new Array ( n ) ; suffix . fill ( 0 ) ; for ( let i = n - 3 ; i >= 0 ; i -- ) { if ( arr [ i + 2 ] == 1 ) suffix [ i ] = suffix [ i + 1 ] + 1 ; else suffix [ i ] = 0 ; } let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { ans = Math . max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } document . write ( ans ) ; } let n = 6 ; let arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] ; maxLengthOf1s ( arr , n ) ;
Minimum number of operations required to obtain a given Binary String | Function to find the minimum number of operations required to obtain the string s ; Iterate the string s ; If first occurrence of 1 is found ; Mark the index ; Base case : If no 1 occurred ; No operations required ; Stores the character for which last operation was performed ; Stores minimum number of operations ; Iterate from pos to n ; Check if s [ i ] is 0 ; Check if last operation was performed because of 1 ; Set last to 0 ; Check if last operation was performed because of 0 ; Set last to 1 ; Return the answer ; Driver Code
function minOperations ( s ) { let n = s . length ; let pos = - 1 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { pos = i ; break ; } } if ( pos == - 1 ) { return 0 ; } let last = 1 ; let ans = 1 ; for ( let i = pos + 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( last == 1 ) { ans ++ ; last = 0 ; } } else { if ( last == 0 ) { ans ++ ; last = 1 ; } } } return ans ; } let s = " " ; document . write ( minOperations ( s ) ) ;
Count the number of clumps in the given Array | Function to count the number of clumps in the given array arr [ ] ; Initialise count of clumps as 0 ; Traverse the arr [ ] ; Whenever a sequence of same value is encountered ; Return the count of clumps ; Driver Code ; length of the given array arr [ ] ; Function Call
function countClumps ( arr , N ) { let clumps = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { let flag = 0 ; while ( arr [ i ] == arr [ i + 1 ] ) { flag = 1 ; i ++ ; } if ( flag ) clumps ++ ; } return clumps ; } let arr = [ 13 , 15 , 66 , 66 , 66 , 37 , 37 , 8 , 8 , 11 , 11 ] ; let N = arr . length ; document . write ( countClumps ( arr , N ) ) ;
Check if an array is sorted and rotated using Binary Search | Function to return the index of the pivot ; Base cases ; Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 } ; Decide whether we need to go to the left half or the right half ; Function to check if a given array is sorted rotated or not ; To check if the elements to the left of the pivot are in descending or not ; To check if the elements to the right of the pivot are in ascending or not ; If both of the above if is true Then the array is sorted rotated ; Else the array is not sorted rotated ; Driver code
function findPivot ( arr , low , high ) { if ( high < low ) return - 1 ; if ( high == low ) return low ; var mid = parseInt ( ( low + high ) / 2 ) ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } function isRotated ( arr , n ) { var l = 0 ; var r = n - 1 ; var pivot = - 1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; var temp = pivot ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } else { pivot = temp ; pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } var arr = [ 4 , 5 , 1 , 3 , 2 ] ; if ( isRotated ( arr , 5 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find a distinct pair ( x , y ) in given range such that x divides y | Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code
function findpair ( l , r ) { let ans1 = l ; let ans2 = 2 * l ; document . write ( ans1 + " " + ans2 ) ; } let l = 1 , r = 10 ; findpair ( l , r ) ;
Jump in rank of a student after updating marks | Function to print the name of student who stood first after updation in rank ; Array of students ; Store the name of the student ; Update the marks of the student ; Store the current rank of the student ; Print the name and jump in rank ; Names of the students ; Marks of the students ; Updates that are to be done ; Number of students
function nameRank ( names , marks , updates , n ) { let x = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { x [ i ] = new Array ( 3 ) ; for ( let j = 0 ; j < 3 ; j ++ ) { x [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { x [ i ] [ 0 ] = names [ i ] x [ i ] [ 1 ] = marks [ i ] + updates [ i ] x [ i ] [ 2 ] = i + 1 } let highest = x [ 0 ] ; for ( let j = 1 ; j < n ; j ++ ) { if ( x [ j ] [ 1 ] >= highest [ 1 ] ) highest = x [ j ] } document . write ( " " , highest [ 0 ] , " " , Math . abs ( highest [ 2 ] - 1 ) , sep = " " ) } let names = [ " " , " " , " " ] ; let marks = [ 80 , 79 , 75 ] ; let updates = [ 0 , 5 , - 9 ] ; let n = marks . length ; nameRank ( names , marks , updates , n )
Integers from the range that are composed of a single distinct digit | Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver Code
function countDigits ( n ) { let count = 0 ; while ( n > 0 ) { count += 1 ; n = parseInt ( n / 10 , 10 ) ; } return count ; } function getDistinct ( d , count ) { let num = 0 ; count = parseInt ( Math . pow ( 10 , count - 1 ) , 10 ) ; while ( count > 0 ) { num += ( count * d ) ; count = parseInt ( count / 10 , 10 ) ; } return num ; } function findCount ( L , R ) { let count = 0 ; let countDigitsL = countDigits ( L ) ; let countDigitsR = countDigits ( R ) ; let firstDigitL = parseInt ( L / parseInt ( Math . pow ( 10 , countDigitsL - 1 ) , 10 ) , 10 ) ; let firstDigitR = parseInt ( R / parseInt ( Math . pow ( 10 , countDigitsR - 1 ) , 10 ) , 10 ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } let L = 10 , R = 50 ; document . write ( findCount ( L , R ) ) ;
Check if a pair with given absolute difference exists in a Matrix | Javascript code to check for pair with given difference exists in the matrix or not ; Function to check if a pair with given difference exists in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Input matrix ; Given difference
let N = 4 ; let M = 4 ; function isPairWithDiff ( mat , k ) { let s = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { let m = mat [ i ] [ j ] - k ; if ( s . includes ( m ) ) { return true ; } } else { let m = k - mat [ i ] [ j ] ; if ( s . includes ( m ) ) { return true ; } } s . push ( mat [ i ] [ j ] ) ; } } return false ; } let mat = [ [ 5 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 100 ] ] ; let k = 85 ; document . write ( isPairWithDiff ( mat , k ) == true ? " " : " " ) ;
Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ; Driver Code
function printNSE ( arr , n ) { let s = [ ] ; let mp = new Map ( ) ; s . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { if ( s . length == 0 ) { s . push ( arr [ i ] ) ; continue ; } while ( s . length != 0 && s [ s . length - 1 ] > arr [ i ] ) { mp [ s [ s . length - 1 ] ] = arr [ i ] ; s . pop ( ) ; } s . push ( arr [ i ] ) ; } while ( s . length != 0 ) { mp [ s [ s . length - 1 ] ] = - 1 ; s . pop ( ) ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " + mp [ arr [ i ] ] + " " ) ; } let arr = [ 11 , 13 , 21 , 3 ] ; let n = arr . length ; printNSE ( arr , n ) ;
Check if a string is suffix of another | Javascript program to find if a string is suffix of another ; Test case - sensitive implementation of endsWith function
let s1 = " " , s2 = " " ; let result = s2 . endsWith ( s1 ) ; if ( result ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum elements that can be made equal with k updates | Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code
function ElementsCalculationFunc ( pre , maxx , x , k , n ) { for ( let i = 0 , j = x ; j <= n ; j ++ , i ++ ) { if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) return true ; } return false ; } function MaxNumberOfElements ( a , n , k ) { a . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i <= n ; ++ i ) { pre [ i ] = 0 ; maxx [ i ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] ; maxx [ i ] = Math . max ( maxx [ i - 1 ] , a [ i - 1 ] ) ; } let l = 1 , r = n , ans = 0 ; while ( l < r ) { let mid = Math . floor ( ( l + r ) / 2 ) ; if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } document . write ( ans + " " ) ; } let arr = [ 2 , 4 , 9 ] ; let n = arr . length ; let k = 3 ; MaxNumberOfElements ( arr , n , k ) ;
Find k | Structure to store the start and end point ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals
class Interval { constructor ( a , b ) { this . s = a ; this . e = b ; } } function kthSmallestNum ( merged , k ) { let n = merged . length ; for ( let j = 0 ; j < n ; j ++ ) { if ( k <= Math . abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ) return ( merged [ j ] . s + k - 1 ) ; k = k - Math . abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } function mergeIntervals ( merged , arr , n ) { arr . sort ( function ( a , b ) { return a . s - b . s ; } ) ; merged . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { let prev = merged [ merged . length - 1 ] ; let curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) merged [ merged . length - 1 ] . e = curr . e ; else { merged . push ( curr ) ; } } return merged ; } let arr = [ new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) ] ; let n = arr . length ; let query = [ 5 , 8 ] ; let q = query . length ; let merged = [ ] ; merged = mergeIntervals ( merged , arr , n ) ; for ( let i = 0 ; i < q ; i ++ ) document . write ( kthSmallestNum ( merged , query [ i ] ) + " " ) ;
Grouping Countries | JavaScript program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; Someone gives different answer ; Check next person ; One valid country group has been found ; Driver Code
function countCountries ( ans , N ) { let total_countries = 0 , i = 0 ; let invalid = false ; while ( i < N ) { let curr_size = ans [ i ] ; let num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { document . write ( " " ) ; return ; } else num -- ; i ++ ; } total_countries ++ ; } document . write ( " " + total_countries + " " ) ; } let ans = [ 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ] ; let n = 8 ; countCountries ( ans , n ) ;
Check if an array contains all elements of a given range | Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present
function check_elements ( arr , n , A , B ) { let range = B - A ; for ( let i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] ) >= A && Math . abs ( arr [ i ] ) <= B ) { let z = Math . abs ( arr [ i ] ) - A ; if ( arr [ z ] > 0 ) { arr [ z ] = arr [ z ] * - 1 ; } } } let count = 0 ; for ( let i = 0 ; i <= range && i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; else count ++ ; } if ( count != ( range + 1 ) ) return false ; return true ; } let arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ] ; let n = arr . length ; let A = 2 , B = 5 ; if ( check_elements ( arr , n , A , B ) ) document . write ( " " ) ; else document . write ( " " ) ;
Recursive Programs to find Minimum and Maximum elements of array | Function to print Minimum element using recursion ; If size = 0 means whole array has been traversed ; Driver Code
function findMinRec ( A , n ) { if ( n == 1 ) return A [ 0 ] ; return Math . min ( A [ n - 1 ] , findMinRec ( A , n - 1 ) ) ; } let A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ] ; let n = A . length ; document . write ( findMinRec ( A , n ) ) ;
Allocate minimum number of pages | Utility method to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return false ; else update curr_sum ; method to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; if yes then find the minimum distribution ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Number of pages in books ; let m = 2 ; No . of students
function isPossible ( arr , n , m , curr_min ) { let studentsRequired = 1 ; let curr_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > curr_min ) return false ; if ( curr_sum + arr [ i ] > curr_min ) { studentsRequired ++ ; curr_sum = arr [ i ] ; if ( studentsRequired > m ) return false ; } else curr_sum += arr [ i ] ; } return true ; } function findPages ( arr , n , m ) { let sum = 0 ; if ( n < m ) return - 1 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; let start = 0 , end = sum ; let result = Number . MAX_VALUE ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( isPossible ( arr , n , m , mid ) ) { result = Math . min ( result , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return result ; } let arr = [ 12 , 34 , 67 , 90 ] ; document . write ( " " + findPages ( arr , arr . length , m ) ) ;
Maximum number of plates that can be placed from top to bottom in increasing order of size | Recursive function to count and return the max number of plates that can be placed ; If no plate remains ; If length and width of previous plate exceeds that of the current plate ; Calculate including the plate ; Calculate excluding the plate ; Otherwise ; Calculate only excluding the plate ; Driver code ; Sorting plates in decreasing order of area ; Assuming first plate to be of maximum size
function countPlates ( plates , lastLength , lastWidth , i , n ) { if ( i == n ) return 0 ; var taken = 0 , notTaken = 0 ; if ( lastLength > plates [ i ] [ 0 ] && lastWidth > plates [ i ] [ 1 ] ) { taken = 1 + countPlates ( plates , plates [ i ] [ 0 ] , plates [ i ] [ 1 ] , i + 1 , n ) ; notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; } else notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; return Math . max ( taken , notTaken ) ; } var plates = [ [ 6 , 4 ] , [ 5 , 7 ] , [ 1 , 2 ] , [ 3 , 3 ] , [ 7 , 9 ] ] ; var n = plates . length ; plates . sort ( ( v1 , v2 ) => v2 [ 0 ] * v2 [ 1 ] - v1 [ 0 ] * v1 [ 1 ] ) ; var lastLength = 1000000000 ; var lastWidth = 1000000000 ; document . write ( countPlates ( plates , lastLength , lastWidth , 0 , n ) ) ;
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum | Function to find the value of X such that Bitwise AND of all array elements with X is maximum ; Stores the count of set bit at each position ; Stores the resultant value of X ; Calculate the count of set bits at each position ; If the jth bit is set ; Stores the contribution of each set bit ; Store all bit and amount of contribution ; Find the total contribution ; Sort V [ ] in decreasing order of second parameter ; Choose exactly K set bits ; Print the answer ; Driver Code
function maximizeSum ( arr , n , k ) { let cnt = new Array ( 30 ) . fill ( 0 ) ; let X = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < 30 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) cnt [ j ] ++ ; } } let v = new Array ( ) ; for ( let i = 0 ; i < 30 ; i ++ ) { let gain = cnt [ i ] * ( 1 << i ) ; v . push ( [ i , gain ] ) ; } v . sort ( ( a , b ) => { if ( a [ 1 ] != b [ 1 ] ) return b [ 1 ] - a [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; for ( let i = 0 ; i < k ; i ++ ) { X |= ( 1 << v [ i ] [ 0 ] ) ; } document . write ( X ) ; } let arr = [ 3 , 4 , 5 , 1 ] ; let K = 1 ; let N = arr . length ; maximizeSum ( arr , N , K ) ;
Find Minimum Depth of a Binary Tree | Class containing left and right child of current node and key value ; Root of the Binary Tree ; Function to calculate the minimum depth of the tree ; Corner case . Should never be hit unless the code is called on root = NULL ; Base case : Leaf Node . This accounts for height = 1. ; If left subtree is NULL , recur for right subtree ; If right subtree is NULL , recur for left subtree ; Let us construct the Tree shown in the above figure
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } let root ; function minimumDepth ( ) { return minimumDepth ( root ) ; } function minimumDepth ( root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; if ( root . left == null ) return minimumDepth ( root . right ) + 1 ; if ( root . right == null ) return minimumDepth ( root . left ) + 1 ; return Math . min ( minimumDepth ( root . left ) , minimumDepth ( root . right ) ) + 1 ; } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " + " " + minimumDepth ( root ) ) ;
Remove an occurrence of most frequent array element exactly K times | Function to print the most frequent array element exactly K times ; Stores frequency array element ; Count frequency of array element ; Maximum array element ; Traverse the Map ; Find the element with maximum frequency ; If the frequency is maximum , store that number in element ; Print element as it contains the element having highest frequency ; Decrease the frequency of the maximum array element ; Reduce the number of operations ; Given array ; Size of the array ; Given K
function maxFreqElements ( arr , N , K ) { var mp = { } ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . hasOwnProperty ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } while ( K > 0 ) { var max = 0 ; var element = 0 ; for ( const [ key , value ] of Object . entries ( mp ) ) { if ( value > max ) { max = value ; element = key ; } } document . write ( element + " " ) ; if ( mp . hasOwnProperty ( element ) ) { mp [ element ] -- ; } else { mp [ element ] = - 1 ; } K -- ; } } var arr = [ 1 , 3 , 2 , 1 , 4 , 1 ] ; var N = arr . length ; var K = 2 ; maxFreqElements ( arr , N , K ) ;
Maximize difference between maximum and minimum array elements after K operations | Reverse array ; Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr ; Update maxDiff ; Driver Code
function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function maxDiffLargSmallOper ( arr , N , K ) { var maxDiff = 0 ; arr . sort ( ) ; arr = reverse ( arr ) ; for ( i = 0 ; i <= Math . min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } var arr = [ 7 , 7 , 7 , 7 ] ; var N = arr . length ; var K = 1 ; document . write ( maxDiffLargSmallOper ( arr , N , K ) ) ;