codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subarray which upon repetition gives the original array | Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
function printArray ( brr ) { for ( let it of brr ) { document . write ( it + ' ' ) ; } } function RepeatingSubarray ( arr , N ) { if ( N < 2 ) { document . write ( " " ) ; } let brr = [ ] ; brr . push ( arr [ 0 ] ) ; brr . push ( arr [ 1 ] ) ; for ( let i = 2 ; i < Math . floor ( N / 2 ) + 1 ; i ++ ) { if ( N % i == 0 ) { let a = false ; let n = brr . length ; let j = i ; while ( j < N ) { let K = j % i ; if ( arr [ j ] == brr [ K ] ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . push ( arr [ i ] ) ; } document . write ( " " ) ; return ; } let arr = [ 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 ] ; let N = arr . length ; RepeatingSubarray ( arr , N ) ;
Find all missing numbers from a given sorted array | Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Given array arr [ ] ; Function call
function prletMissingElements ( arr , N ) { let diff = arr [ 0 ] - 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { document . write ( ( i + diff ) + " " ) ; diff ++ ; } } } } let arr = [ 6 , 7 , 10 , 11 , 13 ] ; let N = arr . length ; prletMissingElements ( arr , N ) ;
Find all missing numbers from a given sorted array | Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Given array arr [ ] ; Function Call
function printMissingElements ( arr , N ) { let b = new Uint8Array ( arr [ N - 1 ] + 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( let i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { document . write ( i + " " ) ; } } } let arr = [ 6 , 7 , 10 , 11 , 13 ] ; let N = arr . length ; printMissingElements ( arr , N ) ;
Least common element in given two Arithmetic sequences | Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield true in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
function smallestCommon ( a , b , c , d ) { if ( a == c ) return a ; if ( a > c ) { swap ( a , c ) ; swap ( b , d ) ; } let first_term_diff = ( c - a ) ; let possible_y ; for ( possible_y = 0 ; possible_y < b ; possible_y ++ ) { if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) { break ; } } if ( possible_y != b ) { return c + possible_y * d ; } return - 1 ; } function swap ( x , y ) { let temp = x ; x = y ; y = temp ; } let A = 2 , B = 20 , C = 19 , D = 9 ; document . write ( smallestCommon ( A , B , C , D ) ) ;
Minimum number of flips with rotation to make binary string alternating | Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Given String ; Length of given string ; Function call
function MinimumFlips ( s , n ) { let a = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = ( s [ i ] == ' ' ? 1 : 0 ) ; } let oddone = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let evenone = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } let minimum = Math . min ( oddone [ n ] , evenone [ n ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } let S = " " ; let n = S . length ; document . write ( MinimumFlips ( S , n ) ) ;
Create a Graph by connecting divisors from N to M and find shortest path | Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To avoid duplicate printing of same element ; Print the factors from n ; Given N and M ; Function Call
function isprm ( n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } function shortestpath ( m , n ) { let mfactor = new Array ( ) let nfactor = new Array ( ) let fre = new Map ( ) ; mfactor . push ( m ) ; fre [ m ] = 1 ; while ( m != 1 ) { if ( isprm ( m ) ) { mfactor . push ( 1 ) ; fre [ 1 ] = 1 ; m = 1 ; } else { for ( let i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . push ( m / i ) ; fre [ m / i ] = 1 ; m = ( m / i ) ; break ; } } } } nfactor . push ( n ) ; while ( fre [ n ] != 1 ) { if ( isprm ( n ) ) { nfactor . push ( 1 ) ; n = 1 ; } else { for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . push ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( let i = 0 ; i < mfactor . length ; i ++ ) { if ( mfactor [ i ] == n ) break ; document . write ( mfactor [ i ] + " " ) ; } for ( let i = nfactor . length - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) document . write ( nfactor [ i ] ) ; else document . write ( nfactor [ i ] + " " ) ; } } let m = 18 , n = 19 ; shortestpath ( m , n ) ;
Possible number of Trees having N vertex | JavaScript program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the final count of trees ; Driver Code ; Given array arr [ ] ; Function call
let mod = ( 1e9 + 7 ) ; function NumberOfTrees ( arr , N ) { let maxElement = Math . max ( ... arr ) ; let level = Array . from ( { length : maxElement + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } let ans = 1 ; for ( let i = 0 ; i < maxElement ; i ++ ) { for ( let j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } let N = 7 ; let arr = [ 0 , 3 , 2 , 1 , 2 , 2 , 1 ] ; document . write ( NumberOfTrees ( arr , N ) ) ;
Possible number of Trees having N vertex | javascript program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
let mod = 1e9 + 7 ; function power ( x , y ) { if ( y == 0 ) return 1 ; let p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( y & 1 ) != 0 ) p = ( x * p ) % mod ; return p ; } function NumberOfTrees ( arr , N ) { let maxElement = Math . max ( ... arr ) ; let level = Array ( maxElement + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } let ans = 1 ; for ( let i = 0 ; i < maxElement ; i ++ ) { ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod ; } return ans ; } let N = 7 ; let arr = [ 0 , 3 , 2 , 1 , 2 , 2 , 1 ] ; document . write ( NumberOfTrees ( arr , N ) ) ;
A Binary String Game | Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Prlet the result ; Given string S ; Function Call
function gameMax ( S ) { let N = S . length ; let list = [ ] ; let one = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { one ++ ; } else { if ( one != 0 ) { list . push ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . push ( one ) ; } list . sort ( ) ; list . reverse ( ) ; let score_1 = 0 , score_2 = 0 ; for ( let i = 0 ; i < list . length ; i ++ ) { if ( list [ i ] % 2 == 1 ) { score_1 += list [ i ] ; } else { score_2 += list [ i ] ; } } if ( score_1 == score_2 ) return " " ; return ( score_1 > score_2 ) ? " " : " " ; } let S = " " ; document . write ( gameMax ( S ) ) ;
Count of carry operations on adding two Binary numbers | JavaScript program for the above approach ; Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to readonly count ; Return the readonly count ; Given two numbers ; Function call
function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function carryCount ( num1 , num2 ) { let count = 0 ; while ( num2 != 0 ) { let carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += countSetBits ( num2 ) ; } return count ; } let A = 15 , B = 10 ; document . write ( carryCount ( A , B ) ) ;
Count of all possible ways to reach a target by a Knight | Javascript Program to implement the above approach ; Function to return X ^ Y % Mod ; Base Case ; Function to return the inverse of factorial of N ; Base case ; Function to return factorial of n % Mod ; Base case ; Function to return the value of n ! / ( ( n - k ) ! * k ! ) ; Function to return the count of ways to reach ( n , m ) from ( 0 , 0 ) ; If ( N + M ) % 3 != 0 ; No possible way exists ; Calculate X and Y from the equations X + 2 Y = N and 2 X + Y == M ; Driver Code
var Mod = 1000000007 ; function power ( X , Y , Mod ) { if ( Y == 0 ) return 1 ; var p = power ( X , Y / 2 , Mod ) % Mod ; p = ( p * p ) % Mod ; if ( Y & 1 ) { p = ( X * p ) % Mod ; } return p ; } function Inversefactorial ( N ) { if ( N <= 0 ) return 1 ; var fact = 1 ; for ( var i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return power ( fact , Mod - 2 , Mod ) ; } function factorial ( N ) { if ( N <= 0 ) return 1 ; var fact = 1 ; for ( var i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return fact ; } function nck ( N , K ) { var factN = factorial ( N ) ; var inv = Inversefactorial ( K ) ; var invFact = Inversefactorial ( N - K ) ; return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod ; } function TotalWaYs ( N , M ) { if ( ( N + M ) % 3 != 0 ) return 0 ; var X = N - ( N + M ) / 3 ; var Y = M - ( N + M ) / 3 ; if ( X < 0 Y < 0 ) return 0 ; return nck ( X + Y , Y ) ; } var N = 3 , M = 3 ; document . write ( TotalWaYs ( N , M ) ) ;
Lead a life problem | Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Given days ; Given earnings ; Given cost ; Given energy e ; Function call
function calculateProfit ( n , earnings , cost , e ) { let profit = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let earning_per_day = 0 ; let daily_spent_food = 0 ; if ( i == ( n - 1 ) ) { earning_per_day = earnings [ i ] * e ; profit = profit + earning_per_day ; break ; } if ( cost [ i ] < earnings [ i ] ) { earning_per_day = earnings [ i ] * e ; daily_spent_food = cost [ i ] * e ; profit = profit + earning_per_day - daily_spent_food ; } } document . write ( profit ) ; } let n = 4 ; let earnings = [ 1 , 8 , 6 , 7 ] ; let cost = [ 1 , 3 , 4 , 1 ] ; let e = 5 ; calculateProfit ( n , earnings , cost , e ) ;
Check if N numbers with Even Sum can be selected from a given Array | Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
function checkEvenSum ( arr , N , size ) { var even_freq = 0 , odd_freq = 0 ; for ( var i = 0 ; i < size ; i ++ ) { if ( arr [ i ] & 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq & 1 ) { var taken = odd_freq - 1 ; var req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { var taken = odd_freq ; var req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } return false ; } var arr = [ 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 ] ; var size = arr . length ; var N = 5 ; if ( checkEvenSum ( arr , N , size ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum count of digits required to obtain given Sum | Function to print the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code
function mindigits ( n ) { if ( n % 9 == 0 ) { document . write ( Math . floor ( n / 9 ) + " " ) ; } else { document . write ( Math . floor ( n / 9 ) + 1 + " " ) ; } } let n1 = 24 ; let n2 = 18 ; mindigits ( n1 ) ; mindigits ( n2 ) ;
Count of all possible numbers not exceeding M having suffix N | Function to count the no . of digits of N ; Function to count all possible numbers having Suffix as N ; Difference of the A . P ; Count of the number of terms ; Driver Code
function digitsOf ( num ) { return num . toString ( ) . length ; } function count ( a , tn ) { let diff = Math . floor ( Math . pow ( 10 , digitsOf ( a ) ) ) ; return Math . floor ( ( tn - a ) / diff ) + 1 ; } let n = 25 , m = 4500 ; document . write ( count ( n , m ) ) ;
Minimum increment / decrement operations required on Array to satisfy given conditions | Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Given array arr ; Function Call
function minOperations ( a , N ) { var num_of_ops1 , num_of_ops2 , sum ; num_of_ops1 = num_of_ops2 = sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 0 && sum >= 0 ) { num_of_ops1 += ( 1 + Math . abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 1 && sum <= 0 ) { num_of_ops1 += ( 1 + Math . abs ( sum ) ) ; sum = 1 ; } } sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 1 && sum >= 0 ) { num_of_ops2 += ( 1 + Math . abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 0 && sum <= 0 ) { num_of_ops2 += ( 1 + Math . abs ( sum ) ) ; sum = 1 ; } } return Math . min ( num_of_ops1 , num_of_ops2 ) ; } var arr = [ 3 , - 4 , 5 , 0 , 1 ] ; var N = arr . length ; document . write ( minOperations ( arr , N ) ) ;
Check if all elements of a Circular Array can be made equal by increments of adjacent pairs | Function to check if all array elements can be made equal ; Stores the sum of even and odd array elements ; If index is odd ; Driver Code
function checkEquall ( arr , N ) { let sumEven = 0 , sumOdd = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) sumOdd += arr [ i ] ; else sumEven += arr [ i ] ; } if ( sumEven == sumOdd ) return true ; else return false ; } let arr = [ 2 , 7 , 3 , 5 , 7 ] ; let N = arr . length ; if ( checkEquall ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number can be expressed as sum of two Perfect powers | Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum boolean array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum leto perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Given number n ; Function call
function isSumOfPower ( n ) { let isSum = Array ( n + 1 ) . fill ( 0 ) ; let perfectPowers = [ ] ; perfectPowers . push ( 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( let i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . push ( i ) ; continue ; } for ( let j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( let i = 0 ; i < perfectPowers . length ; i ++ ) { isSum [ perfectPowers [ i ] ] = false ; } for ( let i = 0 ; i < perfectPowers . length ; i ++ ) { for ( let j = i ; j < perfectPowers . length ; j ++ ) { let sum = perfectPowers [ i ] + perfectPowers [ j ] ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } let n = 9 ; if ( isSumOfPower ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimum increments by index value required to obtain at least two equal Array elements | Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code
function update ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } function check ( arr , N ) { let f = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } function incrementCount ( arr , N ) { let min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } document . write ( min ) ; } let N = 3 ; let arr = [ 12 , 8 , 4 ] ; incrementCount ( arr , N ) ;
Rearrange array such that all even | Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
function checkPossible ( a ) { let even_no_count = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( a . length / 2 > even_no_count ) { document . write ( " " ) ; return ; } document . write ( " " ) ; let j = 0 ; for ( let i = 1 ; i < a . length ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < a . length && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( let i = 0 ; i < a . length ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] ; checkPossible ( arr ) ;
Largest number M having bit count of N such that difference between their OR and XOR value is maximized | Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Given number N ; Function call
function maxORminusXOR ( N ) { let MSB = Math . ceil ( Math . log ( N ) ) ; let M = 0 ; for ( let i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } let N = 10 ; document . write ( maxORminusXOR ( N ) ) ;
Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle | Function to find the number of equilateral triangle formed within another triangle ; Check for the valid condition ; Number of triangles having upward peak ; Number of inverted triangles ; Total no . of K sized triangle ; Given N and K ; Function Call
function No_of_Triangle ( N , K ) { if ( N < K ) return - 1 ; else { let Tri_up = 0 ; Tri_up = Math . floor ( ( ( N - K + 1 ) * ( N - K + 2 ) ) / 2 ) ; let Tri_down = 0 ; Tri_down = Math . floor ( ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) / 2 ) ; return Tri_up + Tri_down ; } } let N = 4 , K = 2 ; document . write ( No_of_Triangle ( N , K ) ) ;
Check if Array forms an increasing | Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Given array arr [ ] ; Function Call
function canMake ( n , ar ) { if ( n == 1 ) return true ; else { if ( ar [ 0 ] < ar [ 1 ] ) { let i = 1 ; while ( i < n && ar [ i - 1 ] < ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] > ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else if ( ar [ 0 ] > ar [ 1 ] ) { let i = 1 ; while ( i < n && ar [ i - 1 ] > ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] < ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else { for ( let i = 2 ; i < n ; i ++ ) { if ( ar [ i - 1 ] <= ar [ i ] ) return false ; } return true ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; if ( ! canMake ( n , arr ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }