codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Rook can reach the given destination in a single move | Function to check if it is possible to reach destination in a single move by a rook ; Given arrays
function check ( current_row , current_col , destination_row , destination_col ) { if ( current_row == destination_row ) return " " ; else if ( current_col == destination_col ) return " " ; else return " " ; } let current_row = 8 ; let current_col = 8 ; let destination_row = 8 ; let destination_col = 4 ; let output = check ( current_row , current_col , destination_row , destination_col ) ; document . write ( output ) ;
Append X digits to the end of N to make it divisible by M | Javascript program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver Code ; Stores the number by appending X digits on the right side of N
var res = - 1 ; function isDiv ( N , X , M ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return true ; } return false ; } for ( var i = 0 ; i <= 9 ; i ++ ) { if ( isDiv ( N * 10 + i , X - 1 , M ) ) { return true ; } } } var N = 4 , M = 50 , X = 2 ; isDiv ( N , X , M , res ) ; document . write ( res ) ;
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | Function to find the minimum moves ; Stores the sum of the array ; Store the maximum element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Given array ; Size of given array ; Function Call
function minimumMoves ( arr , N ) { let sum = 0 ; let maxelement = - 1 ; if ( N == 2 ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . max ( maxelement , arr [ i ] ) ; } let k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . max ( maxelement , k ) ; let ans = k * ( N - 1 ) - sum ; document . write ( ans ) ; } let arr = [ 2 , 3 , 7 ] ; let N = arr . length ; minimumMoves ( arr , N ) ;
Split first N natural numbers into two subsequences with non | Function to split 1 to N into two subsequences with non - coprime sums ; Driver code
function printSubsequence ( N ) { document . write ( " " ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { document . write ( i + " " ) ; } document . write ( N - 1 + " " + " " ) ; document . write ( " " + N + " " + " " ) ; } let N = 8 ; printSubsequence ( N ) ;
Generate an array with product of all subarrays of length exceeding one divisible by K | Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Given N and K ; Function Call
function array_divisbleby_k ( N , K ) { let flag = false ; let d1 = 0 , d2 = 0 ; for ( let i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { document . write ( d2 + " " ) ; } else { document . write ( d1 + " " ) ; } } } else { document . write ( - 1 ) ; } } let N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ;
Minimum characters to be replaced to make a string concatenation of a K | Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given String ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver code ; Function Call
function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let cnt = 0 ; let visited ; function DFS ( u , adj , fre , S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( let i = 0 ; i < adj [ u ] . length ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , fre , S ) ; } } } function minimumOperations ( S , m ) { let V = 100 ; let adj = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { adj . push ( [ ] ) ; } let total = 0 , N = S . length ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( let i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new Array ( V ) ; visited . fill ( false ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { let fre = new Array ( 26 ) ; fre . fill ( 0 ) ; cnt = 0 ; let maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( let j = 0 ; j < 26 ; j ++ ) maxx = Math . max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } document . write ( total ) ; } let S = " " ; let K = 2 ; minimumOperations ( S , K ) ;
Replace specified matrix elements such that no two adjacent elements are equal | Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Given N and M ; Given matrix ; Function call
function print ( arr , n , m ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' ' ) { arr [ i ] [ j ] = ' ' ; } else if ( a == ' ' ) { arr [ i ] [ j ] = ' ' ; } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let n = 4 , m = 4 ; let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; print ( arr , n , m ) ;
Lexicographically smallest string formed by removing duplicates | Function that finds lexicographically smallest string after removing the duplicates from the given string ; Stores the frequency of characters ; Mark visited characters ; Stores count of each character ; Stores the resultant string ; Decrease the count of current character ; If character is not already in answer ; Last character > S [ i ] and its count > 0 ; Mark letter unvisited ; Add s [ i ] in res and mark it visited ; Return the resultant string ; Given string S ; Function Call
function removeDuplicateLetters ( s ) { var cnt = Array ( 26 ) . fill ( 0 ) ; var vis = Array ( 26 ) . fill ( false ) ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var res = " " ; for ( var i = 0 ; i < n ; i ++ ) { cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; if ( ! vis [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ) { while ( res . length > 0 && res [ res . length - 1 ] . charCodeAt ( 0 ) > s [ i ] . charCodeAt ( 0 ) && cnt [ res [ res . length - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > 0 ) { vis [ res [ res . length - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 0 ; res = res . substring ( 0 , res . length - 1 ) ; } res += s [ i ] ; vis [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; } } return res ; } var S = " " ; document . write ( removeDuplicateLetters ( S ) ) ;
Number of pairs whose product is a power of 2 | Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Given array ; Size of the array ; Function Call
function countPairs ( arr , N ) { let countPowerof2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( Number ( arr [ i ] . toString ( 2 ) . split ( " " ) . sort ( ) . join ( " " ) ) . toString ( ) . length == 1 ) countPowerof2 ++ ; } let desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; document . write ( desiredPairs + " " ) ; } let arr = [ 2 , 4 , 7 , 2 ] ; let N = arr . length ; countPairs ( arr , N ) ;
Count ways to split N ! into two distinct co | Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; Mark all its multiples as non - prime ; Count primes <= MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into co - prime factors ; Calling sieve function ; Given N ; Function call
var MAXN = 1000000 var is_prime = Array ( MAXN ) . fill ( 0 ) ; var count_of_primes = Array ( MAXN ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( var i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] ) for ( var j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } is_prime [ 2 ] = 1 ; for ( var i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } function power ( x , y , p ) { var result = 1 ; while ( y > 0 ) { if ( y & 1 == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } function numberOfWays ( N ) { var count = count_of_primes [ N ] - 1 ; var mod = 1000000007 ; var answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; document . write ( answer ) ; } sieve ( ) ; var N = 7 ; numberOfWays ( N ) ;
Maximum possible sum of squares of stack elements satisfying the given properties | Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Prvar the result ; Driver Code ; Function call
function maxSumOfSquares ( N , S ) { var res = 0 ; if ( S < N S > 9 * N ) { document . write ( - 1 ) ; return ; } S = S - N ; var c = 0 ; while ( S > 0 ) { c ++ ; if ( parseInt ( S / 8 ) > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; document . write ( res ) ; } var N = 3 ; var S = 12 ; maxSumOfSquares ( N , S ) ;
Unset least significant K bits of a given number | Function to return the value after unsetting K LSBs ; Create a mask ; Bitwise AND operation with the number and the mask ; Given N and K ; Function Call
function clearLastBit ( N , K ) { var mask = ( - 1 << K + 1 ) ; return N = N & mask ; } var N = 730 , K = 3 ; document . write ( clearLastBit ( N , K ) ) ;
Check if quantities of 3 distinct colors can be converted to a single color by given merge | Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Given colors ; Function Call
function isPossible ( r , b , g ) { r = r % 3 ; b = b % 3 ; g = g % 3 ; if ( r == b b == g g == r ) { return true ; } else { return false ; } } var R = 1 , B = 3 , G = 6 ; if ( isPossible ( R , B , G ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize steps required to make all array elements same by adding 1 , 2 or 5 | Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
function calculate_steps ( arr , n , minimum ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += parseInt ( arr [ i ] / 5 ) ; arr [ i ] = arr [ i ] % 5 ; count += parseInt ( arr [ i ] / 2 ) ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] ) { count ++ ; } } arr [ i ] = val ; } return count ; } function solve ( arr , n ) { arr . sort ( ( a , b ) => b - a ) ; var minimum = arr [ n - 1 ] ; var count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . min ( count1 , Math . min ( count2 , count3 ) ) ; } var arr = [ 3 , 6 , 6 ] ; var N = arr . length ; document . write ( solve ( arr , N ) ) ;
Count of subarrays having sum equal to its length | Function that counts the subarrays with sum of its elements as its length ; Decrementing all the elements of the array by 1 ; Making prefix sum array ; Declare map to store count of elements upto current element ; To count all the subarrays whose prefix sum is 0 ; Iterate the array ; Increment answer by count of current element of prefix array ; Return the answer ; Given array arr [ ] ; Function call
function countOfSubarray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) arr [ i ] -- ; let pref = [ ] ; pref [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; let mp = new Map ; let answer = 0 ; if ( mp [ 0 ] ) mp [ 0 ] ++ ; else mp [ 0 ] = 1 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp [ pref [ i ] ] ) { answer += mp [ pref [ i ] ] ; mp [ pref [ i ] ] ++ ; } } return answer ; } let arr = [ 1 , 1 , 0 ] ; let N = arr . length ; document . write ( countOfSubarray ( arr , N ) ) ;
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; Create a vector for storing the elements of subsets ; Doing until all the set bits of n are used ; This part is executed only when the last bit is set ; Right shift the bit by one position ; Increasing the count each time by one ; Printing the values os elements ; Driver code
function printsubset ( n , k ) { let count = 0 , x = 0 ; let vec = [ ] ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . push ( Math . pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( let i = 0 ; i < vec . length ; i ++ ) document . write ( vec [ i ] + " " ) ; } let n = 7 , k = 4 ; printsubset ( n , k ) ;
Minimum digits to be removed to make either all digits or alternating digits same | Function to find longest possible subsequence of s beginning with x and y ; Iterate over the string ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Given string s ; Find the size of the string ; Function Call ; This value is the count of minimum element to be removed
function solve ( s , x , y ) { let res = 0 ; for ( let c of s ) { if ( c - ' ' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } function find_min ( s ) { let count = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { for ( let j = 0 ; j < 10 ; j ++ ) { count = Math . max ( count , solve ( s , i , j ) ) ; } } return count ; } let s = " " ; let n = s . length ; let answer = find_min ( s ) ; document . write ( n - answer ) ;
Count total set bits in all numbers from range L to R | Function to count set bits in x ; Base Case ; Recursive Call ; Function that returns count of set bits present in all numbers from 1 to N ; Initialize the result ; Return the setbit count ; Given L and R ; Function Call
function countSetBitsUtil ( x ) { if ( x <= 0 ) return 0 ; return ( ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( parseInt ( x / 2 ) ) ) ; } function countSetBits ( L , R ) { var bitCount = 0 ; for ( var i = L ; i <= R ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } var L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;
Possible values of Q such that , for any value of R , their product is equal to X times their sum | Function to find all possible values of Q ; Vector initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver code
function values_of_Q ( X ) { let val_Q = [ ] ; for ( let i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . push ( X + i ) ; } } document . write ( val_Q . length + " " ) ; for ( let i = 0 ; i < val_Q . length ; i ++ ) { document . write ( val_Q [ i ] + " " ) ; } } let X = 3 ; values_of_Q ( X ) ;
Program to find Greatest Common Divisor ( GCD ) of N strings | Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Given array of strings ; Function Call
function gcd ( str1 , str2 ) { if ( str1 . length < str2 . length ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . startsWith ( str2 ) ) { return " " ; } else if ( str2 . length == 0 ) { return str1 ; } else { return gcd ( str1 . substr ( str2 . length ) , str2 ) ; } } function findGCD ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( findGCD ( arr , n ) ) ;
Check whether the given Matrix is balanced or not | Define size of matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Given Matrix mat [ ] [ ] ; Function Call
let N = 4 ; let M = 4 ; function balancedMatrix ( mat ) { let is_balanced = true ; for ( let i = 0 ; i < N && is_balanced ; i ++ ) { for ( let j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i ] [ j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i ] [ j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i ] [ j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " " ; else return " " ; } let mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] ; document . write ( balancedMatrix ( mat ) ) ;
Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS
function unixTimeToHumanReadable ( seconds ) { let ans = " " ; let daysOfMonth = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; let currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = parseInt ( seconds / ( 24 * 60 * 60 ) , 10 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = parseInt ( extraTime / 3600 , 10 ) ; minutes = parseInt ( ( extraTime % 3600 ) / 60 , 10 ) ; secondss = parseInt ( ( extraTime % 3600 ) % 60 , 10 ) ; ans += date . toString ( ) ; ans += " " ; ans += month . toString ( ) ; ans += " " ; ans += currYear . toString ( ) ; ans += " " ; ans += hours . toString ( ) ; ans += " " ; ans += minutes . toString ( ) ; ans += " " ; ans += secondss . toString ( ) ; return ans ; } let T = 1595497956 ; let ans = unixTimeToHumanReadable ( T ) ; document . write ( ans + " " ) ;
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | Function to find area of rectangle inscribed another rectangle of length L and width W ; Area of rectangle ; Return the area ; Given dimensions ; Function call
function AreaofRectangle ( L , W ) { var area = parseFloat ( ( ( W + L ) * ( W + L ) ) / 2 ) . toFixed ( 1 ) ; return area ; } var L = 18 ; var W = 12 ; document . write ( AreaofRectangle ( L , W ) ) ;
Minimum number of operations required to reduce N to 0 | Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code
function downToZero ( n ) { if ( n <= 3 ) return n ; let dp = new Array ( n + 1 ) dp . fill ( - 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; let sqr ; for ( let i = 4 ; i <= n ; i ++ ) { sqr = Math . sqrt ( i ) ; let best = Number . MAX_VALUE ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } let n = 4 ; document . write ( downToZero ( n ) ) ;
Minimum number of operations required to reduce N to 0 | Function to find the minimum steps required to reduce n ; Base case ; Return answer based on parity of n ; Driver Code
function downToZero ( n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } let n = 4 ; document . write ( downToZero ( n ) ) ;