codeparrot/xlcost-text-to-code huggingface dataset:
Min difference between maximum and minimum element in all Y size subarrays | Function to get the maximum of all the subarrays of size Y ; ith index of maxarr array will be the index upto which Arr [ i ] is maximum ; Stack is used to find the next larger element and keeps track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submax ; Function to get the minimum for all subarrays of size Y ; ith index of minarr array will be the index upto which Arr [ i ] is minimum ; Stack is used to find the next smaller element and keeping track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submin ; Function to get minimum difference ; Create submin and submax arrays ; Store initial difference ; Calculate temporary difference ; Final minimum difference ; Driver Code Given array arr [ ] ; Given array arr [ ] ; Given subarray size ; Function Call
function get_submaxarr ( arr , n , y ) { var j = 0 ; var stk = [ ] ; var maxarr = Array ( n ) ; stk . push ( 0 ) ; for ( var i = 1 ; i < n ; i ++ ) { while ( stk . length != 0 && arr [ i ] > arr [ stk [ stk . length - 1 ] ] ) { maxarr [ stk [ stk . length - 1 ] ] = i - 1 ; stk . pop ( ) ; } stk . push ( i ) ; } while ( stk . length != 0 ) { maxarr [ stk [ stk . length - 1 ] ] = n - 1 ; stk . pop ( ) ; } var submax = [ ] ; for ( var i = 0 ; i <= n - y ; i ++ ) { while ( maxarr [ j ] < i + y - 1 j < i ) { j ++ ; } submax . push ( arr [ j ] ) ; } return submax ; } function get_subminarr ( arr , n , y ) { var j = 0 ; var stk = [ ] ; var minarr = Array ( n ) ; stk . push ( 0 ) ; for ( var i = 1 ; i < n ; i ++ ) { while ( stk . length != 0 && arr [ i ] < arr [ stk [ stk . length - 1 ] ] ) { minarr [ stk [ stk . length - 1 ] ] = i ; stk . pop ( ) ; } stk . push ( i ) ; } while ( stk . length != 0 ) { minarr [ stk [ stk . length - 1 ] ] = n ; stk . pop ( ) ; } var submin = [ ] ; for ( var i = 0 ; i <= n - y ; i ++ ) { while ( minarr [ j ] <= i + y - 1 j < i ) { j ++ ; } submin . push ( arr [ j ] ) ; } return submin ; } function getMinDifference ( Arr , N , Y ) { var submin = get_subminarr ( Arr , N , Y ) ; var submax = get_submaxarr ( Arr , N , Y ) ; var minn = submax [ 0 ] - submin [ 0 ] ; var b = submax . length ; for ( var i = 1 ; i < b ; i ++ ) { var dif = submax [ i ] - submin [ i ] ; minn = Math . min ( minn , dif ) ; } document . write ( minn + " " ) ; } var arr = [ 1 , 2 , 3 , 3 , 2 , 2 ] ; var N = arr . length var arr = [ 1 , 2 , 3 , 3 , 2 , 2 ] ; var N = arr . length var Y = 4 ; getMinDifference ( arr , N , Y ) ;
Least root of given quadratic equation for value greater than equal to K | Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Given coefficients of Equations ; Find minimum value of x
function func ( A , B , C , x ) { return ( A * x * x + B * x + C ) ; } function findMinx ( A , B , C , K ) { var start = 1 ; var end = parseInt ( Math . ceil ( Math . sqrt ( K ) ) ) ; while ( start <= end ) { var mid = start + parseInt ( ( end - start ) / 2 ) ; var x = func ( A , B , C , mid ) ; var Y = func ( A , B , C , mid - 1 ) ; if ( x >= K && Y < K ) { return mid ; } else if ( x < K ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } var A = 3 , B = 4 , C = 5 , K = 6 ; document . write ( findMinx ( A , B , C , K ) ) ;
Find the node at the center of an N | To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver code
let tree ; let path ; let maxHeight , maxHeightNode ; function getDiameterPath ( vertex , targetVertex , parent , path ) { if ( vertex == targetVertex ) { path . push ( vertex ) ; return true ; } for ( let i = 0 ; i < tree . get ( vertex ) . length ; i ++ ) { if ( tree . get ( vertex ) [ i ] == parent ) continue ; if ( getDiameterPath ( tree . get ( vertex ) [ i ] , targetVertex , vertex , path ) ) { path . push ( vertex ) ; return true ; } } return false ; } function farthestNode ( vertex , parent , height ) { if ( height > maxHeight ) { maxHeight = height ; maxHeightNode = vertex ; } if ( tree . get ( vertex ) != null ) for ( let i = 0 ; i < tree . get ( vertex ) . length ; i ++ ) { if ( tree . get ( vertex ) [ i ] == parent ) continue ; farthestNode ( tree . get ( vertex ) [ i ] , vertex , height + 1 ) ; } } function addedge ( a , b ) { if ( tree . get ( a ) == null ) tree . set ( a , [ ] ) ; tree . get ( a ) . push ( b ) ; if ( tree . get ( b ) == null ) tree . set ( b , [ ] ) ; tree . get ( b ) . push ( a ) ; } function FindCenter ( n ) { maxHeight = - 1 ; maxHeightNode = - 1 ; farthestNode ( 0 , - 1 , 0 ) ; let leaf1 = maxHeightNode ; maxHeight = - 1 ; farthestNode ( maxHeightNode , - 1 , 0 ) ; let leaf2 = maxHeightNode ; path = [ ] ; getDiameterPath ( leaf1 , leaf2 , - 1 , path ) ; let pathSize = path . length ; if ( pathSize % 2 == 1 ) { document . write ( path [ parseInt ( pathSize / 2 , 10 ) ] ) ; } else { document . write ( path [ parseInt ( pathSize / 2 , 10 ) ] + " " + path [ parseInt ( ( pathSize - 1 ) / 2 , 10 ) ] ) ; } } let N = 4 ; tree = new Map ( ) ; addedge ( 1 , 0 ) ; addedge ( 1 , 2 ) ; addedge ( 1 , 3 ) ; FindCenter ( N ) ;
K | Javascript program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
let maxm = 1000000000 ; function count ( v , n ) { let i , odd = 0 , even = 0 ; let j , d , count ; let t = 1 << v . length ; let size = v . length ; for ( i = 1 ; i < t ; i ++ ) { d = 1 ; count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { d *= v [ j ] ; count ++ ; } } if ( count % 2 == 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } function BinarySearch ( l , r , v , key ) { let mid ; while ( r - l > 1 ) { mid = Math . floor ( ( l + r ) / 2 ) ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } let N = 2 , K = 10 ; let v = [ 2 , 3 ] ; document . write ( BinarySearch ( 1 , maxm , v , K ) + " " ) ;
Find integral points with minimum distance from given set of integers using BFS | Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; List to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code
function minDistancePoints ( A , K , n ) { let m = new Map ( ) ; let q = [ ] ; for ( let i = 0 ; i < n ; ++ i ) { m . set ( A [ i ] , true ) ; q . push ( A [ i ] ) ; } let ans = [ ] ; while ( K > 0 ) { let x = q . shift ( ) ; if ( ! m . has ( x - 1 ) && K > 0 ) { m . set ( x - 1 , true ) ; q . push ( x - 1 ) ; ans . push ( x - 1 ) ; K -- ; } if ( ! m . has ( x + 1 ) && K > 0 ) { m . set ( x + 1 , true ) ; q . push ( x + 1 ) ; ans . push ( x + 1 ) ; K -- ; } } for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ; } let A = [ - 1 , 4 , 6 ] ; let K = 3 ; let n = A . length ; minDistancePoints ( A , K , n ) ;
Leftmost Column with atleast one 1 in a row | JavaScript implementation to find the Leftmost Column with atleast a 1 in a sorted binary matrix ; Function to search for the leftmost column of the matrix with atleast a 1 in sorted binary matrix ; Loop to iterate over all the rows of the matrix ; Binary Search to find the leftmost occurence of the 1 ; Condition if the column contains the 1 at this position of matrix ; If there is a better solution then update the answer ; Condition if the solution doesn 't exist in the matrix ; Driver Code
var N = 3 ; function search ( mat , n , m ) { var i , a = 1000000000 ; for ( i = 0 ; i < n ; i ++ ) { var low = 0 ; var high = m - 1 ; var mid ; var ans = 1000000000 ; while ( low <= high ) { mid = parseInt ( ( low + high ) / 2 ) ; if ( mat [ i ] [ mid ] == 1 ) { if ( mid == 0 ) { ans = 0 ; break ; } else if ( mat [ i ] [ mid - 1 ] == 0 ) { ans = mid ; break ; } } if ( mat [ i ] [ mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } if ( ans < a ) a = ans ; } if ( a == 1000000000 ) return - 1 ; return a + 1 ; } var mat = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] ; document . write ( search ( mat , 3 , 3 ) ) ;
Largest index for each distinct character in given string with frequency K | JavaScript implementation of the approach Function to find largest index for each distinct character occuring exactly K times . ; Function to find largest index for each distinct character occuring exactly K times . ; Finding all characters present in S ; Finding all distinct characters in S ; To store result for each character ; Loop through each lower case English character ; If current character is absent in s ; Getting current character ; Finding count of character ch in S ; To store max Index encountred so far ; Printing required result ; If no such character exists , print - 1 ; Driver code
function maxSubString ( S , K , N ) { function maxSubString ( S , K , N ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; ++ i ) { freq [ S [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = 1 ; } var answer = [ ] ; for ( var i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] === 0 ) continue ; var ch = String . fromCharCode ( i + 97 ) ; var count = 0 ; var index = - 1 ; for ( var j = 0 ; j < N ; ++ j ) { if ( S [ j ] === ch ) count ++ ; if ( count === K ) index = j ; } answer . push ( [ ch , index ] ) ; } var flag = 0 ; for ( var i = 0 ; i < answer . length ; ++ i ) { if ( answer [ i ] [ 1 ] > - 1 ) { flag = 1 ; document . write ( answer [ i ] [ 0 ] + " " + answer [ i ] [ 1 ] + " " ) ; } } if ( flag === 0 ) document . write ( " " + " " ) ; } var S = " " ; var K = 2 ; var N = S . length ; maxSubString ( S . split ( " " ) , K , N ) ;
Smallest number greater than n that can be represented as a sum of distinct power of k | Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; Vector P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code
function greaterK ( n , k ) { let p = new Array ( String ( n ) . length + 2 ) . fill ( 0 ) ; let index = 0 ; let x = n ; while ( x > 0 ) { p [ index ] = ( x % k ) ; x = Math . floor ( x / k ) ; index ++ ; } let idx = 0 ; for ( let i = 0 ; i < p . length - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( let j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } let j = p . length - 1 ; if ( p [ j ] >= 2 ) { p [ index ] = 1 ; index ++ ; } let ans = 0 ; for ( let i = p . length - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } document . write ( ans + " " ) ; } let n = 29 , k = 7 ; greaterK ( n , k ) ;
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | Function that returns true if the string can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code
function canBeBalanced ( s , n ) { var count = 0 , count_1 = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count ++ ; else count -- ; count_1 = Math . min ( count_1 , count ) ; } if ( count_1 >= - 1 && count == 0 ) return true ; return false ; } var s = " " ; var n = s . length ; if ( canBeBalanced ( s , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Absolute difference between the XOR of Non | Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver code ; Find the absolute difference
function calculateDifference ( arr , n ) { let max_val = Math . max ( ... arr ) ; let prime = new Array ( max_val + 1 ) . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } let X1 = 1 , X2 = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { X1 ^= arr [ i ] ; } else if ( arr [ i ] != 1 ) { X2 ^= arr [ i ] ; } } return Math . abs ( X1 - X2 ) ; } let arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] ; let n = arr . length ; document . write ( calculateDifference ( arr , n ) ) ;
Count duplicates in a given linked list | Representation of a node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Starting from the next node ; If some duplicate node is found ; Return the count of duplicate nodes ; Driver Code
class Node { constructor ( ) { var data ; var next ; } } function insert ( head , item ) { var temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } function countNode ( head ) { let count = 0 ; while ( head . next != null ) { let ptr = head . next ; while ( ptr != null ) { if ( head . data == ptr . data ) { count ++ ; break ; } ptr = ptr . next ; } head = head . next ; } return count ; } var head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; document . write ( countNode ( head ) ) ;
Remove all occurrences of any element for maximum array sum | Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code
function maxSumArray ( arr , n ) { var sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) } var minimum = 1000000000 ; mp . forEach ( ( value , key ) => { minimum = Math . min ( minimum , value * key ) ; } ) ; return ( sum - minimum ) ; } var arr = [ 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 ] ; var n = arr . length ; document . write ( maxSumArray ( arr , n ) ) ;
Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | Javascript implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries
let MAX = 101 ; let prefix = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { prefix [ i ] = new Array ( MAX ) . fill ( 0 ) } let ar = new Array ( MAX ) . fill ( 0 ) ; function cal_prefix ( n , arr ) { for ( let i = 0 ; i < n ; i ++ ) ar [ i + 1 ] = arr [ i ] ; for ( let i = 1 ; i < MAX ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) prefix [ i ] [ j ] = 0 ; for ( let j = 1 ; j <= n ; j ++ ) { prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + ( ar [ j ] <= i ? 1 : 0 ) ; } } } function ksub ( l , r , n , k ) { let lo = 1 ; let hi = MAX - 1 ; while ( lo + 1 < hi ) { let mid = Math . floor ( ( lo + hi ) / 2 ) ; if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) hi = mid ; else lo = mid + 1 ; } if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) hi = lo ; return hi ; } let arr = new Array ( 1 , 4 , 2 , 3 , 5 , 7 , 6 ) ; let n = arr . length ; let k = 4 ; cal_prefix ( n , arr ) ; let queries = new Array ( new Array ( 1 , n , 1 ) , new Array ( 2 , n - 2 , 2 ) , new Array ( 3 , n - 1 , 3 ) ) ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) + " " ) ;
Find maximum N such that the sum of square of first N natural numbers is not more than X | Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate till maxvalue of N ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code
function squareSum ( N ) { let sum = parseInt ( ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ) ; return sum ; } function findMaxN ( X ) { let N = Math . sqrt ( X ) ; for ( let i = 1 ; i <= N ; i ++ ) { if ( squareSum ( i ) > X ) return i - 1 ; } return - 1 ; } let X = 25 ; document . write ( findMaxN ( X ) ) ;
Remove exactly one element from the array such that max | function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code
function max_min ( a , n ) { if ( n <= 1 ) return Number . MAX_VALUE ; let f_min = a [ 0 ] , s_min = Number . MAX_VALUE ; let f_max = a [ 0 ] , s_max = Number . MIN_VALUE ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= f_min ) { s_min = f_min ; f_min = a [ i ] ; } else if ( a [ i ] < s_min ) { s_min = a [ i ] ; } if ( a [ i ] >= f_max ) { s_max = f_max ; f_max = a [ i ] ; } else if ( a [ i ] > s_max ) { s_max = a [ i ] ; } } return Math . min ( ( f_max - s_min ) , ( s_max - f_min ) ) ; } let a = [ 1 , 3 , 3 , 7 ] ; let n = a . length ; document . write ( max_min ( a , n ) ) ;
Minimum in an array which is first decreasing then increasing | Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Print the smallest number
function minimal ( a , n ) { let lo = 0 , hi = n - 1 ; while ( lo < hi ) { let mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } let a = [ 8 , 5 , 4 , 3 , 4 , 10 ] ; let n = a . length ; let ind = minimal ( a , n ) ; document . write ( a [ ind ] ) ;
Leftmost and rightmost indices of the maximum and the minimum element of an array | Javascript implementation of the approach ; If found new minimum ; If arr [ i ] = min then rightmost index for min will change ; If found new maximum ; If arr [ i ] = max then rightmost index for max will change ; Driver code
function findIndices ( arr , n ) { let leftMin = 0 , rightMin = 0 ; let leftMax = 0 , rightMax = 0 ; let min = arr [ 0 ] , max = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) { leftMin = rightMin = i ; min = arr [ i ] ; } else if ( arr [ i ] == min ) rightMin = i ; if ( arr [ i ] > max ) { leftMax = rightMax = i ; max = arr [ i ] ; } else if ( arr [ i ] == max ) rightMax = i ; } document . write ( " " + leftMin + " " ) ; document . write ( " " + rightMin + " " ) ; document . write ( " " + leftMax + " " ) ; document . write ( " " + rightMax + " " ) ; } let arr = [ 2 , 1 , 1 , 2 , 1 , 5 , 6 , 5 ] ; let n = arr . length ; findIndices ( arr , n ) ;
Find smallest and largest element from square matrix diagonals | Function to find smallest and largest element from principal and secondary diagonal ; take length of matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Declare and initialize 5 X5 matrix
function diagonalsMinMax ( mat ) { let n = mat . length ; if ( n == 0 ) return ; let principalMin = mat [ 0 ] [ 0 ] , principalMax = mat [ 0 ] [ 0 ] ; let secondaryMin = mat [ n - 1 ] [ 0 ] , secondaryMax = mat [ n - 1 ] [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 1 ; j < n ; j ++ ) { if ( i == j ) { if ( mat [ i ] [ j ] < principalMin ) { principalMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > principalMax ) { principalMax = mat [ i ] [ j ] ; } } if ( ( i + j ) == ( n - 1 ) ) { if ( mat [ i ] [ j ] < secondaryMin ) { secondaryMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > secondaryMax ) { secondaryMax = mat [ i ] [ j ] ; } } } } document . write ( " " + principalMin + " " ) ; document . write ( " " + principalMax + " " ) ; document . write ( " " + secondaryMin + " " ) ; document . write ( " " + secondaryMax + " " ) ; } let matrix = [ [ 1 , 2 , 3 , 4 , - 10 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 1 , 2 , 11 , 3 , 4 ] , [ 5 , 6 , 70 , 5 , 8 ] , [ 4 , 9 , 7 , 1 , - 5 ] ] ; diagonalsMinMax ( matrix ) ;
Count elements such that there are exactly X elements with values greater than or equal to X | JavaScript implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Function to implement upper_bound ( ) ; Driver Code
function getCount ( v , n ) { v . sort ( function ( a , b ) { return a - b ; } ) ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let tmp = n - 1 - upperBound ( v , n , v [ i ] - 1 ) ; if ( tmp == v [ i ] ) cnt ++ ; } return cnt ; } function upperBound ( array , length , value ) { let low = 0 ; let high = length ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } let n = 4 ; let v = [ 1 , 2 , 3 , 4 ] ; document . write ( getCount ( v , n ) ) ;
Number of segments where all elements are greater than X | Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code
function countSegments ( a , n , x ) { let flag = false ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } let a = [ 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ] ; let n = a . length ; let x = 13 ; document . write ( countSegments ( a , n , x ) ) ;