codeparrot/xlcost-text-to-code huggingface dataset:
Search in a sorted 2D matrix ( Stored in row major order ) | Javascript program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code
var M = 3 ; var N = 4 ; function binarySearch1D ( arr , K ) { var low = 0 ; var high = N - 1 ; while ( low <= high ) { var mid = low + parseInt ( ( high - low ) / 2 ) ; if ( arr [ mid ] == K ) return true ; if ( arr [ mid ] < K ) low = mid + 1 ; else high = mid - 1 ; } return false ; } function searchMatrix ( matrix , K ) { var low = 0 ; var high = M - 1 ; while ( low <= high ) { var mid = low + parseInt ( ( high - low ) / 2 ) ; if ( K >= matrix [ mid ] [ 0 ] && K <= matrix [ mid ] [ N - 1 ] ) return binarySearch1D ( matrix [ mid ] , K ) ; if ( K < matrix [ mid ] [ 0 ] ) high = mid - 1 ; else low = mid + 1 ; } return false ; } var matrix = [ [ 1 , 3 , 5 , 7 ] , [ 10 , 11 , 16 , 20 ] , [ 23 , 30 , 34 , 50 ] ] ; var K = 3 ; if ( searchMatrix ( matrix , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find Nth term divisible by a or b | Function to return gcd of a and b ; Function to calculate how many numbers from 1 to num are divisible by a or b ; calculate number of terms divisible by a and by b then , remove the terms which is are divisible by both a and b ; Binary search to find the nth term divisible by a or b ; set low to 1 and high to max ( a , b ) * n , here we have taken high as 10 ^ 18 ; if the current term is less than n then we need to increase low to mid + 1 ; if current term is greater than equal to n then high = mid ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function divTermCount ( a , b , lcm , num ) { return parseInt ( num / a ) + parseInt ( num / b ) - parseInt ( num / lcm ) ; } function findNthTerm ( a , b , n ) { var low = 1 , high = Number . MAX_VALUE , mid ; var lcm = parseInt ( ( a * b ) / gcd ( a , b ) ) ; while ( low < high ) { mid = low + parseInt ( ( high - low ) / 2 ) ; if ( divTermCount ( a , b , lcm , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } var a = 2 , b = 5 , n = 10 ; document . write ( findNthTerm ( a , b , n ) ) ;
Replace all occurrences of pi with 3.14 in a given string | Function to replace all occurrences of pi in a given with 3.14 ; Iterate through second last element of the string ; If current and current + 1 alphabets form the word ' pi ' append 3.14 to output ; Append the current letter ; Return the output string ; Driver Code
function replacePi ( input ) { var output = " " ; var size = input . length ; for ( var i = 0 ; i < size ; ++ i ) { if ( i + 1 < size && input [ i ] === " " && input [ i + 1 ] === " " ) { output += " " ; i ++ ; } else { output += input [ i ] ; } } return output ; } var input = " " ; document . write ( replacePi ( input ) ) ;
Number of elements that can be seen from right side | javascript program to find number of elements that can be seen from right side . ; Driver code
function numberOfElements ( height , n ) { var max_so_far = 0 ; var coun = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] > max_so_far ) { max_so_far = height [ i ] ; coun ++ ; } } return coun ; } var n = 6 ; var height = [ 4 , 8 , 2 , 0 , 0 , 5 ] ; document . write ( numberOfElements ( height , n ) ) ;
Check if a pair with given product exists in Linked list | JavaScript code to find the pair with given product ; Link list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Function to check if pair with given product exists in the list Takes head pointer of the linked list and product ; Check if pair exits ; Start with the empty list ; Use push ( ) to construct linked list ; function to print the result
var MAX = 100000 ; 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 check_pair_product ( head , prod ) { var s = [ ] ; var p = head ; while ( p != null ) { var curr = p . data ; if ( prod % curr == 0 && s . includes ( prod / curr ) ) { document . write ( curr + " " + prod / curr ) ; return true ; } s . push ( p . data ) ; p = p . next ; } return false ; } var head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; head = push ( head , 12 ) ; head = push ( head , 1 ) ; head = push ( head , 18 ) ; head = push ( head , 47 ) ; head = push ( head , 16 ) ; head = push ( head , 12 ) ; head = push ( head , 14 ) ; var res = check_pair_product ( head , 24 ) ; if ( res == false ) document . write ( " " ) ;
Largest element in the array that is repeated exactly k times | Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times
function solve ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) if ( k == 1 && arr [ n - 2 ] != arr [ n - 1 ] ) { cout << arr [ n - 1 ] << endl ; return ; } var count = 1 ; for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) count ++ ; else count = 1 ; if ( count == k && ( i == 0 || ( arr [ i - 1 ] != arr [ i ] ) ) ) { document . write ( arr [ i ] + " " ) ; return ; } } document . write ( " " ) ; } var arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] ; var k = 2 ; var n = arr . length ; solve ( arr , n , k ) ;
Largest element in the array that is repeated exactly k times | Function that finds the largest element that occurs exactly ' k ' times ; store the frequency of each element ; to store the maximum element ; if current element has frequency ' k ' and current maximum hasn 't been set ; set the current maximum ; if current element has frequency ' k ' and it is greater than the current maximum ; change the current maximum ; if there is no element with frequency ' k ' ; print the largest element with frequency ' k ' ; Driver code ; find the largest element that is repeated K times
function solve ( arr , n , k ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } var max = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . get ( arr [ i ] ) == k && max == - 1000000000 ) { max = arr [ i ] ; } else if ( m . get ( arr [ i ] ) == k && max < arr [ i ] ) { max = arr [ i ] ; } } if ( max == - 1000000000 ) document . write ( " " ) ; else document . write ( max ) ; } var arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] ; var k = 4 ; var n = arr . length ; solve ( arr , n , k ) ;
Sum and Product of minimum and maximum element of an Array | Function to find minimum element ; Function to find maximum element ; Function to get Sum ; Function to get product ; Driver Code ; Sum of min and max element ; Product of min and max element
function getMin ( arr , n ) { let res = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) res = Math . min ( res , arr [ i ] ) ; return res ; } function getMax ( arr , n ) { let res = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) res = Math . max ( res , arr [ i ] ) ; return res ; } function findSum ( arr , n ) { let min = getMin ( arr , n ) ; let max = getMax ( arr , n ) ; return min + max ; } function findProduct ( arr , n ) { let min = getMin ( arr , n ) ; let max = getMax ( arr , n ) ; return min * max ; } let arr = [ 12 , 1234 , 45 , 67 , 1 ] ; let n = arr . length ; document . write ( " " + findSum ( arr , n ) + " " ) ; document . write ( " " + findProduct ( arr , n ) + " " ) ;
Count the number of pop operations on stack to get each element of the array | Function to find the count ; Hashmap to store all the elements which are popped once . ; Check if the number is present in the hashmap Or in other words been popped out from the stack before . ; Keep popping the elements while top is not equal to num ; Pop the top ie . equal to num ; Print the number of elements popped . ; Driver code
function countEle ( s , a , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; ++ i ) { var num = a [ i ] ; if ( mp . has ( num ) ) document . write ( " " ) ; else { var cnt = 0 ; while ( s [ s . length - 1 ] != num ) { mp . set ( s [ s . length - 1 ] , true ) ; s . pop ( ) ; cnt ++ ; } s . pop ( ) ; cnt ++ ; document . write ( cnt + " " ) ; } } } var N = 5 ; var s = [ ] ; s . push ( 1 ) ; s . push ( 2 ) ; s . push ( 3 ) ; s . push ( 4 ) ; s . push ( 6 ) ; var a = [ 6 , 3 , 4 , 1 , 2 ] ; countEle ( s , a , N ) ;
Count characters with same neighbors | Function to count the characters with same adjacent characters ; if length is less than 3 then return length as there will be only two characters ; Traverse the string ; Increment the count if the previous and next character is same ; Return count ; Driver code
function countChar ( str ) { var n = str . length ; if ( n <= 2 ) return n ; var count = 2 ; for ( var i = 1 ; i < n - 1 ; i ++ ) if ( str [ i - 1 ] == str [ i + 1 ] ) count ++ ; return count ; } var str = " " ; document . write ( countChar ( str ) ) ;
Length of largest sub | Javascript implementation of above approach ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the length of longest subarray having count of primes more than count of non - primes ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider - 1 as non primes and 1 as primes ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver code
let prime = new Array ( 1000000 + 5 ) ; function findPrime ( ) { prime . fill ( true ) prime [ 1 ] = false ; for ( let p = 2 ; p * p <= 1000000 ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= 1000000 ; i += p ) prime [ i ] = false ; } } } function lenOfLongSubarr ( arr , n ) { let um = new Map ( ) ; let sum = 0 , maxLen = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += prime [ arr [ i ] ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . has ( sum ) ) um . set ( sum , i ) ; if ( um . has ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } findPrime ( ) ; let arr = [ 1 , 9 , 3 , 4 , 5 , 6 , 7 , 8 ] ; let n = arr . length ; document . write ( lenOfLongSubarr ( arr , n ) + " " )
First strictly greater element in a sorted array in Java | Javascript program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code
function next ( arr , target ) { let start = 0 , end = arr . length - 1 ; let ans = - 1 ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; if ( arr [ mid ] <= target ) { start = mid + 1 ; } else { ans = mid ; end = mid - 1 ; } } return ans ; } let arr = [ 1 , 2 , 3 , 5 , 8 , 12 ] ; document . write ( next ( arr , 8 ) ) ;
Reallocation of elements based on Locality of Reference | A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code
function search ( arr , n , x ) { let res = - 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res = i ; if ( res == - 1 ) return false ; let temp = arr [ res ] ; for ( let i = res ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = temp ; return true ; } let arr = [ 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 ] ; let q = [ 63 , 63 , 86 , 63 , 78 ] ; let n = arr . length ; let m = q . length ; for ( let i = 0 ; i < m ; i ++ ) search ( arr , n , q [ i ] ) ? document . write ( " " ) : document . write ( " " ) ;
Probability of a key K present in array | Function to find the probability ; find probability ; Driver Code
function kPresentProbability ( a , n , k ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( a [ i ] == k ) count += 1 ; return count / n ; } let A = [ 4 , 7 , 2 , 0 , 8 , 7 , 5 ] ; let K = 3 ; let N = A . length ; document . write ( kPresentProbability ( A , N , K ) ) ;
Find the slope of the given number | function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver code
function slopeOfNum ( num , n ) { let slope = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( num [ i ] > num [ i - 1 ] && num [ i ] > num [ i + 1 ] ) slope ++ ; else if ( num [ i ] < num [ i - 1 ] && num [ i ] < num [ i + 1 ] ) slope ++ ; } return slope ; } let num = " " ; let n = num . length ; document . write ( " " + slopeOfNum ( num , n ) ) ;
Sudo Placement | Beautiful Pairs | The function to check if beautiful pair exists ; Set for hashing ; Traversing the first array ; Traversing the second array to check for every j corresponding to single i ; x + y = z = > x = y - z ; if such x exists then we return true ; hash to make use of it next time ; no pair exists ; Driver Code ; If pair exists then 1 else 0 2 nd argument as size of first array fourth argument as sizeof 2 nd array
function pairExists ( arr1 , m , arr2 , n ) { let s = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( s . has ( arr2 [ j ] - arr1 [ i ] ) ) return true ; } s . add ( arr1 [ i ] ) ; } return false ; } let arr1 = [ 1 , 5 , 10 , 8 ] ; let arr2 = [ 2 , 20 , 13 ] ; if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) document . write ( 1 + " " ) ; else document . write ( 0 + " " ) ;
Sudo Placement | Placement Tour | This function returns true if the value cumulative according to received integer K is less than budget B , otherwise returns false ; Initialize a temporary array which stores the cumulative value of the original array ; Sort the array to find the smallest K values ; Check if the value is less than budget ; This function prints the optimal number of elements and respective cumulative value which is less than the given number ; Initialize answer as zero as optimal value may not exists ; If the current Mid Value is an optimal value , then try to maximize it ; Call Again to set the corresponding cumulative value for the optimal ans ; Driver Code ; Budget
let cumulativeValue = 0 ; function canBeOptimalValue ( K , arr , N , B ) { let tmp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) tmp [ i ] = ( arr [ i ] + K * ( i + 1 ) ) ; tmp . sort ( ( a , b ) => a - b ) ; cumulativeValue = 0 ; for ( let i = 0 ; i < K ; i ++ ) cumulativeValue += tmp [ i ] ; return cumulativeValue <= B ; } function findNoOfElementsandValue ( arr , N , B ) { let ans = 0 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( canBeOptimalValue ( mid , arr , N , B ) ) { ans = mid ; start = mid + 1 ; } else end = mid - 1 ; } canBeOptimalValue ( ans , arr , N , B , cumulativeValue ) ; document . write ( ans + " " + cumulativeValue + " " ) ; } let arr = [ 1 , 2 , 5 , 6 , 3 ] ; let N = arr . length ; let B = 90 ; findNoOfElementsandValue ( arr , N , B ) ;
Previous greater element | Javascript program previous greater element A naive solution to print previous greater element for every element in an array . ; Previous greater for first element never exists , so we print - 1. ; Let us process remaining elements . ; Find first element on left side that is greater than arr [ i ] . ; If all elements on left are smaller . ; Driver Code
function prevGreater ( arr , n ) { document . write ( " " ) ; for ( let i = 1 ; i < n ; i ++ ) { let j ; for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] < arr [ j ] ) { document . write ( arr [ j ] + " " ) ; break ; } } if ( j == - 1 ) document . write ( " " ) ; } } let arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ] ; let n = arr . length ; prevGreater ( arr , n ) ;
Previous greater element | Javascript program previous greater element An efficient solution to print previous greater element for every element in an array . ; Create a stack and push index of first element to it ; Previous greater for first element is always - 1. ; Traverse remaining elements ; Pop elements from stack while stack is not empty and top of stack is smaller than arr [ i ] . We always have elements in decreasing order in a stack . ; If stack becomes empty , then no element is greater on left side . Else top of stack is previous greater . ; Driver Code
function prevGreater ( arr , n ) { let s = [ ] ; s . push ( arr [ 0 ] ) ; document . write ( " " ) ; for ( let i = 1 ; i < n ; i ++ ) { while ( s . length != 0 && s [ s . length - 1 ] < arr [ i ] ) s . pop ( ) ; if ( s . length == 0 ) document . write ( " " ) ; else document . write ( s [ s . length - 1 ] + " " ) ; s . push ( arr [ i ] ) ; } } let arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ] ; let n = arr . length ; prevGreater ( arr , n ) ;
Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Function
function printDuplicates ( arr , n ) { let i ; let fl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ arr [ i ] % n ] >= n ) { if ( arr [ arr [ i ] % n ] < 2 * n ) { document . write ( arr [ i ] % n + " " ) ; fl = 1 ; } } arr [ arr [ i ] % n ] += n ; } if ( ! fl ) document . write ( " " ) ; } let arr = [ 1 , 6 , 3 , 1 , 3 , 6 , 6 ] ; let arr_size = arr . length ; printDuplicates ( arr , arr_size ) ;
Find the smallest positive number missing from an unsorted array | Set 2 | Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code
function findMissingNo ( arr , n ) { var val ; var nextval ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= 0 arr [ i ] > n ) continue ; val = arr [ i ] ; while ( arr [ val - 1 ] != val ) { nextval = arr [ val - 1 ] ; arr [ val - 1 ] = val ; val = nextval ; if ( val <= 0 val > n ) break ; } } for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } var arr = [ 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 ] ; var arr_size = arr . length ; var missing = findMissingNo ( arr , arr_size ) ; document . write ( " " + missing ) ;
Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code
function findTriplets ( arr , n , sum ) { 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 ) { document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ k ] + " " ) ; } } } } } let arr = [ 0 , - 1 , 2 , - 3 , 1 ] ; let n = arr . length ; findTriplets ( arr , n , - 2 ) ;
Print all triplets with given sum | function to print triplets with given sum ; Find all pairs with sum equals to " sum - arr [ i ] " ; Driver code
function findTriplets ( arr , n , sum ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { let s = new Set ( ) ; for ( let j = i + 1 ; j < n ; j ++ ) { let x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . has ( x ) ) document . write ( x + " " + arr [ i ] + " " + arr [ j ] + " " ) ; else s . add ( arr [ j ] ) ; } } } let arr = [ 0 , - 1 , 2 , - 3 , 1 ] ; let sum = - 2 ; let n = arr . length ; findTriplets ( 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 ; will contain max product ; Driver Code
function maxProduct ( arr , n ) { if ( n < 4 ) return - 1 ; let max_product = Number . MIN_VALUE ; for ( let i = 0 ; i < n - 3 ; i ++ ) for ( let j = i + 1 ; j < n - 2 ; j ++ ) for ( let k = j + 1 ; k < n - 1 ; k ++ ) for ( let l = k + 1 ; l < n ; l ++ ) max_product = Math . max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] * arr [ l ] ) ; return max_product ; } let arr = [ 10 , 3 , 5 , 6 , 20 ] ; let n = arr . length ; let max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
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 ; Sort the array in ascending order ; Return the maximum of x , y and z ; Driver Code
function maxProduct ( arr , n ) { if ( n < 4 ) return - 1 ; arr . sort ( function ( a , b ) { return a - b ; } ) ; let x = arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] * arr [ n - 4 ] ; let y = arr [ 0 ] * arr [ 1 ] * arr [ 2 ] * arr [ 3 ] ; let z = arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] * arr [ n - 2 ] ; return Math . max ( x , Math . max ( y , z ) ) ; } let arr = [ - 10 , - 3 , 5 , 6 , - 20 ] ; let n = arr . length ; let max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
Minimum value of " max ▁ + ▁ min " in a subarray | java program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code
function maxSum ( arr , n ) { if ( n < 2 ) return - 1 ; let ans = arr [ 0 ] + arr [ 1 ] ; for ( let i = 1 ; i + 1 < n ; i ++ ) ans = Math . min ( ans , ( arr [ i ] + arr [ i + 1 ] ) ) ; return ans ; } let arr = [ 1 , 12 , 2 , 2 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;
Stella Octangula Number | Returns value of n * ( 2 * n * n - 1 ) ; Finds if a value of f ( n ) is equal to x where n is in interval [ low . . high ] ; Returns true if x is Stella Octangula Number . Else returns false . ; Find ' high ' for binary search by repeated doubling ; If condition is satisfied for a power of 2. ; Call binary search ; Driver code
function f ( n ) { return n * ( 2 * n * n - 1 ) ; } function binarySearch ( low , high , x ) { while ( low <= high ) { let mid = ( low + high ) / 2 ; if ( f ( mid ) < x ) low = mid + 1 ; else if ( f ( mid ) > x ) high = mid - 1 ; else return true ; } return false ; } function isStellaOctangula ( x ) { if ( x == 0 ) return true ; let i = 1 ; while ( f ( i ) < x ) i = i * 2 ; if ( f ( i ) == x ) return true ; return binarySearch ( i / 2 , i , x ) ; } let n = 51 ; if ( isStellaOctangula ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;