codeparrot/xlcost-text-to-code huggingface dataset:
Find the largest after deleting the given elements | Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Driver Code
function findlargestAfterDel ( arr , m , del , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let largestElement = Number . MIN_VALUE ; for ( let i = 0 ; i < m ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . delete ( arr [ i ] ) ; } else largestElement = Math . max ( largestElement , arr [ i ] ) ; } return largestElement ; } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 33 , 56 , 5 ] ; let n = del . length ; document . write ( findlargestAfterDel ( array , m , del , n ) ) ;
Number of anomalies in an array | A simple javascript solution to count anomalies in an array . ; Driver code
function countAnomalies ( arr , n , k ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && Math . abs ( arr [ i ] - arr [ j ] ) <= k ) break ; if ( j == n ) res ++ ; } return res ; } let arr = [ 7 , 1 , 8 ] ; let k = 5 ; let n = arr . length ; document . write ( countAnomalies ( arr , n , k ) ) ;
Count majority element in a matrix | Function to find count of all majority elements in a Matrix ; Store frequency of elements in matrix ; loop to iteratre through map ; check if frequency is greater than or equal to ( N * M ) / 2 ; Driver Code
function majorityInMatrix ( arr ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( mp . has ( arr [ i ] [ j ] ) ) { mp . set ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) } else mp . set ( arr [ i ] [ j ] , 1 ) ; } } var countMajority = 0 ; mp . forEach ( ( value , key ) => { if ( value >= ( parseInt ( ( N * M ) / 2 ) ) ) { countMajority ++ ; } } ) ; return countMajority ; } var mat = [ [ 1 , 2 , 2 ] , [ 1 , 3 , 2 ] , [ 1 , 2 , 6 ] ] ; document . write ( majorityInMatrix ( mat ) ) ;
Find pair with maximum difference in any column of a Matrix | Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; Driver Code
function colMaxDiff ( mat ) { let max_diff = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let max_val = mat [ 0 ] [ i ] , min_val = mat [ 0 ] [ i ] ; for ( let j = 1 ; j < N ; j ++ ) { max_val = Math . max ( max_val , mat [ j ] [ i ] ) ; min_val = Math . min ( min_val , mat [ j ] [ i ] ) ; } max_diff = Math . max ( max_diff , max_val - min_val ) ; } return max_diff ; } let mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 5 , 3 , 5 , 4 , 0 ] , [ 5 , 6 , 7 , 8 , 9 ] , [ 0 , 6 , 3 , 4 , 12 ] , [ 9 , 7 , 12 , 4 , 3 ] ] ; document . write ( " " + colMaxDiff ( mat ) )
Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted array of distinct elements within limited range . ; Driver Code
function search ( ar , size ) { let a = 0 , b = size - 1 ; let mid = 0 ; while ( ( b - a ) > 1 ) { mid = ( a + b ) / 2 ; if ( ( ar [ a ] - a ) != ( ar [ mid ] - mid ) ) b = mid ; else if ( ( ar [ b ] - b ) != ( ar [ mid ] - mid ) ) a = mid ; } return ( ar [ a ] + 3 ) ; } let ar = [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] ; let size = ar . length ; document . write ( " " + search ( ar , size ) ) ;
Delete array element in given index range [ L | Delete L to R elements ; Return size of Array after delete element ; Driver Code
function deleteElement ( A , L , R , N ) { let i , j = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( i <= L i >= R ) { A [ j ] = A [ i ] ; j ++ ; } } return j ; } let A = [ 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 ] ; let L = 2 , R = 7 ; let n = A . length ; let res_size = deleteElement ( A , L , R , n ) ; for ( let i = 0 ; i < res_size ; i ++ ) document . write ( A [ i ] + " " ) ;
Find the only missing number in a sorted array | JavaScript program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver code
function findmissing ( ar , N ) { var l = 0 , r = N - 1 ; while ( l <= r ) { var mid = parseInt ( ( l + r ) / 2 ) ; if ( ar [ mid ] != mid + 1 && ar [ mid - 1 ] == mid ) return mid + 1 ; if ( ar [ mid ] != mid + 1 ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ] ; var N = arr . length ; document . write ( findmissing ( arr , N ) ) ;
Find index of first occurrence when an unsorted array is sorted | Javascript program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ;  ; Driver code
function findFirst ( arr , n , x ) { arr . sort ( ) ; let ptr = lowerBound ( arr , 0 , n , x ) ; return ( arr [ ptr ] != x ) ? - 1 : ( ptr ) ; } function lowerBound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } let x = 20 ; let arr = [ 10 , 30 , 20 , 50 , 20 ] ; let n = arr . length ; document . write ( findFirst ( arr , n , x ) ) ;
Find index of first occurrence when an unsorted array is sorted | JavaScript program to find index of first occurrence of x when array is sorted . ; Driver Code
function findFirst ( arr , n , x ) { var count = 0 ; var isX = false ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { isX = true ; } else if ( arr [ i ] < x ) { count ++ ; } } return ( isX == false ) ? - 1 : count ; } var x = 20 , arr = [ 10 , 30 , 20 , 50 , 20 ] ; var n = arr . length ; document . write ( findFirst ( arr , n , x ) ) ;
Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver code
function findDuplicate ( arr ) { let slow = arr [ 0 ] ; let fast = arr [ 0 ] ; do { slow = arr [ slow ] ; fast = arr [ arr [ fast ] ] ; } while ( slow != fast ) ; let ptr1 = arr [ 0 ] ; let ptr2 = slow ; while ( ptr1 != ptr2 ) { ptr1 = arr [ ptr1 ] ; ptr2 = arr [ ptr2 ] ; } return ptr1 ; } let arr = [ 1 , 3 , 2 , 1 ] ; document . write ( findDuplicate ( arr ) + " " ) ;
Number of Larger Elements on right side in a string | Javascript program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code
let MAX_CHAR = 26 ; function printGreaterCount ( str ) { let len = str . length ; let ans = new Array ( len ) ; ans . fill ( 0 ) ; let count = new Array ( MAX_CHAR ) ; count . fill ( 0 ) ; for ( let i = len - 1 ; i >= 0 ; i -- ) { count [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( let j = str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( let i = 0 ; i < len ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let str = " " ; printGreaterCount ( str ) ;
Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map m ; Traverse through all elements ; Search if a pair can be formed with arr [ i ] . ; Driver code
function printPairs ( arr , n , sum ) { var m = { } ; for ( var i = 0 ; i < n ; i ++ ) { var rem = sum - arr [ i ] ; if ( m . hasOwnProperty ( rem ) ) { var count = m [ rem ] ; for ( var j = 0 ; j < count ; j ++ ) { document . write ( " " + rem + " " + arr [ i ] + " " + " " ) ; } } if ( m . hasOwnProperty ( arr [ i ] ) ) { m [ arr [ i ] ] ++ ; } else { m [ arr [ i ] ] = 1 ; } } } var arr = [ 1 , 5 , 7 , - 1 , 5 ] ; var n = arr . length ; var sum = 6 ; printPairs ( arr , n , sum ) ;
Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver Code
function maxProduct ( arr , n ) { if ( n < 4 ) { return - 1 ; } let maxA = Number . MIN_VALUE , maxB = Number . MIN_VALUE , maxC = Number . MIN_VALUE , maxD = Number . MIN_VALUE ; let minA = Number . MAX_VALUE , minB = Number . MAX_VALUE , minC = Number . MAX_VALUE , minD = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxD = maxC ; maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxD = maxC ; maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) { maxD = maxC ; maxC = arr [ i ] ; } else if ( arr [ i ] > maxD ) { maxD = arr [ i ] ; } if ( arr [ i ] < minA ) { minD = minC ; minC = minB ; minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) { minD = minC ; minC = minB ; minB = arr [ i ] ; } else if ( arr [ i ] < minC ) { minD = minC ; minC = arr [ i ] ; } else if ( arr [ i ] < minD ) { minD = arr [ i ] ; } } let x = maxA * maxB * maxC * maxD ; let y = minA * minB * minC * minD ; let z = minA * minB * maxA * maxB ; return Math . max ( x , Math . max ( y , z ) ) ; } let arr = [ 1 , - 4 , 3 , - 6 , 7 , 0 ] ; let n = arr . length ; let max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
Ways to choose three points with distance between the most distant points <= L | Returns the number of triplets with distance between farthest points <= L ; sort to get ordered triplets so that we can find the distance between farthest points belonging to a triplet ; generate and check for all possible triplets : { arr [ i ] , arr [ j ] , arr [ k ] } ; Since the array is sorted the farthest points will be a [ i ] and a [ k ] ; ; set of n points on the X axis
function countTripletsLessThanL ( n , L , arr ) { arr . sort ( ) ; var ways = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { for ( k = j + 1 ; k < n ; k ++ ) { var mostDistantDistance = arr [ k ] - arr [ i ] ; if ( mostDistantDistance <= L ) { ways ++ ; } } } } return ways ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var L = 3 ; var ans = countTripletsLessThanL ( n , L , arr ) ; document . write ( " " + ans ) ;
Triplets in array with absolute difference less than k | Return the lower bound i . e smallest index of element having value greater or equal to value ; Return the number of triplet indices satisfies the three constraints ; sort the array ; for each element from index 2 to n - 1. ; finding the lower bound of arr [ i ] - k . ; If there are at least two elements between lower bound and current element . ; increment the count by lb - i C 2. ; Driver code
function binary_lower ( value , arr , n ) { var start = 0 ; var end = n - 1 ; var ans = - 1 ; var mid ; while ( start <= end ) { mid = parseInt ( ( start + end ) / 2 ) ; if ( arr [ mid ] >= value ) { end = mid - 1 ; ans = mid ; } else { start = mid + 1 ; } } return ans ; } function countTriplet ( arr , n , k ) { var count = 0 ; arr . sort ( ( a , b ) => a - b ) for ( var i = 2 ; i < n ; i ++ ) { var cur = binary_lower ( arr [ i ] - k , arr , n ) ; if ( cur <= i - 2 ) { count += parseInt ( ( ( i - cur ) * ( i - cur - 1 ) ) / 2 ) ; } } return count ; } var arr = [ 1 , 1 , 2 , 2 , 3 ] ; var k = 1 ; var n = arr . length ; document . write ( countTriplet ( arr , n , k ) ) ;
Maximum occurring character in a linked list | Link list node ; counting the frequency of current element p . data ; if current counting is greater than max ; Push a node to linked list . Note that this function changes the head ; Start with the empty list ; this will create a linked list of character " geeksforgeeks "
class Node { constructor ( ) { this . data = ' ' ; this . next = null ; } } ; var head_ref = null ; function maxChar ( head ) { var p = head ; var max = - 1 ; var res = ' ' ; while ( p != null ) { var q = p . next ; var count = 1 ; while ( q != null ) { if ( p . data == q . data ) count ++ ; q = q . next ; } if ( max < count ) { res = p . data ; max = count ; } p = p . next ; } return res ; } function push ( new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; } head_ref = null ; var str = " " ; var st = str . split ( ' ' ) ; var i ; for ( i = 0 ; i < st . length ; i ++ ) push ( st [ i ] ) ; document . write ( maxChar ( head_ref ) ) ;
Find the one missing number in range | Find the missing number in a range ; here we xor of all the number ; xor last number ; Driver code
function missingNum ( arr , n ) { let minvalue = Math . min ( ... arr ) ; let xornum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { xornum ^= ( minvalue ) ^ arr [ i ] ; minvalue ++ ; } return xornum ^ minvalue ; } let arr = [ 13 , 12 , 11 , 15 ] ; let n = arr . length ; document . write ( missingNum ( arr , n ) ) ;
Find last index of a character in a string | Returns last index of x if it is present Else returns - 1. ; String in which char is to be found ; char whose index is to be found
function findLastIndex ( str , x ) { let index = - 1 ; for ( let i = 0 ; i < str . length ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; } let str = " " ; let x = ' ' ; let index = findLastIndex ( str , x ) ; if ( index == - 1 ) document . write ( " " ) ; else document . write ( " " + index ) ;
Find last index of a character in a string | Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code
function findLastIndex ( str , x ) { for ( let i = str . length - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } let str = " " ; let x = ' ' ; let index = findLastIndex ( str , x ) ; if ( index == - 1 ) document . write ( " " ) ; else document . write ( " " + index ) ;
Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; driver code
function countMaxSetBits ( left , right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } let l = 1 , r = 5 ; document . write ( countMaxSetBits ( l , r ) + " " ) ; l = 1 , r = 10 ; document . write ( countMaxSetBits ( l , r ) ) ;
Largest number less than or equal to N in BST ( Iterative Approach ) | Javascript code to find the largest value smaller than or equal to N ; To create new BST Node ; To insert a new node in BST ; If tree is empty return new node ; If key is less then or greater then node value then recur down the tree ; Return the ( unchanged ) node pointer ; Returns largest value smaller than or equal to key . If key is smaller than the smallest , it returns - 1. ; Driver code
class Node { constructor ( item ) { this . key = item ; this . left = null ; this . right = null ; } } function newNode ( item ) { let temp = new Node ( item ) ; return temp ; } function insert ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } function findFloor ( root , key ) { let curr = root , ans = null ; while ( curr != null ) { if ( curr . key <= key ) { ans = curr ; curr = curr . right ; } else curr = curr . left ; } if ( ans != null ) return ans . key ; return - 1 ; } let N = 25 ; let root = new Node ( N ) ; insert ( root , 19 ) ; insert ( root , 2 ) ; insert ( root , 1 ) ; insert ( root , 3 ) ; insert ( root , 12 ) ; insert ( root , 9 ) ; insert ( root , 21 ) ; insert ( root , 19 ) ; insert ( root , 25 ) ; document . write ( findFloor ( root , N ) ) ;
Find if given number is sum of first n natural numbers | Function to find no . of elements to be added to get s ; Apply Binary search ; Find mid ; find sum of 1 to mid natural numbers using formula ; If sum is equal to n return mid ; If greater than n do r = mid - 1 ; else do l = mid + 1 ; If not possible , return - 1 ; Drivers code
function findS ( s ) { var l = 1 , r = parseInt ( ( s / 2 ) + 1 ) ; while ( l <= r ) { var mid = parseInt ( ( l + r ) / 2 ) ; var sum = mid * parseInt ( ( mid + 1 ) / 2 ) ; if ( sum == s ) return mid ; else if ( sum > s ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } var s = 15 ; var n = findS ( s ) ; if ( n == - 1 ) document . write ( " " ) ; else document . write ( n ) ;
Search in an array of strings where non | Compare two string equals are not ; Main function to find string location ; Move mid to the middle ; If mid is empty , find closest non - empty string ; If mid is empty , search in both sides of mid and find the closest non - empty string , and set mid accordingly . ; If str is found at mid ; If str is greater than mid ; If str is smaller than mid ; Input arr of Strings . ; input Search String
function compareStrings ( str1 , str2 ) { let i = 0 ; while ( i < str1 . length - 1 && str1 [ i ] == str2 [ i ] ) i ++ ; if ( str1 [ i ] > str2 [ i ] ) return - 1 ; if ( str1 [ i ] < str2 [ i ] ) return 1 ; else return 0 ; } function searchStr ( arr , str , first , last ) { if ( first > last ) return - 1 ; let mid = parseInt ( ( last + first ) / 2 , 10 ) ; if ( arr [ mid ] == " " ) { let left = mid - 1 ; let right = mid + 1 ; while ( true ) { if ( left < right && right > last ) return - 1 ; if ( right <= last && arr [ right ] != " " ) { mid = right ; break ; } if ( left >= right && ! arr [ left ] == " " ) { mid = left ; break ; } right ++ ; left -- ; } } if ( compareStrings ( str , arr [ mid ] ) == 0 ) return mid ; if ( compareStrings ( str , arr [ mid ] ) < 0 ) return searchStr ( arr , str , mid + 1 , last ) ; return searchStr ( arr , str , first , mid - 1 ) ; } let arr = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let str = " " ; let n = arr . length ; document . write ( searchStr ( arr , str , 0 , n - 1 ) ) ;
Maximum number of plates that can be placed from top to bottom in increasing order of size | Function to count and return the max number of plates that can be placed ; Stores the maximum number of plates ; For each i - th plate , traverse all the previous plates ; If i - th plate is smaller than j - th plate ; Include the j - th plate only if current count exceeds the previously stored count ; Update the maximum count ; Driver code ; Sorting plates in decreasing order of area
function countPlates ( plates , n ) { var maximum_plates = 1 ; var dp = Array ( n ) . fill ( 1 ) ; for ( var i = 1 ; i < n ; i ++ ) { var cur = dp [ i ] ; for ( var j = i - 1 ; j >= 0 ; j -- ) { if ( plates [ i ] [ 0 ] < plates [ j ] [ 0 ] && plates [ i ] [ 1 ] < plates [ j ] [ 1 ] ) { if ( cur + dp [ j ] > dp [ i ] ) { dp [ i ] = cur + dp [ j ] ; maximum_plates = Math . max ( maximum_plates , dp [ i ] ) ; } } } } return maximum_plates ; } var plates = [ [ 6 , 4 ] , [ 5 , 7 ] , [ 1 , 2 ] , [ 3 , 3 ] , [ 7 , 9 ] ] ; var n = plates . length ; plates . sort ( ( v1 , v2 ) => { return ( ( v2 [ 0 ] * v2 [ 1 ] ) - ( v1 [ 0 ] * v1 [ 1 ] ) ) ; } ) ; document . write ( countPlates ( plates , n ) ) ;
Sort an array in increasing order of their Multiplicative Persistence | Function to find the number of steps required to reduce a given number to a single - digit number ; Stores the required result ; Iterate until a single digit number is not obtained ; Store the number in a temporary variable ; Iterate over all digits and store their product in num ; Increment the answer by 1 ; Return the result ; Comparator function to sort the array ; Count number of steps required to reduce X and Y into single digits integer ; Return true ; Function to sort the array according to the number of steps required to reduce a given number into a single digit number ; Sort the array using the comparator function ; Print the array after sorting ; Driver Code ; Function Call
function countReduction ( num ) { let ans = 0 ; while ( num >= 10 ) { let temp = num ; num = 1 ; while ( temp > 0 ) { let digit = temp % 10 ; temp = Math . floor ( temp / 10 ) ; num *= digit ; } ans ++ ; } return ans ; } function compare ( x , y ) { let x1 = countReduction ( x ) ; let y1 = countReduction ( y ) ; if ( x1 < y1 ) return - 1 ; return 1 ; } function sortArray ( a , n ) { a . sort ( compare ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 39 , 999 , 4 , 9876 ] ; let N = arr . length ; sortArray ( arr , N ) ;
Check if a Binary String can be sorted in decreasing order by removing non | Function to sort the given string in decreasing order by removing the non adjacent characters ; Keeps the track whether the string can be sorted or not ; Traverse the given string S ; Check if S [ i ] and S [ i + 1 ] are both '1' ; Traverse the string S from the indices i to 0 ; If S [ j ] and S [ j + 1 ] is equal to 0 ; Mark flag false ; If flag is 0 , then it is not possible to sort the string ; Otherwise ; Driver Code
function canSortString ( S , N ) { let flag = 1 ; let i , j ; for ( let i = N - 2 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' && S [ i + 1 ] == ' ' ) { break ; } } for ( let j = i ; j >= 0 ; j -- ) { if ( S [ j ] == ' ' && S [ j + 1 ] == ' ' ) { flag = 0 ; break ; } } if ( flag == 0 ) { return " " ; } else { return " " ; } } let S = " " ; let N = S . length ; document . write ( canSortString ( S , N ) ) ;
Rearrange array elements to maximize the sum of MEX of all prefix arrays | Function to find the maximum sum of MEX of prefix arrays for any arrangement of the given array ; Stores the final arrangement ; Sort the array in increasing order ; Iterate over the array arr [ ] ; Iterate over the array , arr [ ] and push the remaining occurrences of the elements into ans [ ] ; Print the array , ans [ ] ; Given array ; Store the size of the array ; Function Call
function maximumMex ( arr , N ) { var ans = [ ] ; arr . sort ( ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( i == 0 arr [ i ] != arr [ i - 1 ] ) ans . push ( arr [ i ] ) ; } for ( i = 0 ; i < N ; i ++ ) { if ( i > 0 && arr [ i ] == arr [ i - 1 ] ) ans . push ( arr [ i ] ) ; } for ( i = 0 ; i < N ; i ++ ) document . write ( ans [ i ] + " " ) ; } var arr = [ 1 , 0 , 0 ] ; var N = arr . length ; maximumMex ( arr , N ) ;