codeparrot/xlcost-text-to-code huggingface dataset:
Find the person who will finish last | Function to find the person who will finish last ; To keep track of rows and columns having 1 ; Available rows and columns ; Minimum number of choices we have ; If number of choices are odd ; P1 will finish last ; Otherwise , P2 will finish last ; Given matrix
function findLast ( mat ) { let m = mat . length ; let n = mat [ 0 ] . length ; let rows = new Set ( ) ; let cols = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] ) { rows . add ( i ) ; cols . add ( j ) ; } } } let avRows = m - rows . size ; let avCols = n - cols . size ; let choices = Math . min ( avRows , avCols ) ; if ( choices & 1 ) document . write ( " " ) else document . write ( " " ) } let mat = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] ] findLast ( mat ) ;
Sum of decimals that are binary representations of first N natural numbers | JavaScript program to implement the above approach ; Function to find the sum of first N natural numbers represented in binary representation ; Stores the resultant sum ; Iterate until the value of N is greater than 0 ; If N is less than 2 ; Store the MSB position of N ; Iterate in the range [ 1 , x ] and add the contribution of the numbers from 1 to ( 2 ^ x - 1 ) ; Update the value of the cur and add ; Add the cur to ans ; Store the remaining numbers ; Add the contribution by MSB by the remaining numbers ; The next iteration will be repeated for 2 ^ x - 1 ; Print the result ; Driver code
let MOD = 1000000007 ; function sumOfBinaryNumbers ( n ) { let ans = 0 ; let one = 1 ; while ( true ) { if ( n <= 1 ) { ans = ( ans + n ) % MOD ; break ; } let x = Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ; let cur = 0 ; let add = Math . floor ( Math . pow ( 2 , ( x - 1 ) ) ) ; for ( let i = 1 ; i <= x ; i ++ ) { cur = ( cur + add ) % MOD ; add = ( add * 10 % MOD ) ; } ans = ( ans + cur ) % MOD ; let rem = n - Math . floor ( Math . pow ( 2 , x ) ) + 1 ; let p = Math . floor ( Math . pow ( 10 , x ) ) ; p = ( p * ( rem % MOD ) ) % MOD ; ans = ( ans + p ) % MOD ; n = rem - 1 ; } document . write ( ans ) ; } let N = 3 ; sumOfBinaryNumbers ( N ) ;
Nearest Fibonacci Number to N | Function to find the Fibonacci number which is nearest to N ; Base Case ; Initialize the first & second terms of the Fibonacci series ; Store the third term ; Iterate until the third term is less than or equal to num ; Update the first ; Update the second ; Update the third ; Store the Fibonacci number having smaller difference with N ; Print the result ; Driver Code
function nearestFibonacci ( num ) { if ( num == 0 ) { document . write ( 0 ) ; return ; } let first = 0 , second = 1 ; let third = first + second ; while ( third <= num ) { first = second ; second = third ; third = first + second ; } let ans = ( Math . abs ( third - num ) >= Math . abs ( second - num ) ) ? second : third ; document . write ( ans ) ; } let N = 17 ; nearestFibonacci ( N ) ;
Permutation of first N natural numbers having given array as the prefix maximum array | Function to check if the maximum prefix array of ans [ ] is equal to array arr [ ] ; Initialize a variable , Max ; Traverse the array , ans [ ] ; Store the maximum value upto index i ; If it is not equal to a [ i ] , then return false ; Otherwise return false ; Function to find the permutation of the array whose prefix maximum array is same as the given array a [ ] ; Stores the required permutation ; Stores the index of first occurrence of elements ; Traverse the array a [ ] ; If a [ i ] is not present in um , then store it in um ; Update the ans [ i ] to a [ i ] ; Stores the unvisited numbers ; Fill the array , v [ ] ; Store the index ; Traverse the array , ans [ ] ; Fill v [ j ] at places where ans [ i ] is 0 ; Check if the current permutation maximum prefix array is same as the given array a [ ] ; If true , the print the permutation ; Otherwise , print - 1 ; Driver Code ; Function Call
function checkPermutation ( ans , a , n ) { let Max = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { Max = Math . max ( Max , ans [ i ] ) ; if ( Max != a [ i ] ) return false ; } return true ; } function findPermutation ( a , n ) { let ans = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { ans [ i ] = 0 ; } let um = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! um . has ( a [ i ] ) ) { ans [ i ] = a [ i ] ; um . set ( a [ i ] , i ) ; } } let v = [ ] ; let j = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( ! um . has ( i ) ) { v . push ( i ) ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( ans [ i ] == 0 ) { ans [ i ] = v [ j ] ; j ++ ; } } if ( checkPermutation ( ans , a , n ) ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } else document . write ( " " ) ; } let arr = [ 1 , 3 , 4 , 5 , 5 ] ; let N = arr . length ; findPermutation ( arr , N ) ;
Count pairs of equal elements possible by excluding each array element once | Function to count the number of required pairs for every array element ; Initialize a map ; Update the frequency of every element ; Stores the count of pairs ; Traverse the map ; Count the number of ways to select pairs consisting of equal elements only ; Traverse the array ; Print the count for every array element ; Given array ; Size of the array
function countEqualElementPairs ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var total = 0 ; mp . forEach ( ( value , key ) => { total += ( value * ( value - 1 ) ) / 2 ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { document . write ( total - ( mp . get ( arr [ i ] ) - 1 ) + " " ) ; } } var arr = [ 1 , 1 , 2 , 1 , 2 ] ; var N = arr . length ; countEqualElementPairs ( arr , N ) ;
Count of Octal numbers upto N digits | Function to return the count of natural octal numbers upto N digits ; Loop to iterate from 1 to N and calculating number of octal numbers for every ' i ' th digit . ; Driver code
function count ( N ) { var sum = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { sum += 7 * Math . pow ( 8 , i - 1 ) ; } return sum ; } var N = 4 ; document . write ( count ( N ) ) ;
Palindromic divisors of a number | Function to check is num is palindromic or not ; Convert n to string str ; Starting and ending index of string str ; If char at s and e are not equals then return false ; Function to find palindromic divisors ; To sore the palindromic divisors of number n ; If n is divisible by i ; Check if number is a perfect square ; Check divisor is palindromic , then store it ; Check if divisors are palindrome ; Check if n / divisors is palindromic or not ; Print all palindromic divisors in sorted order ; Driver code ; Function call to find all palindromic divisors
function isPalindrome ( n ) { var str = ( n . toString ( ) ) ; var s = 0 , e = str . length - 1 ; while ( s < e ) { if ( str [ s ] != str [ e ] ) { return false ; } s ++ ; e -- ; } return true ; } function palindromicDivisors ( n ) { var PalindromDivisors = [ ] ; for ( var i = 1 ; i <= parseInt ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( isPalindrome ( i ) ) { PalindromDivisors . push ( i ) ; } } else { if ( isPalindrome ( i ) ) { PalindromDivisors . push ( i ) ; } if ( isPalindrome ( n / i ) ) { PalindromDivisors . push ( n / i ) ; } } } } PalindromDivisors . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < PalindromDivisors . length ; i ++ ) { document . write ( PalindromDivisors [ i ] + " " ) ; } } var n = 66 ; palindromicDivisors ( n ) ;
Remove minimum numbers from the array to get minimum OR value | Function to return the minimum deletions to get minimum OR ; To store the minimum element ; Find the minimum element from the array ; To store the frequency of the minimum element ; Find the frequency of the minimum element ; Return the final answer ; Driver code
function findMinDel ( arr , n ) { var min_num = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) min_num = Math . min ( arr [ i ] , min_num ) ; var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] == min_num ) cnt ++ ; return n - cnt ; } var arr = [ 3 , 3 , 2 ] ; var n = arr . length ; document . write ( findMinDel ( arr , n ) ) ;
Number of subarrays with GCD equal to 1 | Javascript implementation of the approach ; Function to return the required count ; To store the final answer ; To store the GCD starting from index ' i ' ; Loop to find the gcd of each subarray from arr [ i ] to arr [ i ... n - 1 ] ; Increment the count if curr_gcd = 1 ; Return the final answer ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function cntSubArr ( arr , n ) { var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var curr_gcd = 0 ; for ( var j = i ; j < n ; j ++ ) { curr_gcd = __gcd ( curr_gcd , arr [ j ] ) ; ans += ( curr_gcd == 1 ) ; } } return ans ; } var arr = [ 1 , 1 , 1 ] ; var n = arr . length ; document . write ( cntSubArr ( arr , n ) ) ;
Program to print prime numbers from 1 to N . | Function to print first N prime numbers ; Declare the variables ; Print display message ; Traverse each number from 1 to N with the help of for loop ; Skip 0 and 1 as they are neither prime nor composite ; flag variable to tell if i is prime or not ; flag = 1 means i is prime and flag = 0 means i is not prime ; Driver code
function print_primes_till_N ( N ) { let i , j , flag ; document . write ( " " + N + " " ) ; for ( i = 1 ; i <= N ; i ++ ) { if ( i == 1 i == 0 ) continue ; flag = 1 ; for ( j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) document . write ( i + " " ) ; } } let N = 100 ; print_primes_till_N ( N ) ;
Maximize the expression ( A AND X ) * ( B AND X ) | Bit Manipulation | Function to find X according to the given conditions ; int can have 32 bits ; Temporary ith bit ; Compute ith bit of X according to given conditions Expression below is the direct conclusion from the illustration we had taken earlier ; Add the ith bit of X to X ; Driver code
function findX ( A , B ) { var X = 0 ; var MAX = 32 ; for ( var bit = 0 ; bit < MAX ; bit ++ ) { var tempBit = 1 << bit ; var bitOfX = A & B & tempBit ; X += bitOfX ; } return X ; } var A = 11 , B = 13 ; document . write ( findX ( A , B ) ) ;
Number of subsets whose mean is maximum | Function to return the count of subsets with the maximum mean ; Maximum value from the array ; To store the number of times maximum element appears in the array ; Return the count of valid subsets ; Driver code
function cntSubSets ( arr , n ) { var maxVal = arr . reduce ( function ( a , b ) { return Math . max ( a , b ) ; } ) ; var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == maxVal ) cnt ++ ; } return ( Math . pow ( 2 , cnt ) - 1 ) ; } var arr = [ 1 , 2 , 1 , 2 ] var n = arr . length ; document . write ( cntSubSets ( arr , n ) ) ;
Probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | Function to return the probability of getting the maximum pair sum when a random pair is chosen from the given array ; Initialize the maximum sum , its count and the count of total pairs ; For every single pair ; Get the sum of the current pair ; If the sum is equal to the current maximum sum so far ; Increment its count ; If the sum is greater than the current maximum ; Update the current maximum and re - initialize the count to 1 ; Find the required probability ; Driver code
function findProb ( arr , n ) { var maxSum = - 100000000 , maxCount = 0 , totalPairs = 0 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { var sum = arr [ i ] + arr [ j ] ; if ( sum == maxSum ) { maxCount ++ ; } else if ( sum > maxSum ) { maxSum = sum ; maxCount = 1 ; } totalPairs ++ ; } } var prob = maxCount / totalPairs ; return prob ; } var arr = [ 1 , 1 , 1 , 2 , 2 , 2 ] var n = arr . length ; document . write ( findProb ( arr , n ) ) ;
Maximum count of common divisors of A and B such that all are co | Javascript implementation of the approach ; Function to return the count of common factors of a and b such that all the elements are co - prime to one another ; GCD of a and b ; Include 1 initially ; Find all the prime factors of the gcd ; If gcd is prime ; Return the required answer ; Driver code
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function maxCommonFactors ( a , b ) { let gcd = GCD ( a , b ) ; let ans = 1 ; for ( let i = 2 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { ans ++ ; while ( gcd % i == 0 ) gcd = parseInt ( gcd / i ) ; } } if ( gcd != 1 ) ans ++ ; return ans ; } let a = 12 , b = 18 ; document . write ( maxCommonFactors ( a , b ) ) ;
Find the day number in the current year for the given date | Javascript implementation of the approach ; Function to return the day number of the year for the given date ; Extract the year , month and the day from the date string ; If current year is a leap year and the date given is after the 28 th of February then it must include the 29 th February ; Add the days in the previous months ; Driver code
var days = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; function dayOfYear ( date ) { var year = parseInt ( date . substring ( 0 , 4 ) ) ; var month = parseInt ( date . substring ( 5 , 6 ) ) ; var day = parseInt ( date . substring ( 8 ) ) ; if ( month > 2 && year % 4 == 0 && ( year % 100 != 0 year % 400 == 0 ) ) { ++ day ; } while ( month -- > 0 ) { day = day + days [ month - 1 ] ; } return day ; } var date = " " ; document . write ( dayOfYear ( date ) ) ;
Find the number of cells in the table contains X | Function to find number of cells in the table contains X ; Driver code ; Function call
function Cells ( n , x ) { let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) if ( x % i == 0 && parseInt ( x / i ) <= n ) ans ++ ; return ans ; } let n = 6 , x = 12 ; document . write ( Cells ( n , x ) ) ;
Smallest power of 4 greater than or equal to N | Function to return the smallest power of 4 greater than or equal to n ; If n is itself is a power of 4 then return n ; Driver code
function nextPowerOfFour ( n ) { let x = Math . floor ( Math . sqrt ( Math . sqrt ( n ) ) ) ; if ( Math . pow ( x , 4 ) == n ) return n ; else { x = x + 1 ; return Math . pow ( x , 4 ) ; } } let n = 122 ; document . write ( nextPowerOfFour ( n ) ) ;
Minimum operations required to convert X to Y by multiplying X with the given co | Function to return the minimum operations required ; Not possible ; To store the greatest power of p that divides d ; While divible by p ; To store the greatest power of q that divides d ; While divible by q ; If d > 1 ; Since , d = p ^ a * q ^ b ; Driver code
function minOperations ( x , y , p , q ) { if ( y % x != 0 ) return - 1 var d = Math . floor ( y / x ) var a = 0 while ( d % p == 0 ) { d = Math . floor ( d / p ) a += 1 } var b = 0 while ( d % q == 0 ) { d = Math . floor ( d / q ) b += 1 } if ( d != 1 ) return - 1 return ( a + b ) } var x = 12 var y = 2592 var p = 2 var q = 3 document . write ( minOperations ( x , y , p , q ) )
Number of Quadruples with GCD equal to K | Function to calculate NC4 ; Base case to calculate NC4 ; Function to return the count of required quadruples using Inclusion Exclusion ; Effective N ; Iterate over 2 to M ; Number of divisors of i till M ; Count stores the number of prime divisors occurring exactly once ; To prevent repetition of prime divisors ; If repetition of prime divisors present ignore this number ; If prime divisor count is odd subtract it from answer else add ; Driver code
function nCr ( n ) { if ( n < 4 ) return 0 ; let answer = n * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ; answer = parseInt ( answer / 24 ) ; return answer ; } function countQuadruples ( N , K ) { let M = parseInt ( N / K ) ; let answer = nCr ( M ) ; for ( let i = 2 ; i < M ; i ++ ) { let j = i ; let temp2 = parseInt ( M / i ) ; let count = 0 ; let check = 0 ; let temp = j ; while ( j % 2 == 0 ) { count ++ ; j = parseInt ( j / 2 ) ; if ( count >= 2 ) break ; } if ( count >= 2 ) { check = 1 ; } for ( let k = 3 ; k <= Math . sqrt ( temp ) ; k += 2 ) { let cnt = 0 ; while ( j % k == 0 ) { cnt ++ ; j = parseInt ( j / k ) ; if ( cnt >= 2 ) break ; } if ( cnt >= 2 ) { check = 1 ; break ; } else if ( cnt == 1 ) count ++ ; } if ( j > 2 ) { count ++ ; } if ( check ) continue ; else { if ( count % 2 == 1 ) { answer -= nCr ( temp2 ) ; } else { answer += nCr ( temp2 ) ; } } } return answer ; } let N = 10 , K = 2 ; document . write ( countQuadruples ( N , K ) ) ;
Find the number which when added to the given ratio a : b , the ratio changes to c : d | Function to return the required number X ; Driver code
function getX ( a , b , c , d ) { var X = ( b * c - a * d ) / ( d - c ) ; return X ; } var a = 2 , b = 3 , c = 4 , d = 5 ; document . write ( getX ( a , b , c , d ) ) ;
Number of ways to arrange a word such that no vowels occur together | Function to check if a character is vowel or consonent ; Function to calculate factorial of a number ; Calculating no of ways for arranging vowels ; Iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; Calculating no of ways to arrange the given word such that all vowels come together ; Calculate no of ways to arrange vowels ; To store denominator of fraction ; Count of consonents ; Calculate the number of ways to arrange the word such that all vowels come together ; To calculate total number of permutations ; To store length of the given word ; Denominator of fraction ; Return total number of permutations of the given word ; Function to calculate number of permutations such that no vowels come together ; To store frequency of character ; Count frequency of all characters ; Calculate total number of permutations ; Calculate total number of permutations such that all vowels come together ; Subtract vwl_tgthr from total to get the result ; Return the result ; Driver code
function isVowel ( ch ) { if ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) return true ; else return false ; } function fact ( n ) { if ( n < 2 ) { return 1 ; } return n * fact ( n - 1 ) ; } function only_vowels ( freq ) { let denom = 1 ; let cnt_vwl = 0 ; for ( let [ key , value ] of freq . entries ( ) ) { if ( isVowel ( key ) ) { denom *= fact ( value ) ; cnt_vwl += value ; } } return Math . floor ( fact ( cnt_vwl ) / denom ) ; } function all_vowels_together ( freq ) { let vow = only_vowels ( freq ) ; let denom = 1 ; let cnt_cnst = 0 ; for ( let [ key , value ] of freq . entries ( ) ) { if ( ! isVowel ( key ) ) { denom *= fact ( value ) ; cnt_cnst += value ; } } let ans = Math . floor ( fact ( cnt_cnst + 1 ) / denom ) ; return ( ans * vow ) ; } function total_permutations ( freq ) { let cnt = 0 ; let denom = 1 ; for ( let [ key , value ] of freq . entries ( ) ) { denom *= fact ( value ) ; cnt += value ; } return Math . floor ( fact ( cnt ) / denom ) ; } function no_vowels_together ( word ) { let freq = new Map ( ) ; for ( let i = 0 ; i < word . length ; i ++ ) { let ch = word [ i ] . toLowerCase ( ) ; if ( freq . has ( ch ) ) { freq . set ( ch , freq . get ( ch ) + 1 ) ; } else { freq . set ( ch , 1 ) ; } } let total = total_permutations ( freq ) ; let vwl_tgthr = all_vowels_together ( freq ) ; let res = total - vwl_tgthr ; return res ; } let word = " " ; let ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ; word = " " ; ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ; word = " " ; ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ;
Program to find the number of men initially | Function to return the number of men initially ; Driver code
function numberOfMen ( D , m , d ) { var Men = ( m * ( D - d ) ) / d ; return Men ; } var D = 5 , m = 4 , d = 4 ; document . write ( numberOfMen ( D , m , d ) ) ;
Area of triangle formed by the axes of co | Function to find area ; Driver code
function area ( a , b , c ) { var d = Math . abs ( ( c * c ) / ( 2 * a * b ) ) ; return d ; } var a = - 2 , b = 4 , c = 3 ; document . write ( area ( a , b , c ) ) ;
Sum of two numbers where one number is represented as array of digits | Function to return the vector containing the answer ; ArrayList v is to store each digits sum and ArrayList ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer
function addToArrayForm ( A , K ) { let v = [ ] ; let ans = [ ] ; let rem = 0 ; let i = 0 ; for ( i = A . length - 1 ; i >= 0 ; i -- ) { let my = A [ i ] + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . push ( my % 10 ) ; } else { v . push ( my ) ; rem = 0 ; } K = parseInt ( K / 10 , 10 ) ; } while ( K > 0 ) { let my = K % 10 + rem ; v . push ( my % 10 ) ; if ( parseInt ( my / 10 , 10 ) > 0 ) rem = 1 ; else rem = 0 ; K = parseInt ( K / 10 , 10 ) ; } if ( rem > 0 ) v . push ( rem ) ; for ( let j = v . length - 1 ; j >= 0 ; j -- ) ans . push ( v [ j ] ) ; return ans ; } let A = [ ] ; A . push ( 2 ) ; A . push ( 7 ) ; A . push ( 4 ) ; let K = 181 ; let ans = addToArrayForm ( A , K ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] ) ;
Compute maximum of the function efficiently over all sub | JavaScript implementation of the above approach ; Function to return maximum sum of a sub - array ; Function to return maximum value of function F ; Compute arrays B [ ] and C [ ] ; Find maximum sum sub - array of both of the arrays and take maximum among them ; Driver code
const MAX = 100005 ; function kadaneAlgorithm ( ar , n ) { let sum = 0 , maxSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; if ( sum < 0 ) sum = 0 ; maxSum = Math . max ( maxSum , sum ) ; } return maxSum ; } function maxFunction ( arr , n ) { let b = new Array ( MAX ) , c = new Array ( MAX ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( i & 1 ) { b [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; c [ i ] = - b [ i ] ; } else { c [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; b [ i ] = - c [ i ] ; } } let ans = kadaneAlgorithm ( b , n - 1 ) ; ans = Math . max ( ans , kadaneAlgorithm ( c , n - 1 ) ) ; return ans ; } let arr = [ 1 , 5 , 4 , 7 ] ; let n = arr . length ; document . write ( maxFunction ( arr , n ) ) ;
Third last digit in 5 ^ N for given N | Function to find the element ; if n < 3 ; If n is even return 6 If n is odd return 1 ; Driver code
function findThirdDigit ( n ) { if ( n < 3 ) return 0 ; return n & 1 ? 1 : 6 ; } var n = 7 ; document . write ( findThirdDigit ( n ) ) ;
Probability of A winning the match when individual probabilities of hitting the target given | Function to return the probability of A winning ; p and q store the values of fractions a / b and c / d ; To store the winning probability of A ; Driver code
function getProbability ( a , b , c , d ) { var p = a / b ; var q = c / d ; var ans = p * ( 1 / ( 1 - ( 1 - q ) * ( 1 - p ) ) ) ; return ans ; } var a = 1 , b = 2 , c = 10 , d = 11 ; document . write ( getProbability ( a , b , c , d ) . toFixed ( 5 ) ) ;