codeparrot/xlcost-text-to-code huggingface dataset:
Insert node into the middle of the linked list | head of list ; Node Class ; function to insert node at the middle of the linked list ; if list is empty ; get a new node ; assign values to the slow and fast pointers ; move slow pointer to next node ; move fast pointer two nodes at a time ; insert the ' newNode ' and adjust the required links ; function to display the linked list ; Creating the list 1.2 . 4.5
var head ; class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function insertAtMid ( x ) { if ( head == null ) head = new Node ( x ) ; else { var newNode = new Node ( x ) ; var slow = head ; var fast = head . next ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } newNode . next = slow . next ; slow . next = newNode ; } } function display ( ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } head = null ; head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 4 ) ; head . next . next . next = new Node ( 5 ) ; document . write ( " " + " " ) ; display ( ) ; var x = 3 ; insertAtMid ( x ) ; document . write ( " " + " " ) ; display ( ) ;
Insertion Sort for Doubly Linked List | Node of a doubly linked list ; function to create and return a new node of a doubly linked list ; allocate node ; put in the data ; function to insert a new node in sorted way in a sorted doubly linked list ; if list is empty ; if the node is to be inserted at the beginning of the doubly linked list ; locate the node after which the new node is to be inserted ; Make the appropriate links / ; if the new node is not inserted at the end of the list ; function to sort a doubly linked list using insertion sort ; Initialize ' sorted ' - a sorted doubly linked list ; Traverse the given doubly linked list and insert every node to ' sorted ' ; Store next for next iteration ; removing all the links so as to create ' current ' as a new node for insertion ; insert current in ' sorted ' doubly linked list ; Update current ; Update head_ref to povar to sorted doubly linked list ; function to prvar the doubly linked list ; function to insert a node at the beginning of the doubly linked list ; allocate node / ; put in the data / ; Make next of new node as head and previous as null / ; change prev of head node to new node / ; move the head to povar to the new node / ; start with the empty doubly linked list / ; insert the following data
class Node { constructor ( val ) { this . data = val ; this . prev = null ; this . next = null ; } } function getNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . prev = newNode . next = null ; return newNode ; } function sortedInsert ( head_ref , newNode ) { var current ; if ( head_ref == null ) head_ref = newNode ; else if ( ( head_ref ) . data >= newNode . data ) { newNode . next = head_ref ; newNode . next . prev = newNode ; head_ref = newNode ; } else { current = head_ref ; while ( current . next != null && current . next . data < newNode . data ) current = current . next ; newNode . next = current . next ; if ( current . next != null ) newNode . next . prev = newNode ; current . next = newNode ; newNode . prev = current ; } return head_ref ; } function insertionSort ( head_ref ) { var sorted = null ; var current = head_ref ; while ( current != null ) { var next = current . next ; current . prev = current . next = null ; sorted = sortedInsert ( sorted , current ) ; current = next ; } head_ref = sorted ; return head_ref ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; new_node . prev = null ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } var head = null ; head = push ( head , 9 ) ; head = push ( head , 3 ) ; head = push ( head , 5 ) ; head = push ( head , 10 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; document . write ( " " ) ; printList ( head ) ; head = insertionSort ( head ) ; document . write ( " " ) ; printList ( head ) ;
Print all possible rotations of a given Array | Global declaration of array ; Function to reverse array between indices s and e ; Function to generate all possible rotations of array ; Driver code
arr = Array . from ( { length : 10000 } , ( _ , i ) => 0 ) ; function reverse ( arr , s , e ) { while ( s < e ) { var tem = arr [ s ] ; arr [ s ] = arr [ e ] ; arr [ e ] = tem ; s = s + 1 ; e = e - 1 ; } } function fun ( arr , k ) { var n = 4 - 1 ; var v = n - k ; if ( v >= 0 ) { reverse ( arr , 0 , v ) ; reverse ( arr , v + 1 , n ) ; reverse ( arr , 0 , n ) ; } } arr [ 0 ] = 1 ; arr [ 1 ] = 2 ; arr [ 2 ] = 3 ; arr [ 3 ] = 4 ; for ( i = 0 ; i < 4 ; i ++ ) { fun ( arr , i ) ; document . write ( " " ) ; for ( j = 0 ; j < 4 ; j ++ ) { document . write ( arr [ j ] + " " ) ; } document . write ( " " ) ; }
Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts | javascript program to find Bitwise OR of two equal halves of an array after performing K right circular shifts ; Array for storing the segment tree ; Function to build the segment tree ; Function to return the OR of elements in the range [ l , r ] ; Check for out of bound condition ; Find middle of the range ; Recurse for all the elements in array ; Function to find the OR sum ; Function to build the segment Tree ; Loop to handle q queries ; Effective number of right circular shifts ; OR of second half of the array [ n / 2 - i , n - 1 - i ] ; OR of first half of the array [ n - i , n - 1 ] OR [ 0 , n / 2 - 1 - i ] ; Prvar final answer to the query ; Driver Code
const MAX = 100005 ; var seg = Array ( 4 * MAX ) . fill ( 0 ) ; function build ( node , l , r , a ) { if ( l == r ) seg [ node ] = a [ l ] ; else { var mid = parseInt ( ( l + r ) / 2 ) ; build ( 2 * node , l , mid , a ) ; build ( 2 * node + 1 , mid + 1 , r , a ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } } function query ( node , l , r , start , end , a ) { if ( l > end r < start ) return 0 ; if ( start <= l && r <= end ) return seg [ node ] ; var mid = parseInt ( ( l + r ) / 2 ) ; return ( ( query ( 2 * node , l , mid , start , end , a ) ) | ( query ( 2 * node + 1 , mid + 1 , r , start , end , a ) ) ) ; } function orsum ( a , n , q , k ) { build ( 1 , 0 , n - 1 , a ) ; for ( j = 0 ; j < q ; j ++ ) { var i = k [ j ] % ( n / 2 ) ; var sec = query ( 1 , 0 , n - 1 , n / 2 - i , n - i - 1 , a ) ; var first = ( query ( 1 , 0 , n - 1 , 0 , n / 2 - 1 - i , a ) | query ( 1 , 0 , n - 1 , n - i , n - 1 , a ) ) ; var temp = sec + first ; document . write ( temp + " " ) ; } } var a = [ 7 , 44 , 19 , 86 , 65 , 39 , 75 , 101 ] ; var n = a . length ; var q = 2 ; var k = [ 4 , 2 ] ; orsum ( a , n , q , k ) ;
Maximize count of corresponding same elements in given Arrays by Rotation | Function that prlets maximum equal elements ; Vector to store the index of elements of array b ; Storing the positions of array B ; frequency array to keep count of elements with similar difference in distances ; Iterate through all element in arr1 [ ] ; Calculate number of shift required to make current element equal ; If d is less than 0 ; Store the frequency of current diff ; Compute the maximum frequency stored ; Printing the maximum number of equal elements ; Given two arrays ; Function Call
function maximumEqual ( a , b , n ) { let store = Array . from ( { length : 1e5 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { store [ b [ i ] ] = i + 1 ; } let ans = Array . from ( { length : 1e5 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { let d = Math . abs ( store [ a [ i ] ] - ( i + 1 ) ) ; if ( store [ a [ i ] ] < i + 1 ) { d = n - d ; } ans [ d ] ++ ; } let finalans = 0 ; for ( let i = 0 ; i < 1e5 ; i ++ ) finalans = Math . max ( finalans , ans [ i ] ) ; document . write ( finalans + " " ) ; } let A = [ 6 , 7 , 3 , 9 , 5 ] ; let B = [ 7 , 3 , 9 , 5 , 6 ] ; let size = A . length ; maximumEqual ( A , B , size ) ;
Print array after it is right rotated K times | Function to rightRotate array ; If rotation is greater than size of array ; Printing rightmost kth elements ; Prints array after ' k ' elements ; Driver code
function RightRotate ( a , n , k ) { k = k % n ; for ( let i = 0 ; i < n ; i ++ ) { if ( i < k ) { document . write ( a [ n + i - k ] + " " ) ; } else { document . write ( ( a [ i - k ] ) + " " ) ; } } document . write ( " " ) ; } let Array = [ 1 , 2 , 3 , 4 , 5 ] ; let N = Array . length ; let K = 2 ; RightRotate ( Array , N , K ) ;
Sort a Rotated Sorted Array | Function to restore the Original Sort ; In reverse ( ) , the first parameter is iterator to beginning element and second parameter is iterator to last element plus one . ; Function to print the Array ; Driver Code
function restoreSortedArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { reverse ( arr , 0 , i ) ; reverse ( arr , i + 1 , n ) ; reverse ( arr , 0 , n ) ; } } } function reverse ( arr , i , j ) { let temp ; while ( i < j ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 3 , 4 , 5 , 1 , 2 ] ; let n = arr . length ; restoreSortedArray ( arr , n - 1 ) ; printArray ( arr , n )
Sort a Rotated Sorted Array | Function to find start index of array ; Function to restore the Original Sort ; Array is already sorted ; In reverse ( ) , the first parameter is iterator to beginning element and second parameter is iterator to last element plus one . ; Function to print the Array ; Driver code
function findStartIndexOfArray ( arr , low , high ) { if ( low > high ) { return - 1 ; } if ( low == high ) { return low ; } let mid = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( arr [ mid ] > arr [ mid + 1 ] ) { return mid + 1 ; } if ( arr [ mid - 1 ] > arr [ mid ] ) { return mid ; } if ( arr [ low ] > arr [ mid ] ) { return findStartIndexOfArray ( arr , low , mid - 1 ) ; } else { return findStartIndexOfArray ( arr , mid + 1 , high ) ; } } function restoreSortedArray ( arr , n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) { return ; } let start = findStartIndexOfArray ( arr , 0 , n - 1 ) ; arr . sort ( ) ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; restoreSortedArray ( arr , n ) ; printArray ( arr , n ) ;
Left Rotation and Right Rotation of a String | Function that rotates s towards left by d ; Function that rotates s towards right by d ; Driver code
function leftrotate ( str , d ) { var ans = str . substring ( d , str . length ) + str . substring ( 0 , d ) ; return ans ; } function rightrotate ( str , d ) { return leftrotate ( str , str . length - d ) ; } var str1 = " " ; document . write ( leftrotate ( str1 , 2 ) + " " ) ; var str2 = " " ; document . write ( rightrotate ( str2 , 2 ) + " " ) ;
Search an Element in Doubly Circular Linked List | Structure of a Node ; Function to insert a node at the end ; If the list is empty , create a single node circular and doubly list ; Find last node / ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Function to display the circular doubly linked list ; Function to search the particular element from the list ; Declare the temp variable ; Declare other control variable for the searching ; If start is null return - 1 ; Move the temp pointer until , temp . next doesn 't move  start address (Circular Fashion) ; Increment count for location ; If it is found raise the flag and break the loop ; Increment temp pointer ; Check whether last element in the list content the value if contain , raise a flag and increment count ; If flag is true , then element found , else not ; Start with the empty list / ; Insert 4. So linked list becomes 4. null ; Insert 5. So linked list becomes 4.5 ; Insert 7. So linked list becomes 4.5 . 7 ; Insert 8. So linked list becomes 4.5 . 7.8 ; Insert 6. So linked list becomes 4.5 . 7.8 . 6
class Node { constructor ( ) { this . data = 0 ; this . next = this . prev = null ; } } function insertNode ( start , value ) { if ( start == null ) { let new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return new_node ; } let last = ( start ) . prev ; let new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return start ; } function displayList ( start ) { let temp = start ; while ( temp . next != start ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data + " " ) ; } function searchList ( start , search ) { let temp = start ; let count = 0 , flag = 0 , value ; if ( temp == null ) return - 1 ; else { while ( temp . next != start ) { count ++ ; if ( temp . data == search ) { flag = 1 ; count -- ; break ; } temp = temp . next ; } if ( temp . data == search ) { count ++ ; flag = 1 ; } if ( flag == 1 ) document . write ( " " + search + " " + count ) ; else document . write ( " " + search + " " ) ; } return - 1 ; } let start = null ; start = insertNode ( start , 4 ) ; start = insertNode ( start , 5 ) ; start = insertNode ( start , 7 ) ; start = insertNode ( start , 8 ) ; start = insertNode ( start , 6 ) ; document . write ( " " ) ; displayList ( start ) ; searchList ( start , 5 ) ;
Reverse a doubly circular linked list | structure of a node of linked list ; function to create and return a new node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node / ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Uitlity function to revesre a doubly circular linked list ; Initialize a new head pointer ; get pointer to the the last node ; set ' curr ' to last node ; traverse list in backward direction ; insert ' curr ' at the end of the list starting with the ' new _ head ' pointer ; head pointer of the reversed list ; function to display a doubly circular list in forward and backward direction ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function getNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; return newNode ; } function insertEnd ( head , new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } var last = head . prev ; new_node . next = head ; head . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } function reverse ( head ) { if ( head == null ) return null ; var new_head = null ; var last = head . prev ; var curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } function display ( head ) { if ( head == null ) return ; var temp = head ; document . write ( " " ) ; while ( temp . next != head ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data + " " ) ; var last = head . prev ; temp = last ; document . write ( " " ) ; while ( temp . prev != last ) { document . write ( temp . data + " " ) ; temp = temp . prev ; } document . write ( temp . data + " " ) ; } var head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; document . write ( " " ) ; display ( head ) ; head = reverse ( head ) ; document . write ( " " ) ; display ( head ) ;
Sqrt ( or Square Root ) Decomposition | Set 2 ( LCA of Tree in O ( sqrt ( height ) ) time ) | Naive Javascrit implementation to find LCA in a tree . ; Stores depth for each node ; Stores first parent for each node ; Marking parent for each node ; Marking depth for each node ; Propogating marking down the tree ; A dummy node ; Precalclating 1 ) depth . 2 ) parent . for each node ; Time Complexity : O ( Height of tree ) recursively jumps one node above till both the nodes become equal ; Driver Code ; Adding edges to the tree
var MAXN = 1001 ; var depth = Array ( MAXN ) ; var parent = Array ( MAXN ) ; var adj = Array . from ( Array ( MAXN ) , ( ) => Array ( ) ) ; function addEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function dfs ( cur , prev ) { parent [ cur ] = prev ; depth [ cur ] = depth [ prev ] + 1 ; for ( var i = 0 ; i < adj [ cur ] . length ; i ++ ) if ( adj [ cur ] [ i ] != prev ) dfs ( adj [ cur ] [ i ] , cur ) ; } function preprocess ( ) { depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } function LCANaive ( u , v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { var temp = u ; u = v ; v = temp ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } for ( var i = 0 ; i < MAXN ; i ++ ) adj [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; preprocess ( ) ; document . write ( " " + LCANaive ( 11 , 8 ) + " " ) ; document . write ( " " + LCANaive ( 3 , 13 ) ) ;
Sqrt ( or Square Root ) Decomposition | Set 2 ( LCA of Tree in O ( sqrt ( height ) ) time ) | Javascript program to find LCA using Sqrt decomposition ; Block size = Math . sqrt ( height ) ; Stores depth for each node ; Stores first parent for each node ; Stores first ancestor in previous block ; Precalculating the required parameters associated with every node ; Marking depth of cur node ; Marking parent of cur node ; Making jump_parent of cur node ; If it is first node of the block then its jump_parent is its cur parent ; If it is not the first node of this block then its jump_parent is jump_parent of its parent ; Propogating the marking down the subtree ; Using sqrt decomposition trick ; Maintaining depth [ v ] > depth [ u ] ; Climb to its jump parent ; u and v have same jump_parent ; Precalclating 1 ) depth . 2 ) parent . 3 ) jump_parent for each node ; Driver code ; Adding edges to the tree ; Here we are directly taking height = 4 according to the given tree but we can pre - calculate height = max depth in one more dfs
let MAXN = 1001 ; let block_sz ; let depth = new Array ( MAXN ) ; let parent = new Array ( MAXN ) ; let jump_parent = new Array ( MAXN ) ; let adj = new Array ( MAXN ) ; function addEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function LCANaive ( u , v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { let t = u ; u = v ; v = t ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } function dfs ( cur , prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] = prev ; if ( depth [ cur ] % block_sz == 0 ) jump_parent [ cur ] = parent [ cur ] ; else jump_parent [ cur ] = jump_parent [ prev ] ; for ( let i = 0 ; i < adj [ cur ] . length ; ++ i ) if ( adj [ cur ] [ i ] != prev ) dfs ( adj [ cur ] [ i ] , cur ) ; } function LCASQRT ( u , v ) { while ( jump_parent [ u ] != jump_parent [ v ] ) { if ( depth [ u ] > depth [ v ] ) { let t = u ; u = v ; v = t ; } v = jump_parent [ v ] ; } return LCANaive ( u , v ) ; } function preprocess ( height ) { block_sz = parseInt ( Math . sqrt ( height ) , 10 ) ; depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; let height = 4 ; preprocess ( height ) ; document . write ( " " + LCASQRT ( 11 , 8 ) + " " ) ; document . write ( " " + LCASQRT ( 3 , 13 ) + " " ) ;
Expected Number of Trials to get N Consecutive Heads | Driver Code ; Formula for number of trails for N consecutive heads
let N = 3 ; document . write ( Math . pow ( 2 , N + 1 ) - 2 ) ;
Find and Count total factors of co | Function to return the count of numbers which are divisible by both A and B in the range [ 1 , N ] in constant time ; Compute the count of numbers divisible by A in the range [ 1 , N ] ; Compute the count of numbers divisible by B in the range [ 1 , N ] ; Adding the counts which are divisible by A and B ; The above value might contain repeated values which are divisible by both A and B . Therefore , the count of numbers which are divisible by both A and B are found ; The count computed above is subtracted to compute the final count ; Function to return the sum of numbers which are divisible by both A and B in the range [ 1 , N ] ; Set to store the numbers so that the numbers are not repeated ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to iterate through the set and find the sum ; Driver code
function countOfNum ( n , a , b ) { let cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = Math . floor ( n / a ) ; cnt_of_b = Math . floor ( n / b ) ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = Math . floor ( n / ( a * b ) ) ; sum = sum - cnt_of_ab ; return sum ; } function sumOfNum ( n , a , b ) { let i ; let sum = 0 ; let ans = new Set ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . add ( i ) ; } for ( let it of ans . values ( ) ) { sum = sum + it ; } return sum ; } let N = 88 ; let A = 11 ; let B = 8 ; let count = countOfNum ( N , A , B ) ; let sumofnum = sumOfNum ( N , A , B ) ; document . write ( sumofnum % count ) ;
Find Range Value of the Expression | Function to return the value of the given expression ; Value of the first term ; Value of the last term ; Driver code ; Get the result
function get ( L , R ) { let x = 1.0 / L ; let y = 1.0 / ( R + 1.0 ) ; return ( x - y ) ; } let L = 6 , R = 12 ; let ans = get ( L , R ) ; document . write ( Math . round ( ans * 100 ) / 100 ) ;
Find next greater element with no consecutive 1 in it 's binary representation | JavaScript implementation of the approach ; To store the pre - computed integers ; Function that returns true if the binary representation of x contains consecutive 1 s ; To store the previous bit ; Check whether the previous bit and the current bit are both 1 ; Update previous bit ; Go to the next bit ; Function to pre - compute the valid numbers from 0 to MAX ; Store all the numbers which do not have consecutive 1 s ; Function to return the minimum number greater than n which does not contain consecutive 1 s ; Search for the next greater element with no consecutive 1 s ; Function to perform the queries ; Driver code ; Pre - compute the numbers ; Perform the queries
const MAX = 100000 ; let v = [ ] ; function upper_bound ( ar , k ) { let s = 0 ; let e = ar . length ; while ( s != e ) { let mid = s + e >> 1 ; if ( ar [ mid ] <= k ) { s = mid + 1 ; } else { e = mid ; } } if ( s == ar . length ) { return - 1 ; } return s ; } function consecutiveOnes ( x ) { let p = 0 ; while ( x > 0 ) { if ( x % 2 == 1 && p == 1 ) return true ; p = x % 2 ; x = parseInt ( x / 2 ) ; } return false ; } function preCompute ( ) { for ( let i = 0 ; i <= MAX ; i ++ ) { if ( ! consecutiveOnes ( i ) ) v . push ( i ) ; } } function nextValid ( n ) { let it = upper_bound ( v , n ) ; let val = v [ it ] ; return val ; } function performQueries ( queries , q ) { for ( let i = 0 ; i < q ; i ++ ) document . write ( nextValid ( queries [ i ] ) + " " ) ; } let queries = [ 4 , 6 ] ; let q = queries . length ; preCompute ( ) ; performQueries ( queries , q ) ;