codeparrot/xlcost-text-to-code huggingface dataset:
Minimum count of elements required to obtain the given Array by repeated mirror operations | Function to find minimum number of elements required to form A [ ] by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the final answer ; Driver Code
function minimumrequired ( A , N ) { let K = N ; let ans ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } let ispalindrome = true ; for ( let i = 0 ; i < parseInt ( K / 2 , 10 ) ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = false ; } if ( ispalindrome ) { ans = parseInt ( K / 2 , 10 ) ; K = parseInt ( K / 2 , 10 ) ; } else { ans = K ; break ; } } return ans ; } let a = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] ; let N = a . length ; document . write ( minimumrequired ( a , N ) ) ;
Sum of product of all integers upto N with their count of divisors | Function to find the sum of the product of all the integers and their positive divisors up to N ; Iterate for every number between 1 and N ; Find the first multiple of i between 1 and N ; Find the last multiple of i between 1 and N ; Find the total count of multiple of in [ 1 , N ] ; Compute the contribution of i using the formula ; Add the contribution of i to the answer ; Return the result ; Given N ; function call
function sumOfFactors ( N ) { var ans = 0 ; for ( i = 1 ; i <= N ; i ++ ) { var first = i ; var last = parseInt ( N / i ) * i ; var factors = parseInt ( ( last - first ) / i ) + 1 ; var totalContribution = parseInt ( ( ( factors ) * ( factors + 1 ) ) / 2 ) * i ; ans += totalContribution ; } return ans ; } var N = 3 ; document . write ( sumOfFactors ( N ) ) ;
Find a number M < N such that difference between their XOR and AND is maximum | Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Given Number N ; Function Call
function getMaxDifference ( N ) { var M = - 1 ; var maxDiff = 0 ; for ( i = 0 ; i < N ; i ++ ) { var diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } var N = 6 ; document . write ( getMaxDifference ( N ) ) ;
Find a number M < N such that difference between their XOR and AND is maximum | Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Given number ; Function call
function findM ( N ) { var M = 0 ; var MSB = parseInt ( Math . log ( N ) ) ; for ( i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } var N = 6 ; document . write ( findM ( N ) ) ;
Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h2 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Given array ; Function Call
function findTriplet ( arr ) { var n = arr . length ; var st = [ ] ; var h3 = - 1000000000 , h1 = 1000000000 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( st . length != 0 && st [ st . length - 1 ] < arr [ i ] ) { h3 = st [ st . length - 1 ] ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } var arr = [ 4 , 7 , 5 , 6 ] ; if ( findTriplet ( arr ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimum number of distinct elements after removing M items | Set 2 | Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Initialize array ; Size of array ; Function call
function distinctNumbers ( arr , m , n ) { var count = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( count . has ( arr [ i ] ) ) count . set ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) else count . set ( arr [ i ] , 1 ) var fre_arr = Array ( n + 1 ) . fill ( 0 ) ; count . forEach ( ( value , key ) => { fre_arr [ value ] ++ ; } ) ; var ans = count . size ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; var t = Math . min ( temp , parseInt ( m / i ) ) ; ans -= t ; m -= i * t ; } return ans ; } var arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] ; var n = arr . length ; var m = 2 ; document . write ( distinctNumbers ( arr , m , n ) ) ;
Find minimum moves to bring all elements in one cell of a matrix | Javascript implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Coordinates of given cell ; Given Matrix ; Element to be moved ; Function call
let M = 4 ; let N = 5 ; function no_of_moves ( Matrix , x , y ) { let moves = 0 ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { if ( Matrix [ i ] [ j ] != 0 ) { moves += Math . abs ( x - i ) ; moves += Math . abs ( y - j ) ; } } } document . write ( moves ) ; } let x = 3 ; let y = 2 ; let Matrix = [ [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] ] ; let num = 1 ; no_of_moves ( Matrix , x , y ) ;
Check if all array elements can be removed by the given operations | Function to check if it is possible to remove all array elements ; Condition if we can remove all elements from the array ; Driver Code
function removeAll ( arr , n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) document . write ( " " ) ; else document . write ( " " ) ; } let Arr = [ 10 , 4 , 7 , 1 , 3 , 6 ] ; let size = Arr . length ; removeAll ( Arr , size ) ;
Smallest subarray having an element with frequency greater than that of other elements | Function to find subarray ; If the array has only one element , then there is no answer . ; Array to store the last occurrences of the elements of the array . ; To maintain the length ; Variables to store start and end indices ; Check if element is occurring for the second time in the array ; Find distance between last and current index of the element . ; If the current distance is less then len update len and set ' start ' and ' end ' ; Set the last occurrence of current element to be ' i ' . ; If flag is equal to 0 , it means there is no answer . ; Driver code
function FindSubarray ( arr , n ) { if ( n == 1 ) { document . write ( " " ) ; } let vis = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) vis [ i ] = - 1 ; vis [ arr [ 0 ] ] = 0 ; let len = Number . MAX_VALUE , flag = 0 ; let start = 0 , end = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let t = arr [ i ] ; if ( vis [ t ] != - 1 ) { let distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) document . write ( " " ) ; else { for ( let i = start ; i <= end ; i ++ ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 2 , 3 , 2 , 4 , 5 ] ; let n = arr . length ; FindSubarray ( arr , n ) ;
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | Function to to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continious characters in the given string ; Subtract non special strings from answer ; Given string s ; Function call
function countSubstrings ( s ) { var n = s . length ; var answer = ( n * ( n - 1 ) ) / 2 ; var cnt = 1 ; var v = [ ] ; for ( var i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . push ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . push ( cnt ) ; for ( var i = 0 ; i < v . length - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } var s = " " ; document . write ( countSubstrings ( s ) ) ;
Minimum minutes needed to make the time palindromic | Function to get the required minutes ; Storing hour and minute value in letegral form ; Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute ; If mins is 60 , increase hour , and reinitilialized to 0 ; If hours is 60 , reinitialized to 0 ; Return the required time ; Given Time as a string ; Function Call
function get_palindrome_time ( str ) { let hh , mm ; hh = ( str [ 0 ] . charCodeAt ( ) - 48 ) * 10 + ( str [ 1 ] . charCodeAt ( ) - 48 ) ; mm = ( str [ 3 ] . charCodeAt ( ) - 48 ) * 10 + ( str [ 4 ] . charCodeAt ( ) - 48 ) ; let requiredTime = 0 ; while ( hh % 10 != Math . floor ( mm / 10 ) || Math . floor ( hh / 10 ) != mm % 10 ) { ++ mm ; if ( mm == 60 ) { mm = 0 ; ++ hh ; } if ( hh == 24 ) hh = 0 ; ++ requiredTime ; } return requiredTime ; } let str = " " ; document . write ( get_palindrome_time ( str . split ( ' ' ) ) ) ;
Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | Function to find the maximum sum of all subarrays ; Initialize maxsum and prefixArray ; Find the frequency using prefix Array ; Perform prefix sum ; Sort both arrays to get a greedy result ; Finally multiply largest frequency with largest array element . ; Return the answer ; Driver Code ; Initial Array ; Subarrays ; Function call
function maximumSubarraySum ( a , n , subarrays ) { let i , maxsum = 0 ; let prefixArray = new Array ( n ) ; prefixArray . fill ( 0 ) ; for ( i = 0 ; i < subarrays . length ; ++ i ) { prefixArray [ subarrays [ i ] [ 0 ] - 1 ] ++ ; prefixArray [ subarrays [ i ] [ 1 ] ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } prefixArray . sort ( ) ; a . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } let n = 6 ; let a = [ 4 , 1 , 2 , 1 , 9 , 2 ] ; let subarrays = [ ] ; subarrays . push ( [ 1 , 2 ] ) ; subarrays . push ( [ 1 , 3 ] ) ; subarrays . push ( [ 1 , 4 ] ) ; subarrays . push ( [ 3 , 4 ] ) ; document . write ( maximumSubarraySum ( a , n , subarrays ) ) ;
Maximum profit such that total stolen value is less than K to get bonus | Function to find the maximum profit from the given values ; Iterating over every possible permutation ; Driver code ; Function call
function maxProfit ( value , N , K ) { value . sort ( ) ; let maxval = value [ N - 1 ] ; let maxProfit = 0 ; let curr_val ; do { curr_val = 0 ; for ( let i = 0 ; i < N ; i ++ ) { curr_val += value [ i ] ; if ( curr_val <= K ) { maxProfit = Math . max ( curr_val + maxval * ( i + 1 ) , maxProfit ) ; } } } while ( next_permutation ( value ) ) ; return maxProfit ; } function next_permutation ( p ) { for ( let a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( let b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { let t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } let N = 4 , K = 6 ; let values = [ 5 , 2 , 7 , 3 ] ; document . write ( maxProfit ( values , N , K ) ) ;
Min operations to reduce N by multiplying by any number or taking square root | Function to reduce N to its minimum possible value by the given operations ; Keep replacing n until is an integer ; Keep replacing n until n is divisible by i * i ; Print the answer ; Given N ; Function Call
function minValue ( n ) { while ( parseInt ( Math . sqrt ( n ) ) == Math . sqrt ( n ) && n > 1 ) { n = parseInt ( Math . sqrt ( n ) ) ; } for ( var i = parseInt ( Math . sqrt ( n ) ) ; i > 1 ; i -- ) { while ( n % ( i * i ) == 0 ) n /= i ; } document . write ( n ) ; } var N = 20 ; minValue ( N ) ;
Count of ways to split given string into two non | Function to check whether the substring from l to r is palindrome or not ; If characters at l and r differ ; Not a palindrome ; If the string is a palindrome ; Function to count and return the number of possible splits ; Stores the count of splits ; Check if the two substrings after the split are palindromic or not ; If both are palindromes ; Print the final count ; Driver Code
function isPalindrome ( l , r , s ) { while ( l <= r ) { if ( s [ l ] != s [ r ] ) return false ; l ++ ; r -- ; } return true ; } function numWays ( s ) { let n = s . length ; let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( isPalindrome ( 0 , i , s ) && isPalindrome ( i + 1 , n - 1 , s ) ) { ans ++ ; } } return ans ; } let S = " " ; document . write ( numWays ( S ) ) ;
Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | Function to find the direction when stopped moving ; Given size of NxM grid ; Function Call
function findDirection ( n , m ) { if ( n > m ) { if ( m % 2 == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } else { if ( n % 2 == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } } let n = 3 , m = 3 ; findDirection ( n , m ) ;
Maximize the sum of modulus with every Array element | Function to return the maximum sum of modulus with every array element ; Sum of array elements ; Return the answer ; Driver Code
function maxModulosum ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum - n ; } let a = [ 3 , 4 , 6 ] ; let n = a . length ; document . write ( maxModulosum ( a , n ) ) ;
Minimum jumps required to group all 1 s together in a given Binary string | Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver code
function getMinJumps ( s ) { let ones = [ ] ; let jumps = 0 , median = 0 , ind = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) ones . push ( i ) ; } if ( ones . length == 0 ) return jumps ; median = ones [ parseInt ( ones . length / 2 , 10 ) ] ; ind = median ; for ( let i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( let i = ind ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } let S = " " ; document . write ( getMinJumps ( S ) ) ;
Find GCD of each subtree of a given node in an N | Maximum Number of nodes ; Tree represented as adjacency list ; for storing value associates with node ; for storing GCD of every subarray ; number of nodes ; Function to find GCD of two numbers using Euclidean algo ; If b == 0 then simply return a ; DFS function to traverse the tree ; Initializing answer with GCD of this node . ; Iterate over each child of current node ; Skipping the parent ; Call DFS for each child ; Taking GCD of the answer of the child to find node 's GCD ; Calling DFS from the root ( 1 ) for precomputing answers ; Function to find and print GCD for Q queries ; Doing preprocessing ; Iterate over each given query ; Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) ; Making a undirected tree ; Values associated with nodes ; Function call
var N = 100005 ; var v = Array . from ( Array ( N ) , ( ) => Array ( ) ) ; var val = Array ( N ) ; var answer = Array ( N ) ; var n ; function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function DFS ( node , parent ) { answer [ node ] = val [ node ] ; v [ node ] . forEach ( child => { if ( child != parent ) { DFS ( child , node ) ; answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) ; } } ) ; } function preprocess ( ) { DFS ( 1 , - 1 ) ; } function findGCD ( queries , q ) { preprocess ( ) ; for ( var i = 0 ; i < q ; i ++ ) { var GCD = answer [ queries [ i ] ] ; document . write ( " " + queries [ i ] + " " + GCD + " " ) ; } } n = 5 ; v [ 1 ] . push ( 2 ) ; v [ 2 ] . push ( 1 ) ; v [ 1 ] . push ( 3 ) ; v [ 3 ] . push ( 1 ) ; v [ 3 ] . push ( 4 ) ; v [ 4 ] . push ( 3 ) ; v [ 3 ] . push ( 5 ) ; v [ 5 ] . push ( 3 ) ; val [ 1 ] = 2 ; val [ 2 ] = 3 ; val [ 3 ] = 4 ; val [ 4 ] = 8 ; val [ 5 ] = 16 ; var queries = [ 2 , 3 , 1 ] ; var q = queries . length ; findGCD ( queries , q ) ;
Minimize cost to convert given two integers to zero using given operations | Function to find out the minimum cost to make two number X and Y equal to zero ; If x is greater than y then swap ; Cost of making y equal to x ; Cost if we choose 1 st operation ; Cost if we choose 2 nd operation ; Total cost ; Driver code
function makeZero ( x , y , a , b ) { if ( x > y ) { let temp = x ; x = y ; y = temp ; } let tot_cost = ( y - x ) * a ; let cost1 = 2 * x * a ; let cost2 = x * b ; tot_cost += Math . min ( cost1 , cost2 ) ; document . write ( tot_cost ) ; } let X = 1 , Y = 3 ; let cost1 = 391 , cost2 = 555 ; makeZero ( X , Y , cost1 , cost2 ) ;
Find N fractions that sum upto a given fraction N / D | Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
function splitFraction ( n , d ) { var ar = new Array ( n ) ; var first = d + n - 1 ; ar [ 0 ] = first ; for ( var i = 1 ; i < n ; i ++ ) { ar [ i ] = first * -- first ; } for ( var i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n === 0 ) { document . write ( " " + ar [ i ] / n + " " ) ; } else { document . write ( n + " " + ar [ i ] + " " ) ; } } } var N = 4 ; var D = 2 ; splitFraction ( N , D ) ;
Count of pairs ( i , j ) in the array such that arr [ i ] is a factor of arr [ j ] | Function to return the count of Pairs ; Driver code
function numPairs ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) ans ++ ; } } return ans ; } let arr = [ 1 , 1 , 2 , 2 , 3 , 3 ] ; let n = arr . length ; document . write ( numPairs ( arr , n ) ) ;
Check if array can be converted into strictly decreasing sequence | Function to check that array can be converted into a strictly decreasing sequence ; Loop to check that each element is greater than the ( N - index ) ; If element is less than ( N - index ) ; If array can be converted ; Driver Code ; Function calling
function check ( arr , n ) { var flag = true ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < n - i ) { flag = false ; } } if ( flag ) { return true ; } else { return false ; } } let arr1 = [ 11 , 11 , 11 , 11 ] ; let n1 = arr1 . length ; if ( check ( arr1 , n1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximum Balanced String Partitions | Function to find a maximum number X , such that a given String can be partitioned into X subStrings that are each balanced ; If the size of the String is 0 , then answer is zero ; Variable that represents the number of ' R ' s and ' L ' s ; To store maximum number of possible partitions ; Increment the variable r if the character in the String is ' R ' ; Increment the variable l if the character in the String is ' L ' ; If r and l are equal , then increment ans ; Return the required answer ; Driver code ; Function call
function BalancedPartition ( str , n ) { if ( n == 0 ) return 0 ; let r = 0 , l = 0 ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { r ++ ; } else if ( str [ i ] == ' ' ) { l ++ ; } if ( r == l ) { ans ++ ; } } return ans ; } let str = " " ; let n = str . length ; document . write ( BalancedPartition ( str , n ) + " " ) ;
Minimize the non | Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver code ; Function Call
function minOccupiedPosition ( A , n ) { var minPos = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } var A = [ 8 , 0 , 7 , 0 , 0 , 6 ] ; var n = A . length ; document . write ( minOccupiedPosition ( A , n ) ) ;
Find minimum number K such that sum of array after multiplication by K exceed S | Function to return the minimum value of k that satisfies the given condition ; store sum of array elements ; Calculate the sum after ; return minimum possible K ; Driver code
function findMinimumK ( a , n , S ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return Math . ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } let a = [ 10 , 7 , 8 , 10 , 12 , 19 ] ; let n = a . length ; let S = 200 ; document . write ( findMinimumK ( a , n , S ) ) ;
Find the largest number smaller than integer N with maximum number of set bits | Function to return the largest number less than N ; Iterate through all possible values ; Multiply the number by 2 i times ; Return the final result ; Driver code
function largestNum ( n ) { let num = 0 ; for ( let i = 0 ; i <= 32 ; i ++ ) { let x = ( 1 << i ) ; if ( ( x - 1 ) <= n ) num = ( 1 << i ) - 1 ; else break ; } return num ; } let N = 345 ; document . write ( largestNum ( N ) ) ;
Find the String having each substring with exactly K distinct characters | Function to find the required output string ; Each element at index i is modulus of K ; initialise integers N and K
function findString ( N , K ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( String . fromCharCode ( ' ' . charCodeAt ( ) + i % K ) ) ; } } let N = 10 ; let K = 3 ; findString ( N , K ) ;
Find total no of collisions taking place between the balls in which initial direction of each ball is given | Function to count no of collision ; length of the string ; Driver code
function count ( s ) { let N , i , cnt = 0 , ans = 0 ; N = s . length ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) cnt ++ ; if ( s [ i ] == ' ' ) ans += cnt ; } return ans ; } let s = " " ; document . write ( count ( s ) ) ;
Maximum number on 7 | Function to find the maximum number that can be displayed using the N segments ; Condition to check base case ; Condition to check if the number is even ; Condition to check if the number is odd ; Driver Code
function segments ( n ) { if ( n == 1 n == 0 ) { return ; } if ( n % 2 == 0 ) { document . write ( " " ) ; segments ( n - 2 ) ; } else if ( n % 2 == 1 ) { document . write ( " " ) ; segments ( n - 3 ) ; } } let n ; n = 11 ; segments ( n ) ;