codeparrot/xlcost-text-to-code huggingface dataset:
Queries to answer the number of ones and zero to the left of given index | Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
function preCalculate ( binary , n , left ) { let count1 = 0 , count0 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { left [ i ] [ 0 ] = count1 ; left [ i ] [ 1 ] = count0 ; if ( binary [ i ] != 0 ) count1 ++ ; else count0 ++ ; } } let binary = [ 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ; let n = binary . length ; let left = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) left [ i ] = [ 0 , 0 ] ; preCalculate ( binary , n , left ) ; let queries = [ 0 , 1 , 2 , 4 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( left [ queries [ i ] ] [ 0 ] + " " + left [ queries [ i ] ] [ 1 ] + " " ) ;
Count of index pairs with equal elements in an array | A method to return number of pairs with equal values ; Finding frequency of each number . ; Calculating count of pairs with equal values ; Driver code
function countPairs ( arr , n ) { let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . set ( arr [ i ] , 1 ) ; } let ans = 0 ; for ( let [ key , value ] of hm . entries ( ) ) { let count = value ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } let arr = [ 1 , 2 , 3 , 1 ] ; document . write ( countPairs ( arr , arr . length ) ) ;
Partition the array into three equal sum segments | First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code
let pos1 = - 1 ; let pos2 = - 1 ; function equiSumUtil ( arr ) { let n = arr . length ; let pre = new Array ( n ) ; let sum = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } let suf = new Array ( n ) ; sum = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } let total_sum = sum ; let j = n - 1 ; i = 0 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != - 1 && pos2 != - 1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } function equiSum ( arr ) { let ans = equiSumUtil ( arr ) ; if ( ans ) { document . write ( " " ) ; for ( let i = 0 ; i <= pos1 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos1 + 1 ; i < pos2 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos2 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } else { document . writeLine ( " " + " " ) ; } } let arr = [ 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 ] ; equiSum ( arr ) ;
Leftmost and rightmost indices of the maximum and the minimum element of an array | Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
function getRightMin ( arr , n ) { let min = arr [ 0 ] ; let rightMin = 0 ; let i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } function getLeftMax ( arr , n ) { let max = arr [ n - 1 ] ; let leftMax = n - 1 ; let i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i = parseInt ( i / 2 , 10 ) ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } let arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ] ; let n = arr . length ; document . write ( " " + 0 + " " ) ; document . write ( " " + getRightMin ( arr , n ) + " " ) ; document . write ( " " + getLeftMax ( arr , n ) + " " ) ; document . write ( " " + ( n - 1 ) ) ;
Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Driver code
function countNum ( arr , n ) { let count = 0 ; arr . sort ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } let arr = [ 3 , 5 , 8 , 6 ] ; let n = arr . length ; document . write ( countNum ( arr , n ) ) ;
Find the k smallest numbers after deleting given elements | Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code
function findElementsAfterDel ( arr , m , del , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let heap = [ ] ; for ( let i = 0 ; i < m ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . delete ( arr [ i ] ) ; } else heap . push ( arr [ i ] ) ; } heap . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < k ; ++ i ) { document . write ( heap [ 0 ] + " " ) ; heap . splice ( 0 , 1 ) ; } } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 56 , 5 ] ; let n = del . length ; let k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ;
Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Drivers code
function countNum ( arr , n ) { let s = new Set ( ) ; let count = 0 , maxm = Number . MIN_VALUE , minm = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( let i = minm ; i <= maxm ; i ++ ) if ( ! s . has ( i ) ) count ++ ; return count ; } let arr = [ 3 , 5 , 8 , 6 ] ; let n = arr . length ; document . write ( countNum ( arr , n ) ) ;
Find minimum speed to finish all Jobs | Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver program ; Print required maxLenwer
function isPossible ( A , n , H , K ) { var time = 0 ; for ( var i = 0 ; i < n ; ++ i ) time += parseInt ( ( A [ i ] - 1 ) / K ) + 1 ; return time <= H ; } function minJobSpeed ( A , n , H ) { if ( H < n ) return - 1 ; var max = A . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var lo = 1 , hi = max ; while ( lo < hi ) { var mi = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } var A = [ 3 , 6 , 7 , 11 ] , H = 8 ; var n = A . length ; document . write ( minJobSpeed ( A , n , H ) ) ;
Number of anomalies in an array | JavaScript program for the above approach ; Sort the array so that we can apply binary search . ; One by one check every element if it is anomaly or not using binary search . ; If arr [ i ] is not largest element and element just greater than it is within k , then return false . ; If there are more than one occurrences of arr [ i ] , return false . ; If arr [ i ] is not smallest element and just smaller element is not k distance away ; Driver Code
function countAnomalies ( a , n , k ) { a . sort ( ) ; let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let u = upper_bound ( a , 0 , n , a [ i ] ) ; if ( u < n && a [ u ] - a [ i ] <= k ) continue ; let s = lower_bound ( a , 0 , n , a [ i ] ) ; if ( u - s > 1 ) continue ; if ( s > 0 && a [ s - 1 ] - a [ i ] <= k ) continue ; res ++ ; } return res ; } function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let arr = [ 7 , 1 , 8 ] , k = 5 ; let n = arr . length ; document . write ( countAnomalies ( arr , n , k ) ) ;
Subarrays with distinct elements | Returns sum of lengths of all subarrays with distinct elements . ; For maintaining distinct elements . ; Initialize ending point and result ; Fix starting point ; Calculating and adding all possible length subarrays in arr [ i . . j ] ; Remove arr [ i ] as we pick new stating point from next ; Driver Code
function sumoflength ( arr , n ) { let s = new Set ( ) ; let j = 0 , ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < n && ! s . has ( arr [ j ] ) ) { s . add ( arr [ i ] ) ; j ++ ; } ans += Math . floor ( ( ( j - i ) * ( j - i + 1 ) ) / 2 ) ; s . delete ( arr [ i ] ) ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( sumoflength ( arr , n ) ) ;
Count subarrays having total distinct elements same as original array |  ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver method
/ *function countDistictSubarray(arr,n) { let vis = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) vis . set ( arr [ i ] , 1 ) ; let k = vis . size ; let vid = new Map ( ) ; let ans = 0 , right = 0 , window = 0 ; for ( let left = 0 ; left < n ; left ++ ) { while ( right < n && window < k ) { if ( vid . has ( arr [ right ] ) ) vid . set ( arr [ right ] , vid . get ( arr [ right ] ) + 1 ) ; else vid . set ( arr [ right ] , 1 ) ; if ( vid . get ( arr [ right ] ) == 1 ) window ++ ; right ++ ; } if ( window == k ) ans += ( n - right + 1 ) ; if ( vid . has ( arr [ left ] ) ) vid . set ( arr [ left ] , vid . get ( arr [ left ] ) - 1 ) ; if ( vid . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } let arr = [ 2 , 1 , 3 , 2 , 3 ] ; document . write ( countDistictSubarray ( arr , arr . length ) ) ;
Count subarrays with same even and odd elements | function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; create two auxiliary hash arrays to count frequency of difference , one array for non - negative difference and other array for negative difference . Size of these two auxiliary arrays is ' n + 1' because difference can reach maximum value ' n ' as well as minimum value ' - n ' ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays
function countSubarrays ( arr , n ) { let difference = 0 ; let ans = 0 ; let hash_positive = new Array ( n + 1 ) ; let hash_negative = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { hash_positive [ i ] = 0 ; hash_negative [ i ] = 0 ; } hash_positive [ 0 ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; } else { difference -- ; } if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } let arr = [ 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 ] ; let n = arr . length ; document . write ( " " + " " + countSubarrays ( arr , n ) ) ;
Saddleback Search Algorithm in a 2D array | Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code
function search ( mat , m , n , x ) { var i = m - 1 , j = 0 ; while ( i >= 0 && j < n ) { if ( mat [ i ] [ j ] == x ) return true ; if ( mat [ i ] [ j ] > x ) i -- ; else j ++ ; } return false ; } var mat = [ [ 10 , 20 , 30 , 40 ] , [ 15 , 25 , 35 , 45 ] , [ 27 , 29 , 37 , 48 ] , [ 32 , 33 , 39 , 50 ] , [ 50 , 60 , 70 , 80 ] ] ; if ( search ( mat , 5 , 4 , 29 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of Larger Elements on right side in a string | Javascript program to find counts of right greater characters for every character . ; Driver code
function printGreaterCount ( str ) { let len = str . length ; let right = new Array ( len ) ; right . fill ( 0 ) ; for ( let i = 0 ; i < len ; i ++ ) { for ( let j = i + 1 ; j < len ; j ++ ) { if ( str [ i ] . charCodeAt ( ) < str [ j ] . charCodeAt ( ) ) { right [ i ] ++ ; } } } for ( let i = 0 ; i < len ; i ++ ) { document . write ( right [ i ] + " " ) ; } } let str = " " ; printGreaterCount ( str ) ;
N / 3 repeated number in an array with O ( 1 ) space | Javascript program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
function appearsNBy3 ( arr , n ) { let count1 = 0 , count2 = 0 ; let first = Number . MAX_VALUE ; let second = Number . MAX_VALUE ; for ( let i = 1 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > parseInt ( n / 3 , 10 ) ) return first ; if ( count2 > parseInt ( n / 3 , 10 ) ) return second ; return - 1 ; } let arr = [ 1 , 2 , 3 , 1 , 1 ] ; let n = arr . length ; document . write ( appearsNBy3 ( arr , n ) ) ;
Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver Code
function countMaxSetBits ( left , right ) { let max_count = - 1 , num = 0 ; for ( let i = left ; i <= right ; ++ i ) { let temp = i , cnt = 0 ; while ( temp > 0 ) { if ( temp % 2 == 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } let l = 1 , r = 5 ; document . write ( countMaxSetBits ( l , r ) + " " ) ; l = 1 ; r = 10 ; document . write ( countMaxSetBits ( l , r ) ) ;
Quickselect Algorithm | partition function similar to quick sort Considers last element as pivot and adds elements with less value to the left and high value to the right and also changes the pivot position to its respective position in the final array . ; inserting elements of less value to the left of the pivot location ; swapping pivot to the final pivot location ; finds the kth position ( of the sorted array ) in a given unsorted array i . e this function can be used to find both kth largest and kth smallest element in the array . ASSUMPTION : all elements in arr [ ] are distinct ; find the partition ; if partition value is equal to the kth position , return value at k . ; if partition value is less than kth position , search right side of the array . ; if partition value is more than kth position , search left side of the array . ; Driver Code
function _partition ( arr , low , high ) { let pivot = arr [ high ] , pivotloc = low ; for ( let i = low ; i <= high ; i ++ ) { if ( arr [ i ] < pivot ) { let temp = arr [ i ] ; arr [ i ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; pivotloc ++ ; } } let temp = arr [ high ] ; arr [ high ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; return pivotloc ; } function kthSmallest ( arr , low , high , k ) { let partition = _partition ( arr , low , high ) ; if ( partition == k - 1 ) return arr [ partition ] ; else if ( partition < k - 1 ) return kthSmallest ( arr , partition + 1 , high , k ) ; else return kthSmallest ( arr , low , partition - 1 , k ) ; } let array = [ 10 , 4 , 5 , 8 , 6 , 11 , 26 ] ; let arraycopy = [ 10 , 4 , 5 , 8 , 6 , 11 , 26 ] ; let kPosition = 3 ; let length = array . length ; if ( kPosition > length ) { document . write ( " " ) ; } else { document . write ( " " + kthSmallest ( arraycopy , 0 , length - 1 , kPosition ) + " " ) ; }
Recaman 's sequence | Prints first n terms of Recaman sequence ; Create an array to store terms ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
function recaman ( n ) { let arr = new Array ( n ) ; arr [ 0 ] = 0 ; document . write ( arr [ 0 ] + " " ) ; for ( let i = 1 ; i < n ; i ++ ) { let curr = arr [ i - 1 ] - i ; let j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; document . write ( arr [ i ] + " " ) ; } } let n = 17 ; recaman ( n ) ;
Print the longest leaf to leaf path in a Binary tree | Tree node structure used in the program ; Function to find height of a tree ; update the answer , because diameter of a tree is nothing but maximum value of ( left_height + right_height + 1 ) for each node ; save the root , this will help us finding the left and the right part of the diameter ; save the height of left & right subtree as well . ; prints the root to leaf path ; print left part of the path in reverse order ; this function finds out all the root to leaf paths ; append this node to the path array ; If it 's a leaf, so print the path that led to here ; print only one path which is equal to the height of the tree . ; otherwise try both subtrees ; Computes the diameter of a binary tree with given root . ; lh will store height of left subtree rh will store height of right subtree ; f is a flag whose value helps in printing left & right part of the diameter only once ; print the left part of the diameter ; print the right part of the diameter ; Enter the binary tree ... 1 / \ 2 3 / \ 4 5 \ / \ 8 6 7 / 9
class Node { constructor ( val ) { this . data = val ; this . left = this . right = null ; } } let ans , lh , rh , f ; let k ; let Root ; function height ( root ) { if ( root == null ) return 0 ; let left_height = height ( root . left ) ; let right_height = height ( root . right ) ; if ( ans < 1 + left_height + right_height ) { ans = 1 + left_height + right_height ; k = root ; lh = left_height ; rh = right_height ; } return 1 + Math . max ( left_height , right_height ) ; } function printArray ( ints , len ) { let i ; if ( f == 0 ) { for ( i = len - 1 ; i >= 0 ; i -- ) { document . write ( ints [ i ] + " " ) ; } } else if ( f == 1 ) { for ( i = 0 ; i < len ; i ++ ) { document . write ( ints [ i ] + " " ) ; } } } function printPathsRecur ( node , path , pathLen , max ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) { if ( pathLen == max && ( f == 0 f == 1 ) ) { printArray ( path , pathLen ) ; f = 2 ; } } else { printPathsRecur ( node . left , path , pathLen , max ) ; printPathsRecur ( node . right , path , pathLen , max ) ; } } function diameter ( root ) { if ( root == null ) return ; ans = Number . MIN_VALUE ; lh = 0 ; rh = 0 ; f = 0 ; let height_of_tree = height ( root ) ; let lPath = new Array ( 100 ) ; let pathlen = 0 ; printPathsRecur ( k . left , lPath , pathlen , lh ) ; document . write ( k . data + " " ) ; let rPath = new Array ( 100 ) ; f = 1 ; printPathsRecur ( k . right , rPath , pathlen , rh ) ; } Root = new Node ( 1 ) ; Root . left = new Node ( 2 ) ; Root . right = new Node ( 3 ) ; Root . left . left = new Node ( 4 ) ; Root . left . right = new Node ( 5 ) ; Root . left . right . left = new Node ( 6 ) ; Root . left . right . right = new Node ( 7 ) ; Root . left . left . right = new Node ( 8 ) ; Root . left . left . right . left = new Node ( 9 ) ; diameter ( Root ) ;
Recaman 's sequence | Print first n terms of Recaman sequence ; Prlet first term and store it in a hash ; Prlet remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
function recaman ( n ) { if ( n <= 0 ) return ; document . write ( 0 + " " ) ; let s = new Set ( ) ; s . add ( 0 ) ; let prev = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let curr = prev - i ; if ( curr < 0 || s . has ( curr ) ) curr = prev + i ; s . add ( curr ) ; document . write ( curr + " " ) ; prev = curr ; } } let n = 17 ; recaman ( n ) ;
Find index of an extra element present in one sorted array | Function to find Index ; Calculating extra element ; returns index of extra element ; function return sum of array elements ; function return index of given element ; Driver Code
function find_extra_element_index ( arrA , arrB ) { let extra_element = sum ( arrA ) - sum ( arrB ) ; return indexOf ( arrA , extra_element ) ; } function sum ( arr ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; } return sum ; } function indexOf ( arr , element ) { for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return - 1 ; } let arrA = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] ; let arrB = [ 2 , 4 , 6 , 8 , 10 , 12 ] ; document . write ( find_extra_element_index ( arrA , arrB ) ) ;