codeparrot/xlcost-text-to-code huggingface dataset:
Swap bits in a given number |  ; Setting bit at p1 position to 1 ; Setting bit at p2 position to 1 ; value1 and value2 will have 0 if num at the respective positions - p1 and p2 is 0. ; Check if value1 and value2 are different i . e . at one position bit is set and other it is not ; If bit at p1 position is set ; Unset bit at p1 position ; Set bit at p2 position ; If bit at p2 position is set ; Set bit at p2 position ; Unset bit at p2 position ; Return final result ; Driver code
function swapBits ( num , p1 , p2 , n ) { let shift1 , shift2 , value1 , value2 ; while ( n -- > 0 ) { shift1 = 1 << p1 ; shift2 = 1 << p2 ; value1 = ( ( num & shift1 ) ) ; value2 = ( ( num & shift2 ) ) ; if ( ( value1 == 0 && value2 != 0 ) || ( value2 == 0 && value1 != 0 ) ) { if ( value1 != 0 ) { num = num & ( ~ shift1 ) ; num = num | shift2 ; } else { num = num & ( ~ shift2 ) ; num = num | shift1 ; } } p1 ++ ; p2 ++ ; } return num ; } let res = swapBits ( 28 , 0 , 3 , 2 ) ; document . write ( " " + res ) ;
Smallest of three integers without comparison operators | JavaScript program to find Smallest of three integers without comparison operators ; Driver Code
function smallest ( x , y , z ) { let c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } let x = 12 , y = 15 , z = 5 ; document . write ( " " + smallest ( x , y , z ) ) ;
Smallest of three integers without comparison operators |  ; Function to find minimum of x and y ; Function to find minimum of 3 numbers x , y and z ; Driver code
let CHAR_BIT = 8 ; function min ( x , y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( 32 * CHAR_BIT - 1 ) ) ) } function smallest ( x , y , z ) { return Math . min ( x , Math . min ( y , z ) ) ; } let x = 12 , y = 15 , z = 5 ; document . write ( " " + smallest ( x , y , z ) ) ;
Smallest of three integers without comparison operators | Using division operator to find minimum of three numbers ; Same as " if ▁ ( y ▁ < ▁ x ) " ; Driver code
function smallest ( x , y , z ) { if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ; return ( ! ( x / z ) ) ? x : z ; } let x = 78 , y = 88 , z = 68 ; document . write ( " " + smallest ( x , y , z ) ) ;
A Boolean Array Puzzle |  ; Driver code
function changeToZero ( a ) { a [ a [ 1 ] ] = a [ 1 - a [ 1 ] ] ; } let arr ; arr = [ ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 0 ; changeToZero ( arr ) ; document . write ( " " + arr [ 0 ] + " " ) ; document . write ( " " + arr [ 1 ] ) ;
Next higher number with same number of set bits | this function returns next higher number with same number of set bits as x . ; right most set bit ; reset the pattern and set next higher bit left part of x will be here ; nextHigherOneBit is now part [ D ] of the above explanation . isolate the pattern ; right adjust pattern ; correction factor ; rightOnesPattern is now part [ A ] of the above explanation . integrate new pattern ( Add [ D ] and [ A ] ) ; Driver code
function snoob ( x ) { let rightOne , nextHigherOneBit , rightOnesPattern , next = 0 ; if ( x > 0 ) { rightOne = x & - x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; rightOnesPattern >>= 2 ; next = nextHigherOneBit | rightOnesPattern ; } return next ; } let x = 156 ; document . write ( " " + " " + snoob ( x ) ) ;
Add 1 to a given number | JavaScript code to add add one to a given number ; Flip all the set bits until we find a 0 ; flip the rightmost 0 bit ; Driver program to test above functions
function addOne ( x ) { let m = 1 ; while ( x & m ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } document . write ( addOne ( 13 ) ) ;
Add 1 to a given number | JavaScript program for the above approach ; Driver Code
function addOne ( x ) { return ( - ( ~ x ) ) ; } document . write ( addOne ( 13 ) ) ;
Multiply a given Integer with 3.5 | javascript Program to multiply a number with 3.5 ; Driver program to test above functions
function multiplyWith3Point5 ( x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } var x = 4 ; document . write ( multiplyWith3Point5 ( x ) ) ;
Multiply a given Integer with 3.5 | Function to multiple number with 3.5 ; The 3.5 is 7 / 2 , so multiply by 7 ( x * 7 ) then divide the result by 2 ( result / 2 ) x * 7 -> 7 is 0111 so by doing mutiply by 7 it means we do 2 shifting for the number but since we doing multiply we need to take care of carry one . ; Then divide by 2 r / 2 ; Driver code
function multiplyWith3Polet5 ( x ) { let r = 0 ; let x1Shift = x << 1 ; let x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; let c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; let t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } document . write ( multiplyWith3Polet5 ( 5 ) ) ;
Turn off the rightmost set bit | unsets the rightmost set bit of n and returns the result ; Driver Code
function fun ( n ) { return n & ( n - 1 ) ; } let n = 7 ; document . write ( " " + " " + fun ( n ) ) ;
Find whether a given number is a power of 4 or not | Function to check if x is power of 4 ; Driver program to test above function
function isPowerOfFour ( n ) { if ( n == 0 ) return false ; while ( n != 1 ) { if ( n % 4 != 0 ) return false ; n = n / 4 ; } return true ; } let test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;
Find whether a given number is a power of 4 or not | Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver code
function isPowerOfFour ( n ) { let count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } let test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;
Find whether a given number is a power of 4 or not | C ++ program to check if given number is power of 4 or not ; Driver code
function isPowerOfFour ( n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ; } test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;
Find whether a given number is a power of 4 or not | javascript program to check if given number is power of 4 or not ; 0 is not considered as a power of 4 ; Driver code
function logn ( n , r ) { return Math . log ( n ) / Math . log ( r ) ; } function isPowerOfFour ( n ) { if ( n == 0 ) return false ; return Math . floor ( logn ( n , 4 ) ) == Math . ceil ( logn ( n , 4 ) ) ; } let test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;
Find whether a given number is a power of 4 or not | Javascript program to check if given number is power of 4 or not ; Driver code
function isPowerOfFour ( n ) { return ( n > 0 && Math . pow ( 4 , ( Math . log2 ( n ) / Math . log2 ( 4 ) ) == n ) ) ; } let test_no = 64 ; if ( isPowerOfFour ( test_no ) ) document . write ( test_no + " " ) ; else document . write ( test_no + " " ) ;
Compute the minimum or maximum of two integers without branching | Function to find minimum of x and y ; Function to find maximum of x and y ; Driver program to test above functions
function min ( x , y ) { return y ^ ( ( x ^ y ) & - ( x << y ) ) ; } function max ( x , y ) { return x ^ ( ( x ^ y ) & - ( x << y ) ) ; } let x = 15 let y = 6 document . write ( " " + x + " " + y + " " ) ; document . write ( min ( x , y ) + " " ) ; document . write ( " " + x + " " + y + " " ) ; document . write ( max ( x , y ) + " " ) ;
Compute the minimum or maximum of two integers without branching | javascript implementation of above approach ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
var CHAR_BIT = 4 ; var INT_BIT = 8 ; function min ( x , y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } function max ( x , y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } var x = 15 ; var y = 6 ; document . write ( " " + x + " " + y + " " + min ( x , y ) + " " ) ; document . write ( " " + x + " " + y + " " + max ( x , y ) ) ;
Compute the minimum or maximum of two integers without branching | absbit32 function ; max function ; min function ; Driver code
function absbit32 ( x , y ) { var sub = x - y ; var mask = ( sub >> 31 ) ; return ( sub ^ mask ) - mask ; } function max ( x , y ) { var abs = absbit32 ( x , y ) ; return ( x + y + abs ) / 2 ; } function min ( x , y ) { var abs = absbit32 ( x , y ) ; return ( x + y - abs ) / 2 ; } document . write ( max ( 2 , 3 ) + " " ) ; document . write ( max ( 2 , - 3 ) + " " ) ; document . write ( max ( - 2 , - 3 ) + " " ) ; document . write ( min ( 2 , 3 ) + " " ) ; document . write ( min ( 2 , - 3 ) + " " ) ; document . write ( min ( - 2 , - 3 ) ) ;
Find the two non | This function sets the values of * x and * y to non - repeating elements in an array arr [ ] of size n ; Xor all the elements of the array all the elements occuring twice will cancel out each other remaining two unnique numbers will be xored ; Bitwise & the sum with it ' s ▁ 2' s Complement Bitwise & will give us the sum containing only the rightmost set bit ; sum1 and sum2 will contains 2 unique elements elements initialized with 0 box number xored with 0 is number itself ; Traversing the array again ; Bitwise & the arr [ i ] with the sum Two possibilities either result == 0 or result > 0 ; If result > 0 then arr [ i ] xored with the sum1 ; If result == 0 then arr [ i ] xored with sum2 ; Print the the two unique numbers ; Driver Code
function UniqueNumbers2 ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; let sum1 = 0 ; let sum2 = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } document . write ( " " + " " + sum1 + " " + sum2 ) ; } let arr = [ 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 ] ; let n = arr . length ; UniqueNumbers2 ( arr , n ) ;
Find the Number Occurring Odd Number of Times | Function to find the element occurring odd number of times ; driver code ; Function calling
function getOddOccurrence ( arr , arr_size ) { for ( let i = 0 ; i < arr_size ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] ; let n = arr . length ; document . write ( getOddOccurrence ( arr , n ) ) ;
Find the Number Occurring Odd Number of Times | function to find the element occurring odd number of times ; Putting all elements into the HashMap ; If array element is already present then increase the count of that element . ; if array element is not present then put element into the HashMap and initialize the count to one . ; Checking for odd occurrence of each element present in the HashMap ; driver code
function getOddOccurrence ( arr , n ) { let hmap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hmap . has ( arr [ i ] ) ) { let val = hmap . get ( arr [ i ] ) ; hmap . set ( arr [ i ] , val + 1 ) ; } else { hmap . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of hmap . entries ( ) ) { if ( hmap . get ( key ) % 2 != 0 ) return key ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] ; let n = arr . length ; document . write ( getOddOccurrence ( arr , n ) ) ;
Count number of bits to be flipped to convert A to B | Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code
function countSetBits ( n ) { var count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } function FlippedCount ( a , b ) { return countSetBits ( a ^ b ) ; } var a = 10 ; var b = 20 ; document . write ( FlippedCount ( a , b ) ) ;
Position of rightmost set bit | JavaScript program for Position of rightmost set bit ; Driver code
function getFirstSetBitPos ( n ) { return Math . log2 ( n & - n ) + 1 ; } let g ; let n = 12 ; document . write ( getFirstSetBitPos ( n ) ) ;
Position of rightmost set bit | function to find the rightmost set bit ; Position variable initialize with 1 m variable is used to check the set bit ; left shift ; Driver Code ; function call
function PositionRightmostSetbit ( n ) { let position = 1 ; let m = 1 ; while ( ( n & m ) == 0 ) { m = m << 1 ; position ++ ; } return position ; } let n = 16 ; document . write ( PositionRightmostSetbit ( n ) ) ;
Position of rightmost set bit | Javascript implementation of above approach ; Counting the position of first set bit ; Driver code
let INT_SIZE = 32 ; function Right_most_setbit ( num ) { let pos = 1 ; for ( let i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } let num = 18 ; let pos = Right_most_setbit ( num ) ; document . write ( pos ) ;
Position of rightmost set bit | Program to find position of rightmost set bit ; Iterate till number > 0 ; Checking if last bit is set ; Increment position and right shift number ; Set bit not found . ; Driver code ; Function call
function Last_set_bit ( n ) { let p = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) { return p ; } p ++ ; n = n >> 1 ; } return - 1 ; } let n = 18 ; let pos = Last_set_bit ( n ) ; if ( pos != - 1 ) { document . write ( pos ) ; } else { document . write ( 0 ) ; }
Binary representation of a given number | bin function ; Driver Code
function bin ( n ) { let i ; document . write ( " " ) ; for ( i = 1 << 30 ; i > 0 ; i = Math . floor ( i / 2 ) ) { if ( ( n & i ) != 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } bin ( 7 ) ; document . write ( " " ) ; bin ( 4 ) ;
Binary representation of a given number | Function to convert decimal to binary number ; Driver code
function bin ( n ) { if ( n > 1 ) bin ( n >> 1 ) ; document . write ( n & 1 ) ; } bin ( 131 ) ; document . write ( " " ) ; bin ( 3 ) ;
Swap all odd and even bits | Function to swap even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; 00010111 ; Output is 43 ( 00101011 )
function swapBits ( x ) { even_bits = x & 0xAAAAAAAA ; odd_bits = x & 0x55555555 ; even_bits >>= 1 ; odd_bits <<= 1 ; return ( even_bits odd_bits ) ; } let x = 23 ; document . write ( swapBits ( x ) ) ;
Find position of the only set bit | A utility function to check whether n is power of 2 or not . ; Returns position of the only set bit in ' n ' ; Iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; Unset current bit and set the next bit in ' i ' ; increment position ; Driver Code
function isPowerOfTwo ( n ) { return ( n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ) ? true : false ; } function findPosition ( n ) { if ( isPowerOfTwo ( n ) == false ) return - 1 ; var i = 1 ; var pos = 1 ; while ( ( i & n ) == 0 ) { i = i << 1 ; pos += 1 ; } return pos ; } var n = 16 ; var pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " + pos ) ; document . write ( " " ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " , pos ) ; document . write ( " " ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) document . write ( " " + n + " " ) ; else document . write ( " " + n + " " + pos ) ;
Find position of the only set bit | A utility function to check whether n is power of 2 or not ; Returns position of the only set bit in ' n ' ; One by one move the only set bit to right till it reaches end ; increment count of shifts ; Driver program to test above function
function isPowerOfTwo ( n ) { return ( n && ( ! ( n & ( n - 1 ) ) ) ) } function findPosition ( n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 var count = 0 while ( n ) { n = n >> 1 count += 1 } return count } var n = 0 var pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " ) n = 12 pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " ) n = 128 pos = findPosition ( n ) if ( pos == - 1 ) document . write ( " " , n , " " ) else document . write ( " " , n , " " , pos ) document . write ( " " )
How to swap two numbers without using a temporary variable ? | Javascript program to swap two numbers without using temporary variable ; Code to swap ' x ' and ' y ' x now becomes 50 ; y becomes 10 ; x becomes 5
var x = 10 ; var y = 5 ; x = x * y ; y = x / y ; x = x / y ; document . write ( " " + " " + x + " " + y ) ;
How to swap two numbers without using a temporary variable ? | Javascript code to swap using XOR ; Code to swap ' x ' ( 1010 ) and ' y ' ( 0101 ) x now becomes 15 ( 1111 ) ; y becomes 10 ( 1010 ) ; x becomes 5 ( 0101 )
let x = 10 , y = 5 ; x = x ^ y ; y = x ^ y ; x = x ^ y ; document . write ( " " + x + " " + y ) ;
How to swap two numbers without using a temporary variable ? | Swap function ; Driver code
function swap ( xp , yp ) { xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ] ; yp [ 0 ] = xp [ 0 ] ^ yp [ 0 ] ; xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ] ; } let x = [ 10 ] ; swap ( x , x ) ; document . write ( " " + x [ 0 ] ) ;
How to swap two numbers without using a temporary variable ? | Function to swap the numbers . ; same as a = a + b ; same as b = a - b ; same as a = a - b ; Driver Code ; Function Call
function swap ( a , b ) { a = ( a & b ) + ( a b ) ; b = a + ( ~ b ) + 1 ; a = a + ( ~ b ) + 1 ; document . write ( " " + a + " " + b ) ; } let a = 5 , b = 10 ; swap ( a , b ) ;
Check a given sentence for a given set of simple grammer rules | Method to check a given sentence for given rules ; Calculate the length of the string . ; Check that the first character lies in [ A - Z ] . Otherwise return false . ; If the last character is not a full stop ( . ) no need to check further . ; Maintain 2 states . Previous and current state based on which vertex state you are . Initialise both with 0 = start state . ; Keep the index to the next character in the string . ; Loop to go over the string . ; Set states according to the input characters in the string and the rule defined in the description . If current character is [ A - Z ] . Set current state as 0. ; If current character is a space . Set current state as 1. ; If current character is [ a - z ] . Set current state as 2. ; If current state is a dot ( . ) . Set current state as 3. ; Validates all current state with previous state for the rules in the description of the problem . ; If we have reached last state and previous state is not 1 , then check next character . If next character is ' \0' , then return true , else false ; Set previous state as current state before going over to the next character . ; Driver Code
function checkSentence ( str ) { var len = str . length ; if ( str [ 0 ] . charCodeAt ( 0 ) < " " . charCodeAt ( 0 ) || str [ 0 ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) ) return false ; if ( str [ len - 1 ] !== " " ) return false ; var prev_state = 0 , curr_state = 0 ; var index = 1 ; while ( index <= str . length ) { if ( str [ index ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ index ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) curr_state = 0 ; else if ( str [ index ] === " " ) curr_state = 1 ; else if ( str [ index ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ index ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) curr_state = 2 ; else if ( str [ index ] === " " ) curr_state = 3 ; if ( prev_state === curr_state && curr_state !== 2 ) return false ; if ( prev_state === 2 && curr_state === 0 ) return false ; if ( curr_state === 3 && prev_state !== 1 ) return index + 1 == str . length ; index ++ ; prev_state = curr_state ; } return false ; } var str = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , ] ; var str_size = str . length ; var i = 0 ; for ( i = 0 ; i < str_size ; i ++ ) { var temp = str [ i ] . split ( " " ) ; if ( checkSentence ( temp ) ) document . write ( ' ' + str [ i ] + ' ' + " " + " " ) ; else document . write ( ' ' + str [ i ] + ' ' + " " + " " ) ; }
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array | Returns index of 0 to be replaced with 1 to get longest continuous sequence of 1 s . If there is no 0 in array , then it returns - 1. ; for maximum number of 1 around a zero ; for storing result ; index of previous zero ; index of previous to previous zero ; Traverse the input array ; If current element is 0 , then calculate the difference between curr and prev_prev_zero ; Update result if count of 1 s around prev_zero is more ; Update for next iteration ; Check for the last encountered zero ; Driver code
function maxOnesIndex ( arr , n ) { let max_count = 0 ; let max_index = 0 ; let prev_zero = - 1 ; let prev_prev_zero = - 1 ; for ( let curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } let arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] ; let n = arr . length ; document . write ( " " + maxOnesIndex ( arr , n ) ) ;
Length of the largest subarray with contiguous elements | Set 1 | Utility functions to find minimum and maximum of two elements ; Returns length of the longest contiguous subarray ; Initialize result ; Initialize min and max for all subarrays starting with i ; Consider all subarrays starting with i and ending with j ; Update min and max in this subarray if needed ; If current subarray has all contiguous elements ; Return result ; driver code
function min ( x , y ) { return ( x < y ) ? x : y ; } function max ( x , y ) { return ( x > y ) ? x : y ; } function findLength ( arr , n ) { let max_len = 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let mn = arr [ i ] , mx = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { mn = min ( mn , arr [ j ] ) ; mx = max ( mx , arr [ j ] ) ; if ( ( mx - mn ) == j - i ) max_len = Math . max ( max_len , mx - mn + 1 ) ; } } return max_len ; } let arr = [ 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 ] ; let n = arr . length ; document . write ( " " + findLength ( arr , n ) ) ;
Print all increasing sequences of length k from first n natural numbers | A utility function to print contents of arr [ 0. . k - 1 ] ; A recursive function to print all increasing sequences of first n natural numbers . Every sequence should be length k . The array arr [ ] is used to store current sequence ; If length of current increasing sequence becomes k , print it ; Decide the starting number to put at current position : If length is 0 , then there are no previous elements in arr [ ] . So start putting new numbers with 1. If length is not 0 , then start from value of previous element plus 1. ; Increase length ; Put all numbers ( which are greater than the previous element ) at new position . ; This is important . The variable ' len ' is shared among all function calls in recursion tree . Its value must be brought back before next iteration of while loop ; This function prints all increasing sequences of first n natural numbers . The length of every sequence must be k . This function mainly uses printSeqUtil ( ) ; An array to store individual sequences ; Initial length of current sequence ; Driver code
function printArr ( arr , k ) { for ( let i = 0 ; i < k ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function printSeqUtil ( n , k , len , arr ) { if ( len == k ) { printArr ( arr , k ) ; return ; } let i = ( len == 0 ) ? 1 : arr [ len - 1 ] + 1 ; len ++ ; while ( i <= n ) { arr [ len - 1 ] = i ; printSeqUtil ( n , k , len , arr ) ; i ++ ; } len -- ; } function printSeq ( n , k ) { let arr = new Array ( k ) ; let len = 0 ; printSeqUtil ( n , k , len , arr ) ; } let k = 3 , n = 7 ; printSeq ( n , k ) ;