codeparrot/xlcost-text-to-code huggingface dataset:
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in arr [ ] * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Finds the maximum element in each window of size k ; Check for window of size K ; For all possible subarrays of length k ; Until deque is empty ; Calculate the total cost and check if less than equal to c ; Find sum of current subarray and the total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Remove all the elements in the old window ; Calculate total cost and check <= c ; If current subarray length satisfies ; If it is not possible ;
function maxLength ( a , b , n , c ) { if ( n == 0 ) return 0 ; let max_length = 0 ; let low = 0 , high = n ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } function possible ( a , b , n , c , k ) { let dq = [ ] ; let sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . length > 0 && b [ i ] > b [ dq [ dq . length - 1 ] ] ) dq . pop ( ) ; dq . push ( i ) ; } let total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; for ( let i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; while ( dq . length > 0 && dq [ 0 ] <= i - k ) dq . pop ( ) ; while ( dq . length > 0 && b [ i ] > b [ dq [ dq . length - 1 ] ] ) dq . pop ( ) ; dq . push ( i ) ; total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; } return false ; } let A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ] ; let B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ] ; let N = A . length ; let C = 40 ; document . write ( maxLength ( A , B , N , C ) ) ;
Program to find the Nth natural number with exactly two bits set | Set 2 | Function to find the Nth number with exactly two bits set ; Initialize variables ; Initialize the range in which the value of ' a ' is present ; Perform Binary Search ; Find the mid value ; Update the range using the mid value t ; Find b value using a and N ; Prlet the value 2 ^ a + 2 ^ b ; Driver Code ; Function Call
function findNthNum ( N ) { let a = 0 , b , left ; let right , mid ; let t , last_num = 0 ; left = 1 ; right = N ; while ( left <= right ) { mid = left + ( right - left ) / 2 ; t = ( mid * ( mid + 1 ) ) / 2 ; if ( t < N ) { left = mid + 1 ; } else if ( t == N ) { a = mid ; break ; } else { a = mid ; right = mid - 1 ; } } t = a - 1 ; b = N - ( t * ( t + 1 ) ) / 2 - 1 ; document . write ( ( 1 << a ) + ( 1 << b ) ) ; } let N = 15 ; findNthNum ( N ) ;
Longest subsequence having maximum sum | Function to find the longest subsequence from the given array with maximum sum ; Stores the largest element of the array ; If Max is less than 0 ; Print the largest element of the array ; Traverse the array ; If arr [ i ] is greater than or equal to 0 ; Print the elements of the subsequence ; Driver code
function longestSubWithMaxSum ( arr , N ) { let Max = Math . max ( ... arr ) ; if ( Max < 0 ) { document . write ( Max ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , - 4 , - 2 , 3 , 0 ] ; let N = arr . length ; longestSubWithMaxSum ( arr , N ) ;
Print first K distinct Moran numbers from a given array | Function to calculate the sum of digits of a number ; Stores the sum of digits ; Add the digit to sum ; Remove digit ; Returns the sum of digits ; Function to check if a number is prime or not ; If r has any divisor ; Set r as non - prime ; Function to check if a number is moran number ; Calculate sum of digits ; Check if n is divisible by the sum of digits ; Calculate the quotient ; If the quotient is prime ; Function to print the first K Moran numbers from the array ; Sort the given array ; Initialise a set ; Traverse the array from the end ; If the current array element is a Moran number ; Insert into the set ; Driver Code
function digiSum ( a ) { let sum = 0 ; while ( a ) { sum += a % 10 ; a = Math . floor ( a / 10 ) ; } return sum ; } function isPrime ( r ) { let s = true ; for ( let i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } function isMorannumber ( n ) { let dup = n ; let sum = digiSum ( dup ) ; if ( n % sum == 0 ) { let c = n / sum ; if ( isPrime ( c ) ) { return true ; } } return false ; } function FirstKMorannumber ( a , n , k ) { let X = k ; a = a . sort ( ( x , y ) => x - y ) let s = new Set ( ) ; for ( let i = n - 1 ; i >= 0 && k > 0 ; i -- ) { if ( isMorannumber ( a [ i ] ) ) { s . add ( a [ i ] ) ; k -- ; } } if ( k > 0 ) { document . write ( X + " " + " " + " " ) ; return ; } s = [ ... s ] . sort ( ( a , b ) => a - b ) for ( let it of s ) { document . write ( it + " " ) ; } document . write ( " " ) ; } let A = [ 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 ] ; let K = 4 ; let N = A . length ; FirstKMorannumber ( A , N , K ) ;
Number of pairs whose sum is a power of 2 | Set 2 | Function to count all pairs whose sum is a power of two ; Stores the frequency of each element of the array ; Update frequency of array elements ; Stores count of required pairs ; Current power of 2 ; Traverse the array ; If pair does not exist ; Increment count of pairs ; Return the count of pairs ; Driver Code
function countPair ( arr , n ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) else m . set ( arr [ i ] , 1 ) } var ans = 0 ; for ( var i = 0 ; i < 31 ; i ++ ) { var key = Math . pow ( 2 , i ) ; for ( var j = 0 ; j < n ; j ++ ) { var k = key - arr [ j ] ; if ( ! m . has ( k ) ) continue ; else ans += m . get ( k ) ; if ( k == arr [ j ] ) ans ++ ; } } return ans / 2 ; } var arr = [ 1 , 8 , 2 , 10 , 6 ] var n = arr . length document . write ( countPair ( arr , n ) ) ;
Longest Subsequence from a numeric String divisible by K | Function to if the integer representation of the current string is divisible by K ; Stores the integer representation of the string ; Check if the num is divisible by K ; Function to find the longest subsequence which is divisible by K ; If the number is divisible by K ; If current number is the maximum obtained so far ; Include the digit at current index ; Exclude the digit at current index ; Driver Code ; Printing the largest number which is divisible by K ; If no number is found to be divisible by K
function isdivisible ( newstr , K ) { var num = 0 ; for ( var i = 0 ; i < newstr . length ; i ++ ) { num = num * 10 + newstr [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } if ( num % K == 0 ) return true ; else return false ; } function findLargestNo ( str , newstr , ans , index , K ) { if ( index == str . length ) { if ( isdivisible ( newstr , K ) ) { if ( ( ans < newstr && ans . length == newstr . length ) newstr . length > ans . length ) { ans = newstr ; } } return ans ; } var x = newstr + str [ index ] ; ans = findLargestNo ( str , x , ans , index + 1 , K ) ; ans = findLargestNo ( str , newstr , ans , index + 1 , K ) ; return ans ; } var str = " " ; var ans = " " , newstr = " " ; var K = 8 ; ans = findLargestNo ( str , newstr , ans , 0 , K ) ; if ( ans != " " ) document . write ( ans ) else document . write ( - 1 )
Counting Rock Samples | TCS Codevita 2020 | Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function call
function findRockSample ( ranges , n , r , arr ) { let a = [ ] ; for ( let i = 0 ; i < r ; i ++ ) { let c = 0 ; let l = ranges [ i ] [ 0 ] ; let h = ranges [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . push ( c ) ; } for ( let i = 0 ; i < a . length ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let n = 5 ; let r = 2 ; let arr = [ 400 , 567 , 890 , 765 , 987 ] ; let ranges = [ [ 300 , 380 ] , [ 800 , 1000 ] ] ; findRockSample ( ranges , n , r , arr ) ;
N digit numbers having difference between the first and last digits as K | Function to store and check the difference of digits ; Base Case ; Last digit of the number to check the difference from the first digit ; Condition to avoid repeated values ; Update the string pt ; Recursive Call ; Update the string pt ; Recursive Call ; Any number can come in between first and last except the zero ; Recursive Call ; Function to place digit of the number ; When N is 1 and K > 0 , then the single number will be the first and last digit it cannot have difference greater than 0 ; This loop place the digit at the starting ; Recursive Call ; Vector to store results ; Generate all the resultant number ; Print the result ; Driver Code ; Function Call
function findNumbers ( st , result , prev , n , K ) { if ( st . length == n ) { result . push ( parseInt ( st ) ) ; return ; } if ( st . length == n - 1 ) { if ( prev - K >= 0 ) { let pt = " " ; pt += String . fromCharCode ( prev - K + 48 ) ; findNumbers ( st + pt , result , prev - K , n , K ) ; } if ( K != 0 && prev + K < 10 ) { let pt = " " ; pt += String . fromCharCode ( prev + K + 48 ) ; findNumbers ( st + pt , result , prev + K , n , K ) ; } } else { for ( let j = 1 ; j <= 9 ; j ++ ) { let pt = " " ; pt += String . fromCharCode ( j + 48 ) ; findNumbers ( st + pt , result , prev , n , K ) ; } } } function numDifference ( N , K ) { let res = [ ] ; let st = " " ; if ( N == 1 && K == 0 ) { res . push ( 0 ) ; } else if ( N == 1 && K > 0 ) { return res ; } for ( let i = 1 ; i < 10 ; i ++ ) { let temp = " " ; temp += String . fromCharCode ( 48 + i ) ; findNumbers ( st + temp , res , i , N , K ) ; st = " " ; } return res ; } function numDifferenceUtil ( N , K ) { let res = [ ] ; res = numDifference ( N , K ) ; for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } let N = 2 , K = 9 ; numDifferenceUtil ( N , K ) ;
Count array elements that can be maximized by adding any permutation of first N natural numbers | Function to get the count of values that can have the maximum value ; Sort array in decreasing order ; Stores the answer ; mark stores the maximum value till each index i ; Check if arr [ i ] can be maximum ; Update the mark ; Prlet the total count ; Given array arr [ ] ; Function call
function countMaximum ( a , n ) { a . sort ( ) ; a . reverse ( ) ; let count = 0 ; let mark = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = Math . max ( mark , a [ i ] + i + 1 ) ; } document . write ( count ) ; } let arr = [ 8 , 9 , 6 ] ; let N = arr . length ; countMaximum ( arr , N ) ;
Kth character from the Nth string obtained by the given operations | String reverse ; Function to return Kth character from recursive String ; If N is 1 then return A ; Iterate a loop and generate the recursive String ; Update current String ; Change A to B and B to A ; Reverse the previous String ; Return the kth character ; Driver Code
function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } function findKthChar ( n , k ) { let prev = " " ; let cur = " " ; if ( n == 1 ) { return ' ' ; } for ( let j = 2 ; j <= n ; j ++ ) { cur = prev + " " ; for ( let i = 0 ; i < prev . length ; i ++ ) { if ( prev [ i ] == ' ' ) { prev [ prev [ i ] ] = ' ' ; } else { prev [ prev [ i ] ] = ' ' ; } } prev = reverse ( prev ) ; cur += prev ; prev = cur ; } return cur [ k ] ; } let N = 4 ; let K = 3 ; document . write ( findKthChar ( N , K ) ) ;
Maximize length of subarray of equal elements by performing at most K increment operations | Function to find the maximum length of subarray of equal elements after performing at most K increments ; Stores the size of required subarray ; Starting point of a window ; Stores the sum of window ; Iterate over array ; Current element ; Remove index of minimum elements from deque which are less than the current element ; Insert current index in deque ; Update current window sum ; Calculate required operation to make current window elements equal ; If cost is less than k ; Shift window start pointer towards right and update current window sum ; Return answer ; Driver Code ; Length of array
function maxSubarray ( a , k , n ) { var answer = 0 ; var start = 0 ; var s = 0 ; var dq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { var x = a [ i ] ; while ( dq . length != 0 && a [ dq [ 0 ] ] <= x ) dq . shift ( ) ; dq . push ( i ) ; s += x ; var cost = a [ dq [ 0 ] ] * ( answer + 1 ) - s ; if ( cost <= k ) answer ++ ; else { if ( dq [ 0 ] == start ) dq . shift ( ) ; s -= a [ start ++ ] ; } } return answer ; } var a = [ 2 , 2 , 4 ] ; var k = 10 ; var n = a . length ; document . write ( maxSubarray ( a , k , n ) ) ;
Check if an array can be split into subarrays with GCD exceeding K | Function to check if it is possible to split an array into subarrays having GCD at least K ; Iterate over the array arr [ ] ; If the current element is less than or equal to k ; If no array element is found to be less than or equal to k ; Given array arr [ ] ; Length of the array ; Given K ; Function call
function canSplitArray ( arr , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) { return " " ; } } return " " ; } let arr = [ 2 , 4 , 6 , 1 , 8 , 16 ] ; let N = arr . length ; let K = 3 ; document . write ( canSplitArray ( arr , N , K ) ) ;
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver Code
function smallSumSubset ( data , target , maxVal ) { let sum = 0 ; for ( let i = 0 ; i < data . length ; i ++ ) sum += data [ i ] ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . length ; else if ( data [ 0 ] >= target ) return 1 ; else if ( data [ 0 ] < target ) { let temp = [ ] ; for ( let i = 1 ; i < data . length ; i ++ ) temp . push ( data [ i ] ) ; return Math . min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data [ 0 ] , maxVal ) ) ; } } let data = [ 3 , 1 , 7 , 1 , 2 ] ; let target = 11 ; let val = smallSumSubset ( data , target , data . length + 1 ) ; if ( val > data . length ) document . write ( - 1 ) ; else document . write ( val ) ;
Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | Function to maintain the case , X >= Y ; To maintain X >= Y ; Replace ' p ' to ' r ' ; Replace ' r ' to ' p ' . ; Function to return the maximum cost ; Stores the length of the String ; To maintain X >= Y . ; Stores the maximum cost ; Stores the count of ' p ' after removal of all " pr " subStrings up to str [ i ] ; Stores the count of ' r ' after removal of all " pr " subStrings up to str [ i ] ; Stack to maintain the order of characters after removal of subStrings ; If subString " pr " is removed ; Increase cost by X ; If any subString " rp " left in the Stack ; If any subString " rp " left in the Stack ; Driver Code
function swapXandY ( str , X , Y ) { let N = str . length ; X = X + Y ; Y = X - Y ; X = X - Y ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { str [ i ] = ' ' ; } else if ( str [ i ] == ' ' ) { str [ i ] = ' ' ; } } return true ; } function maxCost ( str , X , Y ) { let N = str . length ; if ( Y > X ) { swapXandY ( str . split ( ' ' ) , X , Y ) ; } let res = 0 ; let countP = 0 ; let countR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { countP ++ ; } else if ( str [ i ] == ' ' ) { if ( countP > 0 ) { countP -- ; res += X ; } else countR ++ ; } else { res += Math . min ( countP , countR ) * Y ; countP = 0 ; countR = 0 ; } } res += Math . min ( countP , countR ) * Y ; return res ; } let str = " " ; let X = 5 , Y = 4 ; document . write ( maxCost ( str , X , Y ) ) ;
Check if an array contains only one distinct element | Function to find if the array contains only one distinct element ; Assume first element to be the unique element ; Traversing the array ; If current element is not equal to X then break the loop and print No ; Compare and print the result ; Driver code ; Function call
function uniqueElement ( arr ) { var x = arr [ 0 ] ; var flag = 1 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; uniqueElement ( arr ) ;
Maximum Count of pairs having equal Sum based on the given conditions | Function to find the maximum count of pairs having equal sum ; Size of the array ; Iterate through evey sum of pairs possible from the given array ; Count of pairs with given sum ; Check for a possible pair ; Update count of possible pair ; Update the answer by taking the pair which is maximum for every possible sum ; Return the max possible pair ; Function to return the count of pairs ; Size of the array ; Stores the frequencies ; Count the frequency ; Driver Code
function maxCount ( freq , maxi , mini ) { let n = freq . length - 1 ; let ans = 0 ; for ( let sum = 2 * mini ; sum <= 2 * maxi ; ++ sum ) { let possiblePair = 0 ; for ( let firElement = 1 ; firElement < Math . floor ( ( sum + 1 ) / 2 ) ; firElement ++ ) { if ( sum - firElement <= maxi ) { possiblePair += Math . min ( freq [ firElement ] , freq [ sum - firElement ] ) ; } } if ( sum % 2 == 0 ) { possiblePair += freq [ sum / 2 ] / 2 ; } ans = Math . max ( ans , possiblePair ) ; } return ans ; } function countofPairs ( a ) { let n = a . length ; let freq = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let maxi = - 1 ; let mini = n + 1 ; for ( let i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; mini = Math . min ( mini , a [ i ] ) ; } for ( let i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; return maxCount ( freq , maxi , mini ) ; } let a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ] ; document . write ( countofPairs ( a ) ) ;
Maximum Subarray Sum possible by replacing an Array element by its Square | Function to find the maximum subarray sum possible ; Stores sum without squaring ; Stores sum squaring ; Stores the maximum subarray sum ; Either extend the subarray or start a new subarray ; Either extend previous squared subarray or start a new subarray by squaring the current element ; Update maximum subarray sum ; Return answer ; Driver Code ; Function call
function getMaxSum ( a , n ) { let dp = new Array ( n ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 0 ] ; let max_sum = Math . max ( dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( a [ i ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 1 ] + a [ i ] , a [ i ] * a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + a [ i ] * a [ i ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 1 ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 0 ] ) ; } return max_sum ; } let n = 5 ; let a = [ 1 , - 5 , 8 , 12 , - 8 ] ; document . write ( getMaxSum ( a , n ) ) ;
Smallest Subarray with Sum K from an Array | Function to find the length of the smallest subarray with sum K ; Stores the frequency of prefix sums in the array ; Initialize len as INT_MAX ; If sum of array till i - th index is less than K ; No possible subarray exists till i - th index ; Find the exceeded value ; If exceeded value is zero ; Driver Code
function subArraylen ( arr , n , K ) { var mp = new Map ( ) ; mp . set ( arr [ 0 ] , 0 ) ; for ( var i = 1 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; mp . set ( arr [ i ] , i ) ; } var len = 10000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < K ) continue ; else { var x = arr [ i ] - K ; if ( x == 0 ) len = Math . min ( len , i ) ; if ( ! mp . has ( x ) ) continue ; else { len = Math . min ( len , i - mp . get ( x ) ) ; } } } return len ; } var arr = [ 1 , 2 , 4 , 3 , 2 , 4 , 1 ] ; var n = arr . length ; var K = 7 ; var len = subArraylen ( arr , n , K ) ; if ( len == 1000000000 ) { document . write ( " " ) ; } else { document . write ( len ) ; }
Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | Stores at each index if it 's a prime or not  ; Sieve of Eratosthenes to generate all possible primes ; If p is a prime ; Set all multiples of p as non - prime ; Function to generate the required Fibonacci Series ; Stores all primes between n1 and n2 ; Generate all primes between n1 and n2 ; Stores all concatenations of each pair of primes ; Generate all concatenations of each pair of primes ; Stores the primes out of the numbers generated above ; Store the unique primes ; Find the minimum ; Find the minimum ; Find N ; Print the N - th term of the Fibonacci Series ; Driver Code
let prime = new Array ( 100001 ) ; function SieveOfEratosthenes ( ) { for ( let i = 0 ; i < 100001 ; i ++ ) prime [ i ] = 1 ; let p = 2 ; while ( p * p <= 100000 ) { if ( prime [ p ] == 1 ) { for ( let i = p * p ; i < 100001 ; i += p ) prime [ i ] = 0 ; } p += 1 ; } } function join ( a , b ) { let mul = 1 ; let bb = b ; while ( b != 0 ) { mul *= 10 ; b = parseInt ( b / 10 , 10 ) ; } a *= mul ; a += bb ; return a ; } function fibonacciOfPrime ( n1 , n2 ) { SieveOfEratosthenes ( ) ; let initial = [ ] ; for ( let i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] == 1 ) initial . push ( i ) ; let now = [ ] ; for ( let i = 0 ; i < initial . length ; i ++ ) { for ( let j = 0 ; j < initial . length ; j ++ ) { let a = initial [ i ] ; let b = initial [ j ] ; if ( a != b ) { let c = join ( a , b ) ; now . push ( c ) ; } } } let current = [ ] ; for ( let i = 0 ; i < now . length ; i ++ ) if ( prime [ now [ i ] ] == 1 ) current . push ( now [ i ] ) ; let cnt = new Array ( 1000009 ) ; for ( let i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; let current_set = [ ] ; for ( let i = 0 ; i < current . length ; i ++ ) { cnt [ current [ i ] ] ++ ; if ( cnt [ current [ i ] ] == 1 ) current_set . push ( current [ i ] ) ; } let first = 1000000000 ; for ( let i = 0 ; i < current_set . length ; i ++ ) first = Math . min ( first , current_set [ i ] ) ; let second = 0 ; for ( let i = 0 ; i < current_set . length ; i ++ ) second = Math . max ( second , current_set [ i ] ) ; let count = current_set . length - 1 ; let curr = 1 ; let c = 0 ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } document . write ( c ) ; } let x = 2 ; let y = 40 ; fibonacciOfPrime ( x , y ) ;
Count of Array elements greater than all elements on its left and next K elements on its right | Function to print the count of Array elements greater than all elements on its left and next K elements on its right ; Iterate over the array ; If the stack is not empty and the element at the top of the stack is smaller than arr [ i ] ; Store the index of next greater element ; Pop the top element ; Insert the current index ; Stores the count ; Driver Code
function countElements ( arr , n , k ) { var s = [ ] ; var next_greater = new Array ( n + 1 ) . fill ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s . length === 0 ) { s . push ( i ) ; continue ; } while ( s . length !== 0 && arr [ s [ s . length - 1 ] ] < arr [ i ] ) { next_greater [ s [ s . length - 1 ] ] = i ; s . pop ( ) ; } s . push ( i ) ; } var count = 0 ; var maxi = - 2147483648 ; for ( var i = 0 ; i < n ; i ++ ) { if ( next_greater [ i ] - i > k && maxi < arr [ i ] ) { maxi = Math . max ( maxi , arr [ i ] ) ; count ++ ; } } return count ; } var arr = [ 4 , 2 , 3 , 6 , 4 , 3 , 2 ] ; var K = 2 ; var n = arr . length ; document . write ( countElements ( arr , n , K ) ) ;