codeparrot/xlcost-text-to-code huggingface dataset:
Program to check if N is a Centered nonadecagonal number | Function to check that the number is a Centered nonadecagonal number ; Condition to check if the number is a Centered nonadecagonal number ; Driver Code ; Function call
function isCenterednonadecagonal ( N ) { var n = ( 19 + Math . sqrt ( 152 * N + 209 ) ) / 38 ; return ( n - parseInt ( n ) ) == 0 ; } var n = 20 ; if ( isCenterednonadecagonal ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to check if N is a Centered Octadecagonal number | Function to check that the number is a Centered Octadecagonal number ; Implement the formula generated ; Condition to check if the number is a Centered Octadecagonal number ; Driver Code ; Function call
function isCenteredOctadecagonal ( N ) { let n = parseInt ( ( 9 + Math . sqrt ( 36 * N + 45 ) ) / 18 ) ; return ( n - parseInt ( n ) ) == 0 ; } let n = 19 ; if ( isCenteredOctadecagonal ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Logarithm tricks for Competitive Programming | Function to find the Kth root of the number ; Driver Code
function kthRoot ( n , k ) { return Math . pow ( k , ( 1.0 / k ) * ( Math . log ( n ) / Math . log ( k ) ) ) ; } var N = 8.0 ; var K = 3 ; document . write ( kthRoot ( N , K ) ) ;
Logarithm tricks for Competitive Programming | Function to check if the number is power of K ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; Driver Code
function isPower ( N , K ) { var res1 = Math . floor ( Math . log ( N ) / Math . log ( K ) ) ; var res2 = Math . log ( N ) / Math . log ( K ) ; return ( res1 == res2 ) ; } var N = 8 ; var K = 2 ; if ( isPower ( N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of subarrays which start and end with the same element | function to find total sub - array which start and end with same element ; initialize result with 0 ; array to count frequency of 1 to N ; update frequency of A [ i ] ; update result with sub - array contributed by number i ; prlet the result ; Driver Code
function cntArray ( A , N ) { let result = 0 ; let frequency = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { frequency [ A [ i ] ] ++ ; } for ( let i = 1 ; i <= N ; i ++ ) { let frequency_of_i = frequency [ i ] ; result += ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 ; } document . write ( result ) ; } let A = [ 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 ] ; let N = A . length ; cntArray ( A , N ) ;
Length of array pair formed where one contains all distinct elements and other all same elements | Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
function findMaxSize ( a , n ) { var frq = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; ++ i ) frq [ a [ i ] ] ++ ; var maxfrq = frq . reduce ( ( a , b ) => Math . max ( a , b ) ) var dist = n + 1 - frq . filter ( x => x == 0 ) . length ; var ans1 = Math . min ( maxfrq - 1 , dist ) ; var ans2 = Math . min ( maxfrq , dist - 1 ) ; var ans = Math . max ( ans1 , ans2 ) ; return ans ; } var arr = [ 4 , 2 , 4 , 1 , 4 , 3 , 4 ] ; var n = arr . length ; document . write ( findMaxSize ( arr , n ) ) ;
Longest subarray with all elements same | Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
function subarray ( arr , n ) { var ans = 1 , temp = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { ++ temp ; } else { ans = Math . max ( ans , temp ) ; temp = 1 ; } } ans = Math . max ( ans , temp ) ; return ans ; } var arr = [ 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 ] ; var n = arr . length ; document . write ( subarray ( arr , n ) ) ;
Min and max length subarray having adjacent element difference atmost K | Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; Right will move in right direction and compare difference between itself and element right to it ; If subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver code ; Function call to find maximum and minimum possible sub array
function findMaxMinSubArray ( arr , k , n ) { let min = n ; let max = 0 ; let left ; let right ; let tmp ; for ( let i = 0 ; i < n ; i ++ ) { tmp = 1 ; left = i ; while ( left - 1 >= 0 && Math . abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) { left -- ; tmp ++ ; } right = i ; while ( right + 1 <= n - 1 && Math . abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) { right ++ ; tmp ++ ; } if ( min > tmp ) min = tmp ; if ( max < tmp ) max = tmp ; } document . write ( min ) ; document . write ( " " ) ; document . write ( max ) ; } let arr = [ 1 , 2 , 5 , 6 , 7 ] ; let K = 2 ; let n = arr . length ; findMaxMinSubArray ( arr , K , n ) ;
Count of elements which is the sum of a subarray of the given Array | Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code
function countElement ( arr , n ) { let freq = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { let tmpsum = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( tmpsum <= n ) { ans += freq [ tmpsum ] ; freq [ tmpsum ] = 0 ; } } } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; let n = arr . length ; document . write ( countElement ( arr , n ) ) ;
Nth root of a number using log | Function to find the Kth root of the number using log function ; Driver Code
function kthRoot ( n , k ) { return Math . pow ( k , ( ( 1.0 / k ) * ( Math . log ( n ) / Math . log ( k ) ) ) ) ; } var n = 81 ; var k = 4 ; var x = kthRoot ( n , k ) document . write ( x . toFixed ( 6 ) ) ;
Make A , B and C equal by adding total value N to them | JavaScript program to distribute integer N among A , B , C such that they become equal ; Find maximum among the three elements ; Summation of three elements ; Check if difference is divisible by 3 ; Driver code
function distributeN ( A , B , C , n ) { let max1 = Math . max ( A , B ) ; let max2 = Math . max ( B , C ) ; let maximum = Math . max ( max1 , max2 ) ; let sum = A + B + C ; let p = ( 3 * maximum ) - sum ; let diff = n - p ; if ( diff < 0 diff % 3 ) document . write ( " " ) ; else document . write ( " " ) ; } let A = 10 , B = 20 ; let C = 15 , n = 14 ; distributeN ( A , B , C , n ) ;
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
function minimumSteps ( a , b , c ) { if ( a < 1 b < 1 ) return - 1 ; if ( a == 1 && b == 1 ) return c ; if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } return minimumSteps ( a - b , b , c + 1 ) ; } let a = 75 ; let b = 17 ; document . write ( minimumSteps ( a , b , 0 ) + " " ) ;
Longest Mountain Subarray | Function to find the longest mountain subarray ; If the size of array is less than 3 , the array won 't show mountain like  behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . So set the value of j to current index i . ; Checks if next element is less than current element ; Checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; This condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . If the current length is greater than the calculated length , then value of d is updated . ; Ignore if there is no increase or decrease in the value of the next element ; Checks and calculates the length if last element of the array is the last element of a mountain sub - array ; Driver Code
function LongestMountain ( a ) { let i = 0 , j = - 1 , k = - 1 , p = 0 , d = 0 , n = 0 ; if ( a . length < 3 ) return 0 ; for ( i = 0 ; i < a . length - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] ) { if ( k != - 1 ) { k = - 1 ; j = - 1 ; } if ( j == - 1 ) j = i ; } else { if ( a [ i + 1 ] < a [ i ] ) { if ( j != - 1 ) k = i + 1 ; if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } } else { k = - 1 ; j = - 1 ; } } } if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } return d ; } let a = [ 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 ] ; document . write ( LongestMountain ( a ) ) ;
Check whether an Array can be made 0 by splitting and merging repeatedly | Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
function solve ( A ) { let i , ctr = 0 ; for ( i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] % 2 ) { ctr ++ ; } } if ( ctr % 2 ) { return " " ; } else { return " " ; } } let arr = [ 9 , 17 ] ; document . write ( solve ( arr ) ) ;
Count of perfect squares of given length | Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function countSquares ( n ) { let cnt = 0 ; for ( let i = Math . pow ( 10 , ( n - 1 ) ) ; i < Math . pow ( 10 , n ) ; i ++ ) { if ( i != 0 && isPerfectSquare ( i ) ) cnt ++ ; } return cnt ; } let n = 3 ; document . write ( countSquares ( n ) ) ;
Count of perfect squares of given length | Function to return the count of n digit perfect squares ; Driver code
function countSquares ( n ) { let r = Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) ; let l = Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) ; return r - l ; } let n = 3 ; document . write ( countSquares ( n ) ) ;
Sum of alternating sign cubes of first N Natural numbers | Function to compute sum of the cubes with alternating sign ; Driver code
function summation ( N ) { var co = parseInt ( ( N + 1 ) / 2 ) ; var ce = parseInt ( ( N ) / 2 ) ; var se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) ; var so = ( co * co ) * ( 2 * ( ( co * co ) ) - 1 ) ; return so - se ; } var n = 3 ; document . write ( summation ( n ) ) ;
Program to check if N is a Star Number | Function to check that the number is a star number ; Condition to check if the number is a star number ; Driver Code ; Function call
function isStar ( N ) { let n = ( 6 + Math . sqrt ( 24 * N + 12 ) ) / 6 ; return ( n - parseInt ( n ) ) == 0 ; } let i = 13 ; if ( isStar ( i ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Smallest number to make Array sum at most K by dividing each element | Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver Code
function findMinDivisor ( arr , n , limit ) { let low = 0 , high = 1000000000 ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += Math . ceil ( arr [ i ] / mid ) ; } if ( sum <= limit ) high = mid ; else low = mid + 1 ; } return low ; } let arr = [ 2 , 3 , 4 , 9 ] ; let N = arr . length ; let K = 6 ; document . write ( findMinDivisor ( arr , N , K ) ) ;
Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; return the final result ; Driver Code
function getCount ( arr , n ) { var count = 0 ; for ( var j = 1 ; j < n - 1 ; j ++ ) { var p = 0 , q = 0 ; for ( var i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( var k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } var arr = [ 1 , 2 , 2 ] ; var N = arr . length ; document . write ( getCount ( arr , N ) ) ;
Find pair with maximum ratio in an Array | Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver Code
function computeMaxValue ( arr , n ) { var ans = 0 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { var val = Math . max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = Math . max ( ans , val ) ; } } return ans ; } var arr = [ 15 , 10 , 3 , 2 ] ; var n = arr . length ; document . write ( computeMaxValue ( arr , n ) ) ;
Find the Kth number which is not divisible by N | Function to find the Kth not divisible by N ; Driver code ; Function Call
function kthNonDivisible ( N , K ) { return K + parseInt ( Math . floor ( ( K - 1 ) / ( N - 1 ) ) , 10 ) ; } let N = 3 ; let K = 6 ; document . write ( kthNonDivisible ( N , K ) ) ;
Find the maximum sum pair in an Array with even parity | Javascript implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to prlet the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function Call
let sz = 1e3 ; function isEvenParity ( x ) { let parity = 0 ; while ( x != 0 ) { if ( x % 2 == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } function prletArray ( arr , len ) { for ( let i = 0 ; i < len ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function findPairEvenParity ( arr , len ) { let firstMaximum = Number . MIN_VALUE ; let secondMaximum = Number . MIN_VALUE ; for ( let i = 0 ; i < len ; i ++ ) { if ( isEvenParity ( arr [ i ] ) ) { if ( arr [ i ] >= firstMaximum ) { secondMaximum = firstMaximum ; firstMaximum = arr [ i ] ; } else if ( arr [ i ] >= secondMaximum ) { secondMaximum = arr [ i ] ; } } } document . write ( firstMaximum + " " + secondMaximum ) ; } let arr = [ 18 , 15 , 8 , 9 , 14 ] ; let len = arr . length ; findPairEvenParity ( arr , len ) ; ;
Program to check if N is a Hexagonal Number or not | Function to check if number is hexagonal ; Calculate the value for n ; Check if n - floor ( n ) is equal to 0 ; Driver code
function isHexagonal ( N ) { let val = 8 * N + 1 ; let x = 1 + Math . sqrt ( val ) ; let n = ( x ) / 4 ; if ( ( n - parseInt ( n ) ) == 0 ) { return true ; } else { return false ; } } let N = 14 ; if ( isHexagonal ( N ) == true ) { document . write ( " " + " " ) ; } else { document . write ( " " ) ; }
Program to convert Number in characters | JavaScript program to convert number in characters ; To calculate the reverse of the number ; The remainder will give the last digit of the number ; Extract the first digit of the reversed number ; Match it with switch case ; Divide the number by 10 to get the next number ; Driver code
function NumbertoCharacter ( n ) { let rev = 0 , r = 0 ; while ( n > 0 ) { r = n % 10 ; rev = rev * 10 + r ; n = Math . floor ( n / 10 ) ; } while ( rev > 0 ) { r = rev % 10 ; switch ( r ) { case 1 : document . write ( " " ) ; break ; case 2 : document . write ( " " ) ; break ; case 3 : document . write ( " " ) ; break ; case 4 : document . write ( " " ) ; break ; case 5 : document . write ( " " ) ; break ; case 6 : document . write ( " " ) ; break ; case 7 : document . write ( " " ) ; break ; case 8 : document . write ( " " ) ; break ; case 9 : document . write ( " " ) ; break ; case 0 : document . write ( " " ) ; break ; default : document . write ( " " ) ; break ; } rev = Math . floor ( rev / 10 ) ; } } let n = 12345 ; NumbertoCharacter ( n ) ;
Count all subarrays whose sum can be split as difference of squares of two Integers | Function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Loop to iterate over all the possible subsequences of the array ; Finding the sum of all the possible subsequences ; Condition to check whether the number can be split as difference of squares ; Driver code
function Solve ( arr , n ) { let temp = 0 , count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( let j = i ; j < n ; j ++ ) { temp += arr [ j ] ; if ( ( temp + 2 ) % 4 != 0 ) count ++ ; } } return count ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let N = arr . length ; document . write ( Solve ( arr , N ) ) ;
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | Javascript program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n ] [ r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver Code
let m = 4 ; function power ( x , y , p ) { let res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function nCr ( n , p , f ) { for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i ] [ j ] = 0 ; else if ( j == 0 j == i ) f [ i ] [ j ] = 1 ; else f [ i ] [ j ] = ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % p ; } } } function operations ( arr , n , f ) { let p = 1000000007 ; nCr ( n , p - 1 , f ) ; arr . sort ( ) ; let ans = 1 ; for ( let i = 0 ; i < n ; i ++ ) { let x = 0 ; for ( let j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } document . write ( ans + " " ) ; } let arr = [ 4 , 5 , 7 , 9 , 3 ] ; let n = arr . length ; let f = new Array ( n + 1 ) ; for ( var i = 0 ; i < f . length ; i ++ ) { f [ i ] = new Array ( 2 ) ; } operations ( arr , n , f ) ;
Check whether a number can be represented by the product of two squares | Function to check if there exist two numbers product of whose squares is n . ; Check whether the product of the square of both numbers is equal to N ; Driver code
function prodSquare ( n ) { for ( i = 2 ; i * i <= n ; i ++ ) for ( j = 2 ; j <= n ; j ++ ) if ( i * i * j * j == n ) return true ; return false ; } var n = 25 ; if ( prodSquare ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether a number can be represented by the product of two squares | Function to check if there exist two numbers product of whose squares is n ; Initialize map ; Store square value in hashmap ; Driver program to test above
function prodSquare ( n ) { var s = new Map ( ) ; for ( var i = 2 ; i * i <= n ; ++ i ) { s . set ( i * i , 1 ) ; if ( s . has ( n / ( i * i ) ) ) return true ; } return false ; } var n = 25 ; if ( prodSquare ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print N distinct numbers following the given operations | Function to print the required array ; Check if number is a multiple of 4 ; Printing Left Half of the array ; Printing Right Half of the array ; Driver code
function printArr ( n ) { if ( n % 4 == 0 ) { for ( var i = 1 ; i <= n / 2 ; i ++ ) document . write ( i * 2 + ' ' ) ; for ( var i = 1 ; i < n / 2 ; i ++ ) document . write ( i * 2 - 1 + ' ' ) ; document . write ( n + n / 2 - 1 + ' ' ) ; } else document . write ( " " ) ; } var n = 22 ; printArr ( n ) ;
Sum of product of all subsets formed by only divisors of N | Function to find the sum of product of all the subsets formed by only divisors of N ; Vector to store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver Code
function GetSum ( n ) { var divisors = [ ] ; for ( var i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . push ( i ) ; if ( i != n / i ) { divisors . push ( n / i ) ; } } } var ans = 1 ; for ( var i = 0 ; i < divisors . length ; i ++ ) { ans *= ( divisors [ i ] + 1 ) ; } ans = ans - 1 ; return ans ; } var N = 4 ; document . write ( GetSum ( N ) ) ;
Length of longest Powerful number subsequence in an Array | Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
function isPowerful ( n ) { while ( n % 2 == 0 ) { let power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( let factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { let power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } function longestPowerfulSubsequence ( arr , n ) { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } let arr = [ 6 , 4 , 10 , 13 , 9 , 25 ] ; let n = arr . length ; document . write ( longestPowerfulSubsequence ( arr , n ) + " " ) ;
Maximum OR value of a pair in an Array without using OR operator | Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
function maxOR ( arr , n ) { var max_value = Math . max . apply ( Math , arr ) ; var number_of_bits = parseInt ( ( ( Math . log ( max_value ) ) ) ) + 2 ; var complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; var c = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } var arr = [ 3 , 6 , 8 , 16 ] ; var n = arr . length ; document . write ( maxOR ( arr , n ) ) ;