codeparrot/xlcost-text-to-code huggingface dataset:
Trapping Rain Water | Javascript implementation of the approach ; indices to traverse the array ; To store Left max and right max for two pointers left and right ; To store the total amount of rain water trapped ; We need check for minimum of left and right max for each element ; Add the difference between current value and right max at index r ; Update right max ; Update right pointer ; Add the difference between current value and left max at index l ; Update left max ; Update left pointer ; Driver code
function maxWater ( arr , n ) { let left = 0 ; let right = n - 1 ; let l_max = 0 ; let r_max = 0 ; let result = 0 ; while ( left <= right ) { if ( r_max <= l_max ) { result += Math . max ( 0 , r_max - arr [ right ] ) ; r_max = Math . max ( r_max , arr [ right ] ) ; right -= 1 ; } else { result += Math . max ( 0 , l_max - arr [ left ] ) ; l_max = Math . max ( l_max , arr [ left ] ) ; left += 1 ; } } return result ; } let arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let n = arr . length ; document . write ( maxWater ( arr , n ) ) ;
Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | Javascript code for median with case of returning double value when even number of elements are present in both array combinely ; Function to find max ; Function to find minimum ; Function to find median of two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . and if j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . Searching on right ; if i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have any elements in the  first half from a[] so we  returning the last element in  b[] from the first half. ; and this condition happens when we don 't have any elements in the  first half from b[] so we  returning the last element in  a[] from the first half. ; calculating the median . If number of elements is odd there is one middle element . ; Elements from a [ ] in the second half is an empty set . ; Elements from b [ ] in the second half is an empty set . ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
let a = [ 900 ] ; let b = [ 10 , 13 , 14 ] ; function maximum ( a , b ) { return a > b ? a : b ; } function minimum ( a , b ) { return a < b ? a : b ; } function findMedianSortedArrays ( n , m ) { let min_index = 0 , max_index = n , i = 0 , j = 0 , median = 0 ; while ( min_index <= max_index ) { i = Math . floor ( ( min_index + max_index ) / 2 ) ; j = Math . floor ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) median = b [ j - 1 ] ; else if ( j == 0 ) median = a [ i - 1 ] ; else median = maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; break ; } } if ( ( n + m ) % 2 == 1 ) return median ; if ( i == n ) return ( median + b [ j ] ) / 2.0 ; if ( j == m ) return ( median + a [ i ] ) / 2.0 ; return ( median + minimum ( a [ i ] , b [ j ] ) ) / 2.0 ; } let n = a . length ; let m = b . length ; if ( n < m ) document . write ( " " + findMedianSortedArrays ( n , m ) ) ; else document . write ( " " + findMedianSortedArrays ( m , n ) ) ;
Print uncommon elements from two sorted arrays | JavaScript program to find uncommon elements of two sorted arrays ; If not common , prlet smaller ; Skip common element ; prleting remaining elements ; Driver Code
function prletUncommon ( arr1 , arr2 , n1 , n2 ) { let i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) { document . write ( arr1 [ i ] + " " ) ; i ++ ; k ++ ; } else if ( arr2 [ j ] < arr1 [ i ] ) { document . write ( arr2 [ j ] + " " ) ; k ++ ; j ++ ; } else { i ++ ; j ++ ; } } while ( i < n1 ) { document . write ( arr1 [ i ] + " " ) ; i ++ ; k ++ ; } while ( j < n2 ) { document . write ( arr2 [ j ] + " " ) ; j ++ ; k ++ ; } } let arr1 = [ 10 , 20 , 30 ] ; let arr2 = [ 20 , 25 , 30 , 40 , 50 ] ; let n1 = arr1 . length ; let n2 = arr2 . length ; prletUncommon ( arr1 , arr2 , n1 , n2 ) ;
Least frequent element in an array | JavaScript program to find the least frequent element in an array . ; Sort the array ; find the min frequency using linear traversal ; If last element is least frequent ; Driver code
function leastFrequent ( arr , n ) { arr . sort ( ) ; let min_count = n + 1 , res = - 1 ; let curr_count = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } let arr = [ 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 ] ; let n = arr . length ; document . write ( leastFrequent ( arr , n ) ) ;
Least frequent element in an array | JavaScript program to find the least frequent element in an array . ; Insert all elements in hash . ; find the min frequency ; driver program
function leastFrequent ( arr , n ) { var hash = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( hash . has ( arr [ i ] ) ) hash . set ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) else hash . set ( arr [ i ] , 1 ) ; } var min_count = n + 1 , res = - 1 ; hash . forEach ( ( value , key ) => { if ( min_count >= value ) { res = key ; min_count = value ; } } ) ; return res ; } var arr = [ 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 ] ; var n = arr . length ; document . write ( leastFrequent ( arr , n ) ) ;
Maximum sum of increasing order elements from n arrays | JavaScript program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; max_smaller equals to INT_MIN means no element is found in a [ i ] so return 0 ; Driver code
let M = 4 ; function maximumSum ( a , n ) { let prev = Math . max ( a [ n - 1 ] [ 0 ] , a [ n - 1 ] [ M - 1 ] + 1 ) ; let sum = prev ; for ( let i = n - 2 ; i >= 0 ; i -- ) { let max_smaller = Number . MIN_VALUE ; for ( let j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev && a [ i ] [ j ] > max_smaller ) max_smaller = a [ i ] [ j ] ; } if ( max_smaller == Number . MIN_VALUE ) return 0 ; prev = max_smaller ; sum += max_smaller ; } return sum ; } let arr = [ [ 1 , 7 , 3 , 4 ] , [ 4 , 2 , 5 , 1 ] , [ 9 , 5 , 1 , 8 ] ] ; let n = arr . length ; document . write ( maximumSum ( arr , n ) ) ;
Pairs such that one is a power multiple of other | function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; driver program
function countPairs ( A , n , k ) { var ans = 0 ; A . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { var x = 0 ; while ( ( A [ i ] * Math . pow ( k , x ) ) <= A [ j ] ) { if ( ( A [ i ] * Math . pow ( k , x ) ) == A [ j ] ) { ans ++ ; break ; } x ++ ; } } } return ans ; } var A = [ 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 ] ; var n = A . length ; var k = 3 ; document . write ( countPairs ( A , n , k ) ) ;
Minimum distance between two occurrences of maximum | function to return min distance ; case a ; case b ; case c ; Driver code
function minDistance ( arr , n ) { let maximum_element = arr [ 0 ] ; let min_dis = n ; let index = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( maximum_element == arr [ i ] ) { min_dis = Math . min ( min_dis , ( i - index ) ) ; index = i ; } else if ( maximum_element < arr [ i ] ) { maximum_element = arr [ i ] ; min_dis = n ; index = i ; } else continue ; } return min_dis ; } let arr = [ 6 , 3 , 1 , 3 , 6 , 4 , 6 ] ; let n = arr . length ; document . write ( " " + minDistance ( arr , n ) ) ;
Find final value if we double after every successful search in array | Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver code
function findValue ( arr , n , k ) { for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] == k ) k *= 2 ; return k ; } let arr = [ 2 , 3 , 4 , 10 , 8 , 1 ] , k = 2 ; let n = arr . length ; document . write ( findValue ( arr , n , k ) ) ;
Last duplicate element in a sorted array | JavaScript program to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; If we reach here , then no duplicate found . ; Driver code
function dupLastIndex ( arr , n ) { if ( arr == null n <= 0 ) return ; for ( let i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] == arr [ i - 1 ] ) { document . write ( " " + i + " " ) ; document . write ( " " + arr [ i ] + " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 1 , 5 , 5 , 6 , 6 , 7 , 9 ] ; let n = arr . length ; dupLastIndex ( arr , n ) ;
Find an array element such that all elements are divisible by it | function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; Driver code
function findSmallest ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } let a = [ 25 , 20 , 5 , 10 , 100 ] ; let n = a . length ; document . write ( findSmallest ( a , n ) ) ;
Find an array element such that all elements are divisible by it | function to find the smallest element ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver Code
function min_element ( a ) { let min = Number . MAX_VALUE ; let i ; for ( i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } function findSmallest ( a , n ) { let smallest = min_element ( a ) ; for ( let i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } let a = [ 25 , 20 , 5 , 10 , 100 ] ; let n = a . length ; document . write ( findSmallest ( a , n ) ) ;
Maximum in array which is at | Function to find the index of Max element that satisfies the condition ; Finding index of max of the array ; Returns - 1 if the max element is not twice of the i - th element . ; Driver Code
function findIndex ( arr , len ) { let maxIndex = 0 ; for ( let i = 0 ; i < len ; ++ i ) if ( arr [ i ] > arr [ maxIndex ] ) maxIndex = i ; for ( let i = 0 ; i < len ; ++ i ) if ( maxIndex != i && arr [ maxIndex ] < 2 * arr [ i ] ) return - 1 ; return maxIndex ; } let arr = [ 3 , 6 , 1 , 0 ] ; let len = arr . length ; document . write ( findIndex ( arr , len ) ) ;
Consecutive steps to roof top | Function to count consecutive steps ; count the number of consecutive increasing height building ; Driver code
function find_consecutive_steps ( arr , len ) { let count = 0 ; let maximum = 0 ; for ( let index = 1 ; index < len ; index ++ ) { if ( arr [ index ] > arr [ index - 1 ] ) count ++ ; else { maximum = Math . max ( maximum , count ) ; count = 0 ; } } return Math . max ( maximum , count ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let len = arr . length ; document . write ( find_consecutive_steps ( arr , len ) ) ;
Maximum difference between groups of size two | Javascript program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code
function CalculateMax ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let min_sum = arr [ 0 ] + arr [ 1 ] ; let max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . abs ( max_sum - min_sum ) ) ; } let arr = [ 6 , 7 , 1 , 11 ] ; let n = arr . length ; document . write ( CalculateMax ( arr , n ) ) ;
Minimum difference between groups of size two | Javascript program to find minimum difference between groups of highest and lowest sums ; Sorting the whole array . ; Generating sum groups . ; driver code
function calculate ( a , n ) { a . sort ( ) ; let i , j ; let s = [ ] ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . push ( ( a [ i ] + a [ j ] ) ) ; let mini = Math . min ( ... s ) ; let maxi = Math . max ( ... s ) ; return Math . abs ( maxi - mini ) ; } let a = [ 2 , 6 , 4 , 3 ] ; let n = a . length ; document . write ( calculate ( a , n ) ) ;
Closest numbers from a list of unsorted integers | Returns minimum difference between any two pair in arr [ 0. . n - 1 ] ; Sort array elements ; Compare differences of adjacent pairs to find the minimum difference . ; Traverse array again and print all pairs with difference as minDiff . ; Driver code
function printMinDiffPairs ( arr , n ) { if ( n <= 1 ) return ; arr . sort ( ) ; let minDiff = arr [ 1 ] - arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) minDiff = Math . min ( minDiff , arr [ i ] - arr [ i - 1 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { if ( ( arr [ i ] - arr [ i - 1 ] ) == minDiff ) { document . write ( " " + arr [ i - 1 ] + " " + arr [ i ] + " " ) ; } } } let arr = [ 5 , 3 , 2 , 4 , 1 ] ; let n = arr . length ; printMinDiffPairs ( arr , n ) ;
Maximum absolute difference of value and index sums | javascript program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver Function
let MAX = 256 ; function calculateDiff ( i , j , array ) { return Math . abs ( array [ i ] - array [ j ] ) + Math . abs ( i - j ) ; } function maxDistance ( array ) { let result = 0 ; for ( let i = 0 ; i < array . length ; i ++ ) { for ( let j = i ; j < array . length ; j ++ ) { result = Math . max ( result , calculateDiff ( i , j , array ) ) ; } } return result ; } let array = [ - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 ] ; document . write ( maxDistance ( array ) ) ;
Maximum absolute difference of value and index sums | Function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; Updating max and min variables as described in algorithm . ; Calculating maximum absolute difference . ; Driver Code
function maxDistance ( array ) { let max1 = Number . MIN_VALUE ; let min1 = Number . MAX_VALUE ; let max2 = Number . MIN_VALUE ; let min2 = Number . MAX_VALUE ; for ( let i = 0 ; i < array . length ; i ++ ) { max1 = Math . max ( max1 , array [ i ] + i ) ; min1 = Math . min ( min1 , array [ i ] + i ) ; max2 = Math . max ( max2 , array [ i ] - i ) ; min2 = Math . min ( min2 , array [ i ] - i ) ; } return Math . max ( max1 - min1 , max2 - min2 ) ; } let array = [ - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 ] ; document . write ( maxDistance ( array ) ) ;
Number of local extrema in an array | function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; Driver code
function extrema ( a , n ) { let count = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } let a = [ 1 , 0 , 2 , 1 ] ; let n = a . length ; document . write ( extrema ( a , n ) ) ;
Find closest number in array | Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver Code
function findClosest ( arr , target ) { let n = arr . length ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; let i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } function getClosest ( val1 , val2 , target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } let arr = [ 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 ] ; let target = 11 ; document . write ( findClosest ( arr , target ) ) ;
Number of pairs with maximum sum | Function to find the number of maximum pair sums ; traverse through all the pairs ; Traverse through all pairs and keep a count of the number of maximum pairs ; Driver Code
function sum ( a , n ) { let maxSum = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) maxSum = Math . max ( maxSum , a [ i ] + a [ j ] ) ; let c = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } let array = [ 1 , 1 , 1 , 2 , 2 , 2 ] ; let n = array . length ; document . write ( sum ( array , n ) ) ;
Number of pairs with maximum sum | function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; Driver Code
function sum ( a , n ) { let maxVal = a [ 0 ] , maxCount = 1 ; let secondMax = Number . MIN_VALUE ; let secondMaxCount = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * parseInt ( ( maxCount - 1 ) / 2 , 10 ) ; return secondMaxCount ; } let array = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 ] ; let n = array . length ; document . write ( sum ( array , n ) ) ;
Find first k natural numbers missing in given array | Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code
function printKMissing ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) ; let i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; let count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { document . write ( curr + " " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { document . write ( curr , " " ) ; curr ++ ; count ++ ; } } let arr = new Array ( 2 , 3 , 4 ) ; let n = arr . length ; let k = 3 ; printKMissing ( arr , n , k ) ;
Find first k natural numbers missing in given array | Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code
function printmissingk ( arr , n , k ) { let d = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) d . set ( arr [ i ] , arr [ i ] ) ; let cnt = 1 ; let fl = 0 ; for ( let i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . has ( cnt ) ) { fl += 1 ; document . write ( cnt + " " ) ; if ( fl == k ) break ; } cnt += 1 ; } } let arr = [ 1 , 4 , 3 ] ; let n = arr . length ; let k = 3 ; printmissingk ( arr , n , k ) ;
Noble integers in an array ( count of greater elements is equal to value ) | Returns a Noble integer if present , else returns - 1. ; If count of greater elements is equal to arr [ i ] ; Driver code
function nobleInteger ( arr ) { let size = arr . length ; for ( let i = 0 ; i < size ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < size ; j ++ ) if ( arr [ i ] < arr [ j ] ) count ++ ; if ( count == arr [ i ] ) return arr [ i ] ; } return - 1 ; } let arr = [ 10 , 3 , 20 , 40 , 2 ] ; let res = nobleInteger ( arr ) ; if ( res != - 1 ) document . write ( " " + " " + res ) ; else document . write ( " " + " " ) ;
Noble integers in an array ( count of greater elements is equal to value ) | Returns a Noble integer if present , else returns - 1. ; Return a Noble element if present before last . ; In case of duplicates , we reach last occurrence here . ; Driver code
function nobleInteger ( arr ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let n = arr . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; } let arr = [ 10 , 3 , 20 , 40 , 2 ] ; let res = nobleInteger ( arr ) ; if ( res != - 1 ) document . write ( " " + res ) ; else document . write ( " " ) ;
Noble integers in an array ( count of greater elements is equal to value ) | JavaScript program to find Noble elements in an array . ; Declare a countArr which keeps count of all elements greater than or equal to arr [ i ] . Initialize it with zero . ; Iterating through the given array ; If current element is less than zero , it cannot be a solution so we skip it . ; If current element is >= size of input array , if will be greater than all elements which can be considered as our solution , as it cannot be greater than size of array . ; Else we increase the count of elements >= our current array in countArr ; Initially , countArr [ n ] is count of elements greater than all possible solutions ; Iterating through countArr ; If totalGreater = current index , means we found arr [ i ] for which count of elements >= arr [ i ] is equal to arr [ i ] ; If at any point count of elements greater than arr [ i ] becomes more than current index , then it means we can no longer have a solution ; Adding count of elements >= arr [ i ] to totalGreater . ; Driver Code
function nobleInteger ( arr , n ) { let countArr = new Uint8Array ( n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { continue ; } else if ( arr [ i ] >= n ) { countArr [ n ] ++ ; } else { countArr [ arr [ i ] ] ++ ; } } let totalGreater = countArr [ n ] ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( totalGreater == i && countArr [ i ] > 0 ) { return i ; } else if ( totalGreater > i ) { return - 1 ; } totalGreater += countArr [ i ] ; } return - 1 ; } let arr = [ 10 , 3 , 20 , 40 , 2 ] ; let res = nobleInteger ( arr , 5 ) ; if ( res != - 1 ) document . write ( " " + res ) ; else document . write ( " " ) ;
Minimum sum of absolute difference of pairs of two arrays | Returns minimum possible pairwise absolute difference of two arrays . ; Sort both arrays ; Find sum of absolute differences ; Both a [ ] and b [ ] must be of same size .
function findMinSum ( a , b , n ) { a . sort ( ) ; b . sort ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum = sum + Math . abs ( a [ i ] - b [ i ] ) ; return sum ; } let a = [ 4 , 1 , 8 , 7 ] ; let b = [ 2 , 3 , 6 , 5 ] ; let n = a . length ; document . write ( findMinSum ( a , b , n ) ) ;