codeparrot/xlcost-text-to-code huggingface dataset:
Find remainder when a number A raised to N factorial is divided by P | Function to calculate factorial of a Number ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Given Input ; Function Call
function fact ( n ) { let ans = 1 ; for ( let i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } function remainder ( n , a , p ) { let len = fact ( n ) ; let ans = 1 ; for ( let i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } let A = 2 , N = 1 , P = 2 ; document . write ( remainder ( N , A , P ) ) ;
Check if a number N can be expressed in base B | Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a boolean variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Given number N and base B ; Function call
function check ( n , w ) { var a = Array ( 105 ) . fill ( 0 ) ; var p = 0 ; while ( n > 0 ) { a [ p ++ ] = n % w ; n /= w ; n = parseInt ( n ) ; } var flag = true ; for ( i = 0 ; i <= 100 ; i ++ ) { if ( a [ i ] == 0 a [ i ] == 1 ) continue ; else if ( a [ i ] == w a [ i ] == w - 1 ) a [ i + 1 ] ++ ; else flag = false ; } return flag ; } var B = 3 , N = 7 ; if ( check ( N , B ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of groups among N people having only one leader in each group | Javascript program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Given N number of peoples ; Function Call
let mod = 1000000007 ; function exponentMod ( A , B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; let y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( ( y + mod ) % mod ) ; } function countWays ( N ) { let select = exponentMod ( 2 , N - 1 ) ; let ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; document . write ( ways ) ; } let N = 5 ; countWays ( N ) ;
Expected number of moves to reach the end of a board | Matrix Exponentiation | Javascript implementation of the approach ; Function to multiply two 7 * 7 matrix ; Function to perform matrix exponentiation ; 7 * 7 identity matrix ; Loop to find the power ; Function to return the required count ; Base cases ; Multiplier matrix ; Finding the required multiplier i . e mul ^ ( X - 6 ) ; Final answer ; Driver code
var maxSize = 50 ; function matrix_product ( a , b ) { var c = Array . from ( Array ( 7 ) , ( ) => Array ( 7 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < 7 ; i ++ ) for ( var j = 0 ; j < 7 ; j ++ ) for ( var k = 0 ; k < 7 ; k ++ ) c [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; return c ; } function mul_expo ( mul , p ) { var s = [ [ 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ] ; while ( p != 1 ) { if ( p % 2 == 1 ) s = matrix_product ( s , mul ) ; mul = matrix_product ( mul , mul ) ; p = parseInt ( p / 2 ) ; } return matrix_product ( mul , s ) ; } function expectedSteps ( x ) { if ( x == 0 ) return 0 ; if ( x <= 6 ) return 6 ; var mul = [ [ 7 / 6 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ - 1 / 6 , 0 , 0 , 0 , 0 , 0 , 0 ] ] ; mul = mul_expo ( mul , x - 6 ) ; return ( mul [ 0 ] [ 0 ] + mul [ 1 ] [ 0 ] + mul [ 2 ] [ 0 ] + mul [ 3 ] [ 0 ] + mul [ 4 ] [ 0 ] + mul [ 5 ] [ 0 ] ) * 6 ; } var n = 10 ; document . write ( expectedSteps ( n - 1 ) ) ;
Place the prisoners into cells to maximize the minimum difference between any two | Function that returns true if the prisoners can be placed such that the minimum distance between any two prisoners is at least sep ; Considering the first prisoner is placed at 1 st cell ; If the first prisoner is placed at the first cell then the last_prisoner_placed will be the first prisoner placed and that will be in cell [ 0 ] ; Checking if the prisoner can be placed at ith cell or not ; If all the prisoners got placed then return true ; Function to return the maximized distance ; Sort the array so that binary search can be applied on it ; Minimum possible distance for the search space ; Maximum possible distance for the search space ; To store the result ; Binary search ; If the prisoners can be placed such that the minimum distance between any two prisoners is at least mid ; Update the answer ; Driver code
function canPlace ( a , n , p , sep ) { var prisoners_placed = 1 ; var last_prisoner_placed = a [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { var current_cell = a [ i ] ; if ( current_cell - last_prisoner_placed >= sep ) { prisoners_placed ++ ; last_prisoner_placed = current_cell ; if ( prisoners_placed == p ) { return true ; } } } return false ; } function maxDistance ( cell , n , p ) { cell . sort ( ) ; var start = 0 ; var end = cell [ n - 1 ] - cell [ 0 ] ; var ans = 0 ; while ( start <= end ) { var mid = start + parseInt ( ( ( end - start ) / 2 ) ) ; if ( canPlace ( cell , n , p , mid ) ) { ans = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } var cell = [ 1 , 2 , 8 , 4 , 9 ] ; var n = cell . length ; var p = 3 ; document . write ( maxDistance ( cell , n , p ) ) ;
Find N in the given matrix that follows a pattern | Function to return the row and the column of the given integer ; Binary search for the row number ; Condition to get the maximum x that satisfies the criteria ; Binary search for the column number ; Condition to get the maximum y that satisfies the criteria ; Get the row and the column number ; Return the pair ; Driver code
function solve ( n ) { let low = 1 , high = 1e4 , x = n , p = 0 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = Math . floor ( ( mid * ( mid + 1 ) ) / 2 ) ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } let start = 1 , end = 1e4 , y = 1 , q = 0 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; let sum = Math . floor ( ( mid * ( mid + 1 ) ) / 2 ) ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - Math . floor ( ( p * ( p + 1 ) ) / 2 ) ; y = y + Math . floor ( ( q * ( q + 1 ) ) / 2 ) ; let r = x ; let c = q + 1 - n + y ; let ans = [ r , c ] ; return ans ; } let n = 5 ; let p = solve ( n ) ; document . write ( p [ 0 ] + " " + p [ 1 ] ) ;
Smallest subarray with GCD as 1 | Segment Tree | Javascript implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Building the segment tree ; Two pointer variables ; To store the final answer ; Looping ; Incrementing j till we don 't get  a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code
let maxLen = 30 ; let seg = new Array ( 3 * maxLen ) ; function build ( l , r , In , arr ) { if ( l == r ) return seg [ In ] = arr [ l ] ; let mid = Math . floor ( ( l + r ) / 2 ) ; return seg [ In ] = __gcd ( build ( l , mid , 2 * In + 1 , arr ) , build ( mid + 1 , r , 2 * In + 2 , arr ) ) ; } function query ( l , r , l1 , r1 , In ) { if ( l1 <= l && r <= r1 ) return seg [ In ] ; if ( l > r1 r < l1 ) return 0 ; let mid = Math . floor ( ( l + r ) / 2 ) ; return __gcd ( query ( l , mid , l1 , r1 , 2 * In + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * In + 2 ) ) ; } function findLen ( arr , n ) { build ( 0 , n - 1 , 0 , arr ) ; let i = 0 , j = 0 ; let ans = Number . MAX_VALUE ; while ( i < n ) { while ( j < n && query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = Math . min ( ( j - i + 1 ) , ans ) ; i ++ ; j = Math . max ( j , i ) ; } if ( ans == Number . MAX_VALUE ) return - 1 ; else return ans ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let arr = [ 2 , 2 , 2 ] ; let n = arr . length ; document . write ( findLen ( arr , n ) ) ;
Find an N x N grid whose xor of every row and column is equal | Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code
function findGrid ( n ) { let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = new Array ( n ) ; let x = 0 ; for ( let i = 0 ; i < parseInt ( n / 4 ) ; i ++ ) { for ( let j = 0 ; j < parseInt ( n / 4 ) ; j ++ ) { for ( let k = 0 ; k < 4 ; k ++ ) { for ( let l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k ] [ j * 4 + l ] = x ; x ++ ; } } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( arr [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let n = 4 ; findGrid ( n ) ;
Cartesian tree from inorder traversal | Segment Tree | Javascript implementation of the approach ; Node of the BST ; Array to store segment tree ; Function to create segment - tree to answer range - max query ; Base case ; Maximum index in left range ; Maximum index in right range ; If value at l1 > r1 ; Else ; Returning the maximum in range ; Function to answer range max query ; Base cases ; Maximum in left range ; Maximum in right range ; l1 = - 1 means left range was out - side required range ; Returning the maximum among two ranges ; Function to print the inorder traversal of the binary tree ; Base case ; Traversing the left sub - tree ; Printing current node ; Traversing the right sub - tree ; Function to build cartesian tree ; Base case ; Maximum in the range ; Creating current node ; Creating left sub - tree ; Creating right sub - tree ; Returning current node ; In - order traversal of cartesian tree ; Size of the array ; Building the segment tree ; Building && printing cartesian tree
var maxLen = 30 ; class node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } ; var segtree = Array ( maxLen * 4 ) . fill ( 0 ) ; function buildTree ( l , r , i , arr ) { if ( l == r ) { segtree [ i ] = l ; return l ; } var l1 = buildTree ( l , parseInt ( ( l + r ) / 2 ) , 2 * i + 1 , arr ) ; var r1 = buildTree ( parseInt ( ( l + r ) / 2 ) + 1 , r , 2 * i + 2 , arr ) ; if ( arr [ l1 ] > arr [ r1 ] ) segtree [ i ] = l1 ; else segtree [ i ] = r1 ; return segtree [ i ] ; } function rangeMax ( l , r , rl , rr , i , arr ) { if ( r < rl l > rr ) return - 1 ; if ( l >= rl && r <= rr ) return segtree [ i ] ; var l1 = rangeMax ( l , parseInt ( ( l + r ) / 2 ) , rl , rr , 2 * i + 1 , arr ) ; var r1 = rangeMax ( parseInt ( ( l + r ) / 2 ) + 1 , r , rl , rr , 2 * i + 2 , arr ) ; if ( l1 == - 1 ) return r1 ; if ( r1 == - 1 ) return l1 ; if ( arr [ l1 ] > arr [ r1 ] ) return l1 ; else return r1 ; } function inorder ( curr ) { if ( curr == null ) return ; inorder ( curr . left ) ; document . write ( curr . data + " " ) ; inorder ( curr . right ) ; } function createCartesianTree ( l , r , arr , n ) { if ( r < l ) return null ; var m = rangeMax ( 0 , n - 1 , l , r , 0 , arr ) ; var curr = new node ( arr [ m ] ) ; curr . left = createCartesianTree ( l , m - 1 , arr , n ) ; curr . right = createCartesianTree ( m + 1 , r , arr , n ) ; return curr ; } var arr = [ 8 , 11 , 21 , 100 , 5 , 70 , 55 ] ; var n = arr . length ; buildTree ( 0 , n - 1 , 0 , arr ) ; inorder ( createCartesianTree ( 0 , n - 1 , arr , n ) ) ;
Kth smallest element in the array using constant space when array can 't be modified | Function to return the kth smallest element from the array ; Minimum and maximum element from the array ; Modified binary search ; To store the count of elements from the array which are less than mid and the elements which are equal to mid ; If mid is the kth smallest ; If the required element is less than mid ; If the required element is greater than mid ; Driver code
function kthSmallest ( arr , k , n ) { let temp = [ ... arr ] ; let low = temp . sort ( ( a , b ) => a - b ) [ 0 ] ; let high = temp [ temp . length - 1 ] ; while ( low <= high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; let countless = 0 , countequal = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( arr [ i ] < mid ) ++ countless ; else if ( arr [ i ] == mid ) ++ countequal ; } if ( countless < k && ( countless + countequal ) >= k ) { return mid ; } else if ( countless >= k ) { high = mid - 1 ; } else if ( countless < k && countless + countequal < k ) { low = mid + 1 ; } } } let arr = [ 7 , 10 , 4 , 3 , 20 , 15 ] ; let n = arr . length ; let k = 3 ; document . write ( kthSmallest ( arr , k , n ) ) ;
Find 2 ^ ( 2 ^ A ) % B | Function to return 2 ^ ( 2 ^ A ) % B ; Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ A ) % B
function F ( A , B ) { if ( A == 1 ) return ( 4 % B ) ; else { var temp = F ( A - 1 , B ) ; return ( temp * temp ) % B ; } } var A = 25 , B = 50 ; document . write ( F ( A , B ) ) ;
Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | Javascript implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
MOD = 1000000007 function rangeSum ( l , r ) { var a = 1 , b = 9 , res = 0 ; for ( var i = 1 ; i <= 10 ; i ++ ) { var L = Math . max ( l , a ) ; var R = Math . min ( r , b ) ; if ( L <= R ) { var sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } var l = 98 , r = 102 ; document . write ( rangeSum ( l , r ) ) ;
Minimum number N such that total set bits of all numbers from 1 to N is at | Javascript implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code
const INF = 99999 ; const size = 10 ; function getSetBitsFromOneToN ( N ) { let two = 2 , ans = 0 ; let n = N ; while ( n ) { ans += parseInt ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } function findMinimum ( x ) { let low = 0 , high = 100000 ; let ans = high ; while ( low <= high ) { let mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = Math . min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } let x = 20 ; document . write ( findMinimum ( x ) ) ;
Numbers whose factorials end with n zeros | Function to calculate trailing zeros ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code
function trailingZeroes ( n ) { var cnt = 0 ; while ( n > 0 ) { n = parseInt ( n / 5 ) ; cnt += n ; } return cnt ; } function binarySearch ( n ) { var low = 0 ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } var result = [ ] ; while ( trailingZeroes ( low ) == n ) { result . push ( low ) ; low ++ ; } for ( var i = 0 ; i < result . length ; i ++ ) document . write ( result [ i ] + " " ) ; } var n = 2 ; binarySearch ( n ) ;
Number of days after which tank will become empty | Utility method to get sum of first n numbers ; Method returns minimum number of days after which tank will become empty ; If water filling is more than capacity then after C days only tank will become empty ; Initialize binary search variable ; Loop until low is less than high ; If cumulate sum is greater than ( C - l ) then search on left side ; If ( C - l ) is more then search on right side ; Final answer will be obtained by adding l to binary search result ; Driver code
function getCumulateSum ( n ) { return parseInt ( ( n * ( n + 1 ) ) / 2 , 10 ) ; } function minDaysToEmpty ( C , l ) { if ( C <= l ) return C ; let lo = 0 ; let hi = 1e4 ; let mid ; while ( lo < hi ) { mid = parseInt ( ( lo + hi ) / 2 , 10 ) ; if ( getCumulateSum ( mid ) >= ( C - l ) ) hi = mid ; else lo = mid + 1 ; } return ( l + lo ) ; } let C = 5 ; let l = 2 ; document . write ( minDaysToEmpty ( C , l ) ) ;
Number of days after which tank will become empty | Method returns minimum number of days after which tank will become empty ; Driver code
function minDaysToEmpty ( C , l ) { if ( l >= C ) return C ; let eq_root = ( Math . sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 ; return ( Math . ceil ( eq_root ) + l ) ; } document . write ( minDaysToEmpty ( 5 , 2 ) + " " ) ; document . write ( minDaysToEmpty ( 6514683 , 4965 ) ) ;
K | JavaScript Program to find kth element from two sorted arrays ; Driver Code
function kth ( arr1 , arr2 , m , n , k ) { var sorted1 = Array ( m + n ) . fill ( 0 ) ; var i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } var arr1 = [ 2 , 3 , 6 , 7 , 9 ] ; var arr2 = [ 1 , 4 , 8 , 10 ] ; var k = 5 ; document . write ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ;
Search element in a sorted matrix | Javascript implementation to search an element in a sorted matrix ; This function does Binary search for x in i - th row . It does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; Element found ; Element not found ; Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; Single row matrix ; Do binary search in middle column . Condition to terminate the loop when the 2 desired rows are found ; Element found ; If element is present on the mid of the two rows ; Search element on 1 st half of 1 st row ; Search element on 2 nd half of 1 st row ; Search element on 1 st half of 2 nd row ; search element on 2 nd half of 2 nd row ; Driver code
let MAX = 100 ; function binarySearch ( mat , i , j_low , j_high , x ) { while ( j_low <= j_high ) { let j_mid = Math . floor ( ( j_low + j_high ) / 2 ) ; if ( mat [ i ] [ j_mid ] == x ) { document . write ( " " + i + " " + j_mid + " " ) ; return ; } else if ( mat [ i ] [ j_mid ] > x ) j_high = j_mid - 1 ; else j_low = j_mid + 1 ; } document . write ( " " ) ; } function sortedMatrixSearch ( mat , n , m , x ) { if ( n == 1 ) { binarySearch ( mat , 0 , 0 , m - 1 , x ) ; return ; } let i_low = 0 ; let i_high = n - 1 ; let j_mid = Math . floor ( m / 2 ) ; while ( ( i_low + 1 ) < i_high ) { let i_mid = Math . floor ( ( i_low + i_high ) / 2 ) ; if ( mat [ i_mid ] [ j_mid ] == x ) { document . write ( " " + i_mid + " " + j_mid + " " ) ; return ; } else if ( mat [ i_mid ] [ j_mid ] > x ) i_high = i_mid ; else i_low = i_mid ; } if ( mat [ i_low ] [ j_mid ] == x ) document . write ( " " + i_low + " " + j_mid + " " ) ; else if ( mat [ i_low + 1 ] [ j_mid ] == x ) document . write ( " " + ( i_low + 1 ) + " " + j_mid + " " ) ; else if ( x <= mat [ i_low ] [ j_mid - 1 ] ) binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) ; else if ( x >= mat [ i_low ] [ j_mid + 1 ] && x <= mat [ i_low ] [ m - 1 ] ) binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) ; else if ( x <= mat [ i_low + 1 ] [ j_mid - 1 ] ) binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) ; else binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) ; } let n = 4 , m = 5 , x = 8 ; let mat = [ [ 0 , 6 , 8 , 9 , 11 ] , [ 20 , 22 , 28 , 29 , 31 ] , [ 36 , 38 , 50 , 61 , 63 ] , [ 64 , 66 , 100 , 122 , 128 ] ] ; sortedMatrixSearch ( mat , n , m , x ) ;
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | JavaScript program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix . ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driver Code
let R = 2 ; let C = 2 ; function bsearch ( low , high , n , arr ) { let mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } function mindiff ( arr , n , m ) { for ( let i = 0 ; i < n ; i ++ ) arr . sort ( ) ; let ans = + 2147483647 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) ; ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) ; if ( p - 1 >= 0 ) ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) ; } } return ans ; } let m = [ [ 8 , 5 ] , [ 6 , 8 ] ] ; document . write ( mindiff ( m , R , C ) ) ;
Find bitonic point in given bitonic sequence | Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program
function binarySearch ( arr , left , right ) { if ( left <= right ) { var mid = parseInt ( ( left + right ) / 2 ) ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return - 1 ; } var arr = [ 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 ] ; var n = arr . length ; var index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != - 1 ) document . write ( arr [ index ] ) ;
Find the only repeating element in a sorted array of size n | Returns index of second appearance of a repeating element The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver code
function findRepeatingElement ( arr , low , high ) { if ( low > high ) return - 1 ; var mid = parseInt ( ( low + high ) / 2 ) ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } var arr = [ 1 , 2 , 3 , 3 , 4 , 5 ] ; var n = arr . length ; var index = findRepeatingElement ( arr , 0 , n - 1 ) ; if ( index != - 1 ) document . write ( arr [ index ] ) ;
Find cubic root of a number | Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver Code
function diff ( n , mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } function cubicRoot ( n ) { let start = 0 , end = n ; let e = 0.0000001 ; while ( true ) { let mid = ( start + end ) / 2 ; let error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } let n = 3 ; document . write ( " " + n + " " + cubicRoot ( n ) ) ;