codeparrot/xlcost-text-to-code huggingface dataset:
Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort the array , else return false . ; Copying the array . ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driver Code
function checkReverse ( arr , n ) { let temp = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { temp [ i ] = arr [ i ] ; } temp . sort ( ) ; let front ; for ( front = 0 ; front < n ; front ++ ) { if ( temp [ front ] != arr [ front ] ) { break ; } } let back ; for ( back = n - 1 ; back >= 0 ; back -- ) { if ( temp [ back ] != arr [ back ] ) { break ; } } if ( front >= back ) { return true ; } do { front ++ ; if ( arr [ front - 1 ] < arr [ front ] ) { return false ; } } while ( front != back ) ; return true ; } let arr = [ 1 , 2 , 5 , 4 , 3 ] ; let n = arr . length ; if ( checkReverse ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort t he array , else return false . ; Find first increasing part ; Find reversed part ; Find last increasing part ; To handle cases like { 1 , 2 , 3 , 4 , 20 , 9 , 16 , 17 } ; Driver program
function checkReverse ( arr , n ) { if ( n == 1 ) return true ; let i ; for ( i = 1 ; i < n && arr [ i - 1 ] < arr [ i ] ; i ++ ) ; if ( i == n ) return true ; let j = i ; while ( j < n && arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) return false ; j ++ ; } if ( j == n ) return true ; let k = j ; if ( arr [ k ] < arr [ i - 1 ] ) return false ; while ( k > 1 && k < n ) { if ( arr [ k ] < arr [ k - 1 ] ) return false ; k ++ ; } return true ; } let arr = [ 1 , 3 , 4 , 10 , 9 , 8 ] ; let n = arr . length ; if ( checkReverse ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Making elements of two arrays same with minimum increment / decrement | Javascript program to find minimum increment / decrement operations to make array elements same . ; sorting both arrays in ascending order ; variable to store the final result ; After sorting both arrays Now each array is in non - decreasing order . Thus , we will now compare each element of the array and do the increment or decrement operation depending upon the value of array b [ ] . ; Driver code
function MinOperation ( a , b , n ) { a . sort ( function ( a , b ) { return a - b } ) ; b . sort ( function ( a , b ) { return a - b } ) ; let result = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( a [ i ] > b [ i ] ) result = result + Math . abs ( a [ i ] - b [ i ] ) ; else if ( a [ i ] < b [ i ] ) result = result + Math . abs ( a [ i ] - b [ i ] ) ; } return result ; } let a = [ 3 , 1 , 1 ] ; let b = [ 1 , 2 , 2 ] ; let n = a . length ; document . write ( MinOperation ( a , b , n ) ) ;
Sorting array except elements in a subarray | Sort whole array a [ ] except elements in range a [ l . . r ] ; Copy all those element that need to be sorted to an auxiliary array b [ ] ; sort the array b ; Copy sorted elements back to a [ ] ; Driver code
function sortExceptUandL ( a , l , u , n ) { let b = [ ] ; for ( let i = 0 ; i < l ; i ++ ) b [ i ] = a [ i ] ; for ( let i = u + 1 ; i < n ; i ++ ) b [ l + ( i - ( u + 1 ) ) ] = a [ i ] ; b . sort ( ) ; for ( let i = 0 ; i < l ; i ++ ) a [ i ] = b [ i ] ; for ( let i = u + 1 ; i < n ; i ++ ) a [ i ] = b [ l + ( i - ( u + 1 ) ) ] ; } let a = [ 5 , 4 , 3 , 12 , 14 , 9 ] ; let n = a . length ; let l = 2 , u = 4 ; sortExceptUandL ( a , l , u , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ;
Sorting all array elements except one | Javascript program to sort all elements except element at index k . ; Move k - th element to end ; Sort all elements except last ; Store last element ( originally k - th ) ; Move all elements from k - th to one position ahead . ; Restore k - th element ; Move k - th element to end
function sortExceptK ( arr , k , n ) { let temp = arr [ k ] ; arr [ k ] = arr [ n - 1 ] ; arr [ n - 1 ] = temp ; arr . sort ( function ( a , b ) { return a - b } ) ; let last = arr [ n - 1 ] ; for ( let i = n - 1 ; i > k ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ k ] = last ; temp = arr [ k ] ; arr [ k ] = arr [ n - 1 ] ; arr [ n - 1 ] = temp ; return 0 ; } let a = [ 10 , 4 , 11 , 7 , 6 , 20 ] ; let k = 2 ; let n = a . length ; sortExceptK ( a , k , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ;
Sort the linked list in the order of elements appearing in the array | Linked list node ; Function to insert a node at the beginning of the linked list ; function to print the linked list ; Function that sort list in order of apperaing elements in an array ; Store frequencies of elements in a hash table . ; One by one put elements in lis according to their appearance in array ; Update ' frequency ' nodes with value equal to arr [ i ] ; Modify list data as element appear in an array ; Driver Code ; creating the linked list ; Function call to sort the list in order elements are apperaing in an array ; print the modified linked list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } function sortlist ( arr , N , head ) { var hash = new Map ( ) ; var temp = head ; while ( temp != null ) { if ( hash . has ( temp . data ) ) hash . set ( temp . data , hash . get ( temp . data ) + 1 ) ; else hash . set ( temp . data , 1 ) ; temp = temp . next ; } temp = head ; for ( var i = 0 ; i < N ; i ++ ) { var frequency = hash . get ( arr [ i ] ) ; while ( frequency -- > 0 ) { temp . data = arr [ i ] ; temp = temp . next ; } } } var head = null ; var arr = [ 5 , 1 , 3 , 2 , 8 ] ; var N = arr . length ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 5 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; sortlist ( arr , N , head ) ; document . write ( " " + " " ) ; printList ( head ) ;
Maximum number of partitions that can be sorted individually to make sorted | Function to find maximum partitions . ; Find maximum in prefix arr [ 0. . i ] ; If maximum so far is equal to index , we can make a new partition ending at index i . ; Driver code
function maxPartitions ( arr , n ) { let ans = 0 , max_so_far = 0 ; for ( let i = 0 ; i < n ; ++ i ) { max_so_far = Math . max ( max_so_far , arr [ i ] ) ; if ( max_so_far == i ) ans ++ ; } return ans ; } let arr = [ 1 , 0 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( maxPartitions ( arr , n ) ) ;
Ropes left after every removal of smallest | Function print how many Ropes are Left After Every Cutting operation ; Sort all Ropes in increasing order of their length ; min length rope ; Now traverse through the given Ropes in increase order of length ; After cutting if current rope length is greater than '0' that mean all ropes to it 's right side are also  greater than 0 ; Now current rope become min length rope ; After first operation all ropes length become zero ; Driver Code
function cuttringRopes ( Ropes , n ) { Ropes . sort ( ) ; let singleOperation = 0 ; let cuttingLenght = Ropes [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( Ropes [ i ] - cuttingLenght > 0 ) { document . write ( n - i + " " ) ; cuttingLenght = Ropes [ i ] ; singleOperation ++ ; } } if ( singleOperation == 0 ) document . write ( " " ) ; } let Ropes = [ 5 , 1 , 1 , 2 , 3 , 5 ] ; let n = Ropes . length ; cuttringRopes ( Ropes , n ) ;
Rank of all elements in an array | Function to find rank ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s . ; Use formula to obtain rank ; Driver Code
function rankify ( A , n ) { var R = [ ... Array ( n ) ] ; for ( var i = 0 ; i < n ; i ++ ) { var r = 1 , s = 1 ; for ( var j = 0 ; j < n ; j ++ ) { if ( j != i && A [ j ] < A [ i ] ) r += 1 ; if ( j != i && A [ j ] == A [ i ] ) s += 1 ; } R [ i ] = parseFloat ( r + parseFloat ( s - 1 ) / parseFloat ( 2 ) ) ; } for ( var i = 0 ; i < n ; i ++ ) document . write ( parseFloat ( R [ i ] ) . toFixed ( 1 ) + " " ) ; } var A = [ 1 , 2 , 5 , 2 , 1 , 25 , 2 ] ; var n = A . length ; for ( var i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; rankify ( A , n ) ;
Minimum number of subtract operation to make an array decreasing | Function to count minimum no of operation ; Count how many times we have to subtract . ; Check an additional subtraction is required or not . ; Modify the value of arr [ i ] ; Count total no of subtraction ; Driver code
function min_noOf_operation ( arr , n , k ) { let noOfSubtraction ; let res = 0 ; for ( let i = 1 ; i < n ; i ++ ) { noOfSubtraction = 0 ; if ( arr [ i ] > arr [ i - 1 ] ) { noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) noOfSubtraction ++ ; arr [ i ] = arr [ i ] - k * noOfSubtraction ; } res = res + noOfSubtraction ; } return res ; } let arr = [ 1 , 1 , 2 , 3 ] ; let N = 4 ; let k = 5 ; document . write ( Math . floor ( min_noOf_operation ( arr , N , k ) ) ) ;
Maximize the sum of arr [ i ] * i | JavaScript program to find the maximum value of i * arr [ i ] ; Sort the array ; Finding the sum of arr [ i ] * i ; Driver Code
function maxSum ( arr , n ) { arr . sort ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ( arr [ i ] * i ) ; return sum ; } let arr = [ 3 , 5 , 6 , 1 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;
Pairs with Difference less than K | Function to count pairs ; Driver code
function countPairs ( a , n , k ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( a [ j ] - a [ i ] ) < k ) res ++ ; return res ; } var a = [ 1 , 10 , 4 , 2 ] ; var k = 3 ; var n = a . length ; document . write ( countPairs ( a , n , k ) ) ;
Pairs with Difference less than K | JavaScript code to find count of Pairs with difference less than K . ; To sort the array . ; Keep incrementing result while subsequent elements are within limits . ; Driver code
function countPairs ( a , n , k ) { a . sort ( ( a , b ) => a - b ) ; let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let j = i + 1 ; while ( j < n && a [ j ] - a [ i ] < k ) { res ++ ; j ++ ; } } return res ; } let a = [ 1 , 10 , 4 , 2 ] ; let k = 3 ; let n = a . length ; document . write ( countPairs ( a , n , k ) + " " ) ;
Merging two unsorted arrays in sorted order | Function to merge array in sorted order ; Sorting a [ ] and b [ ] ; Merge two sorted arrays into res [ ] ; Merging remaining elements of a [ ] ( if any ) ; Merging remaining elements of b [ ] ( if any ) ; Driver code ; Final merge list
function sortedMerge ( a , b , res , n , m ) { a . sort ( ( a , b ) => a - b ) ; b . sort ( ( a , b ) => a - b ) ; let i = 0 , j = 0 , k = 0 ; while ( i < n && j < m ) { if ( a [ i ] <= b [ j ] ) { res [ k ] = a [ i ] ; i += 1 ; k += 1 ; } else { res [ k ] = b [ j ] ; j += 1 ; k += 1 ; } } while ( i < n ) { res [ k ] = a [ i ] ; i += 1 ; k += 1 ; } while ( j < m ) { res [ k ] = b [ j ] ; j += 1 ; k += 1 ; } } let a = [ 10 , 5 , 15 ] ; let b = [ 20 , 3 , 2 , 12 ] ; let n = a . length ; let m = b . length ; let res = new Array ( n + m ) ; sortedMerge ( a , b , res , n , m ) ; document . write ( " " ) ; for ( let i = 0 ; i < n + m ; i ++ ) document . write ( " " + res [ i ] ) ;
Maximizing Unique Pairs from two arrays | Returns count of maximum pairs that caan be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; To keep track of visited elements of b [ ] ; For every element of a [ ] , find a pair for it and break as soon as a pair is found . ; Increasing the count if a pair is formed . ; Making the corresponding flag array element as 1 indicating the element in the second array element has been used . ; We break the loop to make sure an element of a [ ] is used only once . ; Driver code
function findMaxPairs ( a , b , n , k ) { a . sort ( function ( c , d ) { return c - d ; } ) ; b . sort ( function ( c , d ) { return c - d ; } ) let flag = new Array ( n ) ; for ( let i = 0 ; i < flag . length ; i ++ ) { flag [ i ] = false ; } let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( Math . abs ( a [ i ] - b [ j ] ) <= k && flag [ j ] == false ) { result ++ ; flag [ j ] = true ; break ; } } } return result ; } let a = [ 10 , 15 , 20 ] ; let b = [ 17 , 12 , 24 ] ; let n = a . length ; let k = 3 ; document . write ( findMaxPairs ( a , b , n , k ) ) ;
Maximizing Unique Pairs from two arrays | Returns count of maximum pairs that caan be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; Increasing array pointer of both the first and the second array . ; Increasing array pointer of the second array . ; Increasing array pointer of the first array . ; Driver code
function findMaxPairs ( a , b , n , k ) { a . sort ( function ( a , b ) { return a - b } ) ; b . sort ( function ( a , b ) { return a - b } ) ; let result = 0 ; for ( let i = 0 , j = 0 ; i < n && j < n ; ) { if ( Math . abs ( a [ i ] - b [ j ] ) <= k ) { result ++ ; i ++ ; j ++ ; } else if ( a [ i ] > b [ j ] ) j ++ ; else i ++ ; } return result ; } let a = [ 10 , 15 , 20 ] ; let b = [ 17 , 12 , 24 ] ; let n = a . length ; let k = 3 ; document . write ( findMaxPairs ( a , b , n , k ) ) ;
Sum of minimum absolute difference of each array element | function to find the sum of minimum absolute difference ; sort the given array ; initialize sum ; min absolute difference for the 1 st array element ; min absolute difference for the last array element ; find min absolute difference for rest of the array elements and add them to sum ; required sum ; Driver Code
function sumOfMinAbsDifferences ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let sum = 0 ; sum += Math . abs ( arr [ 0 ] - arr [ 1 ] ) ; sum += Math . abs ( arr [ n - 1 ] - arr [ n - 2 ] ) ; for ( let i = 1 ; i < n - 1 ; i ++ ) sum += Math . min ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) , Math . abs ( arr [ i ] - arr [ i + 1 ] ) ) ; return sum ; } let arr = [ 5 , 10 , 1 , 4 , 8 , 7 ] ; let n = arr . length ; document . write ( " " + sumOfMinAbsDifferences ( arr , n ) ) ;
Smallest Difference pair of values between two unsorted Arrays | function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; Return final sma result ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result
function findSmallestDifference ( A , B , m , n ) { A . sort ( ( a , b ) => a - b ) ; B . sort ( ( a , b ) => a - b ) ; let a = 0 , b = 0 ; let result = Number . MAX_SAFE_INTEGER ; while ( a < m && b < n ) { if ( Math . abs ( A [ a ] - B [ b ] ) < result ) result = Math . abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else b ++ ; } return result ; } let A = [ 1 , 2 , 11 , 5 ] ; let B = [ 4 , 12 , 19 , 23 , 127 , 235 ] ; let m = A . length ; let n = B . length ; document . write ( findSmallestDifference ( A , B , m , n ) ) ;
Find elements larger than half of the elements in an array | Prints elements larger than n / 2 element ; Sort the array in ascending order ; Print last ceil ( n / 2 ) elements ; Driver Code
function findLarger ( arr , n ) { arr . sort ( ) ; for ( let i = n - 1 ; i >= n / 2 ; i -- ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 3 , 6 , 1 , 0 , 9 ] ; let n = arr . length ; findLarger ( arr , n ) ;
Find the element that appears once in an array where every other element appears twice | singleelement function ; Driver code
function singleelement ( arr , n ) { let low = 0 , high = n - 2 ; let mid ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( arr [ mid ] == arr [ mid ^ 1 ] ) { low = mid + 1 ; } else { high = mid - 1 ; } } return arr [ low ] ; } let arr = [ 2 , 3 , 5 , 4 , 5 , 3 , 4 ] ; let size = arr . length ; document . write ( singleelement ( arr , size ) ) ;
Count triplets with sum smaller than a given value | A Simple Javascript program to count triplets with sum smaller than a given value ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver method to test the above function
let arr = [ 5 , 1 , 3 , 4 , 7 ] ; function countTriplets ( n , sum ) { let ans = 0 ; for ( let i = 0 ; i < n - 2 ; i ++ ) { for ( let j = i + 1 ; j < n - 1 ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) ans ++ ; } } return ans ; } let sum = 12 ; document . write ( countTriplets ( arr . length , sum ) ) ;
Count triplets with sum smaller than a given value | A Simple Javascript program to count triplets with sum smaller than a given value ; Sort input array ; Initialize result ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there can be total k - j third elements . ; Driver method to test the above function
let arr = [ 5 , 1 , 3 , 4 , 7 ] ; function countTriplets ( n , sum ) { arr . sort ( function ( a , b ) { return b - a } ) ; let ans = 0 ; for ( let i = 0 ; i < n - 2 ; i ++ ) { let j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { ans += ( k - j ) ; j ++ ; } } } return ans ; } let sum = 12 ; document . write ( countTriplets ( arr . length , sum ) ) ;
Number of unique triplets whose XOR is zero | function to count the number of unique triplets whose xor is 0 ; To store values that are present ; stores the count of unique triplets ; traverse for all i , j pairs such that j > i ; xor of a [ i ] and a [ j ] ; if xr of two numbers is present , then increase the count ; returns answer ; Driver code
function countTriplets ( a , n ) { var s = [ ] ; for ( i = 0 ; i < n ; i ++ ) s . push ( a [ i ] ) ; var count = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var xr = a [ i ] ^ a [ j ] ; if ( s . includes ( xr ) && xr != a [ i ] && xr != a [ j ] ) count ++ ; } } return count / 3 ; } var a = [ 1 , 3 , 5 , 10 , 14 , 15 ] ; var n = a . length ; document . write ( countTriplets ( a , n ) ) ;
Find the Missing Number | Function to get the missing number ; Driver Code
function getMissingNo ( a , n ) { let total = Math . floor ( ( n + 1 ) * ( n + 2 ) / 2 ) ; for ( let i = 0 ; i < n ; i ++ ) total -= a [ i ] ; return total ; } let arr = [ 1 , 2 , 4 , 5 , 6 ] ; let n = arr . length ; let miss = getMissingNo ( arr , n ) ; document . write ( miss ) ;
Find the Missing Number | a represents the array n : Number of elements in array a ; Driver Program
function getMissingNo ( a ) { let n = a . length ; let i , total = 1 ; for ( i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; } let arr = [ 1 , 2 , 3 , 5 ] ; document . write ( getMissingNo ( arr ) ) ;
Find the Missing Number | getMissingNo takes list as argument ; Driver Code
function getMissingNo ( a , n ) { let n_elements_sum = Math . floor ( n * ( n + 1 ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) sum += a [ i ] ; return n_elements_sum - sum ; } let a = [ 1 , 2 , 4 , 5 , 6 ] ; let n = a . length + 1 ; let miss = getMissingNo ( a , n ) document . write ( miss ) ;
Count number of occurrences ( or frequency ) in a sorted array | Returns number of times x occurs in arr [ 0. . n - 1 ] ; Driver code
function countOccurrences ( arr , n , x ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( x == arr [ i ] ) res ++ ; } return res ; } arr = [ 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 ] let n = arr . length ; let x = 2 ; document . write ( countOccurrences ( arr , n , x ) ) ;
Count number of occurrences ( or frequency ) in a sorted array | A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; Returns number of times x occurs in arr [ 0. . n - 1 ] ; If element is not present ; Count elements on left side . ; Count elements on right side . ; Driver code
function binarySearch ( arr , l , r , x ) { if ( r < l ) return - 1 ; var mid = l + parseInt ( ( r - l ) / 2 ) ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } function countOccurrences ( arr , n , x ) { var ind = binarySearch ( arr , 0 , n - 1 , x ) ; if ( ind == - 1 ) return 0 ; var count = 1 ; var left = ind - 1 ; while ( left >= 0 && arr [ left ] == x ) count ++ , left -- ; var right = ind + 1 ; while ( right < n && arr [ right ] == x ) count ++ , right ++ ; return count ; } var arr = [ 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 ] ; var n = arr . length ; var x = 2 ; document . write ( countOccurrences ( arr , n , x ) ) ;
Count 1 's in a sorted binary array | Returns counts of 1 's in arr[low..high]. The array is  assumed to be sorted in non-increasing order  ; get the middle index ; else recur for left side ; If element is not last 1 , recur for right side ; check if the element at middle index is last 1 ; Driver code
function countOnes ( arr , n ) { let ans ; let low = 0 , high = n - 1 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( arr [ mid ] < 1 ) high = mid - 1 ; else if ( arr [ mid ] > 1 ) low = mid + 1 ; else { if ( mid == n - 1 arr [ mid + 1 ] != 1 ) return mid + 1 ; else low = mid + 1 ; } } } let arr = [ 1 , 1 , 1 , 1 , 0 , 0 , 0 ] ; let n = arr . length ; document . write ( " " + countOnes ( arr , n ) ) ;
Find lost element from a duplicated array | Function to find missing element based on binary search approach . arr1 [ ] is of larger size and N is size of it . arr1 [ ] and arr2 [ ] are assumed to be in same order . ; special case , for only element which is missing in second array ; special case , for first element missing ; Initialize current corner points ; loop until lo < hi ; If element at mid indices are equal then go to right subarray ; if lo , hi becomes contiguous , break ; missing element will be at hi index of bigger array ; This function mainly does basic error checking and calls findMissingUtil ; Driver Code
function findMissingUtil ( arr1 , arr2 , N ) { if ( N == 1 ) return arr1 [ 0 ] ; if ( arr1 [ 0 ] != arr2 [ 0 ] ) return arr1 [ 0 ] ; let lo = 0 , hi = N - 1 ; while ( lo < hi ) { let mid = parseInt ( ( lo + hi ) / 2 , 10 ) ; if ( arr1 [ mid ] == arr2 [ mid ] ) lo = mid ; else hi = mid ; if ( lo == hi - 1 ) break ; } return arr1 [ hi ] ; } function findMissing ( arr1 , arr2 , M , N ) { if ( N == M - 1 ) document . write ( " " + findMissingUtil ( arr1 , arr2 , M ) + " " ) ; else if ( M == N - 1 ) document . write ( " " + findMissingUtil ( arr2 , arr1 , N ) + " " ) ; else document . write ( " " + " " ) ; } let arr1 = [ 1 , 4 , 5 , 7 , 9 ] ; let arr2 = [ 4 , 5 , 7 , 9 ] ; let M = arr1 . length ; let N = arr2 . length ; findMissing ( arr1 , arr2 , M , N ) ;
Find lost element from a duplicated array | This function mainly does XOR of all elements of arr1 [ ] and arr2 [ ] ; Do XOR of all element ; Driver Code
function findMissing ( arr1 , arr2 , M , N ) { if ( M != N - 1 && N != M - 1 ) { document . write ( " " ) ; return ; } let res = 0 ; for ( let i = 0 ; i < M ; i ++ ) res = res ^ arr1 [ i ] ; for ( let i = 0 ; i < N ; i ++ ) res = res ^ arr2 [ i ] ; document . write ( " " + res ) ; } let arr1 = [ 4 , 1 , 5 , 9 , 7 ] ; let arr2 = [ 7 , 5 , 9 , 4 ] ; let M = arr1 . length ; let N = arr2 . length ; findMissing ( arr1 , arr2 , M , N ) ;