codeparrot/xlcost-text-to-code huggingface dataset:
Count subsequence of length 4 having product of the first three elements equal to the fourth element | Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code
function countQuadruples ( A , N ) { let ans = 0 ; let freq = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { ans += freq . get ( A [ i ] ) ; } for ( let j = 0 ; j < i ; j ++ ) { for ( let k = 0 ; k < j ; k ++ ) { if ( freq . has ( A [ i ] * A [ j ] * A [ k ] ) ) { freq . set ( freq . get ( A [ i ] * A [ j ] * A [ k ] ) , freq . get ( [ A [ i ] * A [ j ] * A [ k ] ] ) + 1 ) ; } else { freq . set ( A [ i ] * A [ j ] * A [ k ] , 1 ) ; } } } } return ans ; } let arr = [ 10 , 2 , 2 , 7 , 40 , 160 ] ; let N = arr . length ; document . write ( countQuadruples ( arr , N ) ) ;
Maximum times X and Y can be reduced to near 0 using numbers A or B | Helper function to check if we can perform Mid number of moves let MAXN = 10000000 ; MAXN = 1e7 ; Remove atleast Mid * B from both X and Y ; If any value is negative return false . ; Calculate remaining values ; If val >= Mid then it is possible to perform this much moves ; else return false ; Initialize a variable to store the answer ; Fix the left and right range ; Binary Search over the answer ; Check for the middle value as the answer ; It is possible to perform this much moves ; Maximise the answer ; Return answer ; Driver Code ; Generalise that A >= B
function can ( Mid , X , Y , A , B ) { let p1 = X - Mid * B ; let p2 = Y - Mid * B ; if ( p1 < 0 p2 < 0 ) { return false ; } let k = A - B ; if ( k == 0 ) { return true ; } let val = Math . floor ( p1 / k ) + Math . floor ( p2 / k ) ; if ( val >= Mid ) { return true ; } return false ; } function maxPossibleMoves ( X , Y , A , B ) { let ans = 0 ; let L = 1 , R = MAXN ; while ( L <= R ) { let Mid = Math . floor ( ( L + R ) / 2 ) ; if ( can ( Mid , X , Y , A , B ) ) { L = Mid + 1 ; ans = Math . max ( ans , Mid ) ; } else { R = Mid - 1 ; } } return ans ; } let X = 10 , Y = 12 , A = 2 , B = 5 ; if ( A < B ) { let temp = A ; A = B ; B = temp ; } document . write ( maxPossibleMoves ( X , Y , A , B ) ) ;
Find two proper factors of N such that their sum is coprime with N | Function to find two proper factors of N such that their sum is coprime with N ; Find factors in sorted order ; Find largest value of d2 such that d1 and d2 are co - prime ; Check if d1 and d2 are proper factors of N ; Print answer ; No such factors exist ; Driver code ; Function Call
function printFactors ( n ) { for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { let d1 = i , d2 = n ; while ( d2 % d1 == 0 ) { d2 = Math . floor ( d2 / d1 ) ; } if ( d1 > 1 && d2 > 1 ) { document . write ( d1 + " " + d2 ) ; return ; } } } document . write ( - 1 ) ; } let N = 10 ; printFactors ( N ) ;
Count of triplets in an Array with odd sum | Function to count the number of unordered triplets such that their sum is an odd integer ; Count the number of odd and even integers in the array ; Number of ways to create triplets using one odd and two even integers ; Number of ways to create triplets using three odd integers ; Return answer ; Driver Code ; Function Call ; Print Answer
function countTriplets ( arr , n ) { let odd = 0 , even = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; else even ++ ; } let c1 = Math . floor ( odd * ( even * ( even - 1 ) ) / 2 ) ; let c2 = Math . floor ( ( odd * ( odd - 1 ) * ( odd - 2 ) ) / 6 ) ; return c1 + c2 ; } let arr = [ 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 ] ; let n = arr . length ; let ans = countTriplets ( arr , n ) ; document . write ( ans ) ;
Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
function maxUniqueElements ( A , N ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( A [ i ] ) ) { mp . set ( mp . get ( A [ i ] ) , mp . get ( A [ i ] ) + 1 ) ; } else { mp . set ( A [ i ] , 1 ) ; } } let cnt = 0 ; for ( let [ key , value ] of mp ) { if ( value % 2 == 0 ) { cnt ++ ; } } let ans = mp . size ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } let N = 5 ; let A = [ 1 , 2 , 1 , 3 , 7 ] ; document . write ( maxUniqueElements ( A , N ) ) ;
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps | Function to count the number of cells in the grid such that maximum cell is reachable with a jump of K ; Maximum reachable rows from the current row ; Stores the count of cell that are reachable from the current row ; Count of reachable rows ; Update the maximum value ; Add it to the count ; Maximum reachable columns from the current column ; Stores the count of cell that are reachable from the current column ; Count of rechable columns ; Update the maximum value ; Add it to the count ; Return the total count of cells ; Driver Code
function countCells ( n , m , s ) { let mx1 = - 1 ; let cont1 = 0 ; for ( let i = 0 ; i < s && i < n ; ++ i ) { let aux = Math . floor ( ( n - ( i + 1 ) ) / s + 1 ) ; if ( aux > mx1 ) { mx1 = cont1 = aux ; } else if ( aux == mx1 ) cont1 += aux ; } let mx2 = - 1 ; let cont2 = 0 ; for ( let i = 0 ; i < s && i < m ; ++ i ) { let aux = Math . floor ( ( m - ( i + 1 ) ) / s + 1 ) ; if ( aux > mx2 ) mx2 = cont2 = aux ; else if ( aux == mx2 ) cont2 += aux ; } return cont1 * cont2 ; } let N = 5 , M = 5 , K = 2 ; document . write ( countCells ( N , M , K ) ) ;
Count of pairs from first N natural numbers with remainder at least K | Function to count the number of pairs ( a , b ) such that a % b is at least K ; Base Case ; Stores resultant count of pairs ; Iterate over the range [ K + 1 , N ] ; Find the cycled elements ; Find the remaining elements ; Return the resultant possible count of pairs ; Driver Code
function countTotalPairs ( N , K ) { if ( K == 0 ) { return N * N ; } let ans = 0 ; for ( let b = K + 1 ; b <= N ; b ++ ) { ans += Math . floor ( ( N / b ) * ( b - K ) ) ; ans += Math . max ( N % b - K + 1 , 0 ) ; } return ans ; } let N = 5 , K = 2 ; document . write ( countTotalPairs ( N , K ) ) ;
Maximum number of pairs of distinct array elements possible by including each element in only one pair |  ; stores the frequency array ; maxi stores the maximum frequency of an element ; it stores the sum of all the frequency other than the element which has maximum frequency ; there will be always zero or more element which will not participate in making pairs ; if n is odd then except one element we can always form pair for every element if n is even then all the elements can form pair
let arr = [ 4 , 2 , 4 , 1 , 4 , 3 ] ; let n = arr . length ; let maxi = 0 , remain , ans ; let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } for ( let it of freq ) { maxi = Math . max ( maxi , it [ 1 ] ) ; } remain = n - maxi ; if ( maxi >= remain ) { ans = remain ; } else { ans = Math . floor ( n / 2 ) ; } document . write ( ans ) ; freq . clear ( ) ;
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code
function minSumDifference ( ar , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; let rem = sum % n ; return rem * ( n - rem ) ; } let arr = [ 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 ] ; let N = arr . length ; document . write ( minSumDifference ( arr , N ) ) ;
Count N | JavaScript program for the above approache ; Function to find the value of x ^ y ; Stores the value of x ^ y ; Iterate until y is positive ; If y is odd ; Divide y by 2 ; Return the value of x ^ y ; Function to find the number of N - digit integers satisfying the given criteria ; Count of even positions ; Count of odd positions ; Return the resultant count ; Driver Code
var m = 10 ** 9 + 7 function power ( x , y ) { var res = 1 while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % m y = y >> 1 x = ( x * x ) % m } return res } function countNDigitNumber ( N ) { var ne = Math . floor ( N / 2 ) + N % 2 var no = Math . floor ( N / 2 ) return power ( 4 , ne ) * power ( 5 , no ) } let N = 5 document . write ( countNDigitNumber ( N ) % m ) ;
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Utility function to find minimum steps ; Initialize count and result ; Traverse over the range [ 1 , N ] ; Update res ; Increment count ; Return res ; Input ; Function call
function minSteps ( N ) { let count = 1 , res = 0 ; for ( let i = 1 ; i <= N ; i += count ) { res = Math . max ( res , count ) ; count ++ ; } return res ; } let N = 6 ; document . write ( minSteps ( N ) ) ;
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Utility function to find minimum steps ; Input integer ; Function call
function minSteps ( N ) { var res = parseInt ( ( Math . sqrt ( 1 + 8 * N ) - 1 ) / 2 ) ; return res ; } var N = 6 ; document . write ( minSteps ( N ) ) ;
The dice problem | Function to find number written on the opposite face of the dice ; Stores number on opposite face of dice ; Print the answer ; Given value of N ; Function call to find number written on the opposite face of the dice
function oppositeFaceOfDice ( N ) { let ans = 7 - N ; document . write ( ans ) ; } let N = 2 ; oppositeFaceOfDice ( N ) ;
Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | Function to count points from the range [ X , Y ] that can be reached by moving by L or R steps ; Initialize difference array ; Initialize Count ; Marking starting point ; Iterating from X to Y ; Accumulate difference array ; If diff_arr [ i ] is greater than 1 ; Updating difference array ; Visited point found ; Given Input ; Function Call
function countReachablePoints ( X , Y , L , R ) { let diff_arr = new Array ( 100000 ) . fill ( 0 ) ; let count = 0 ; diff_arr [ X ] = 1 ; diff_arr [ X + 1 ] = - 1 ; for ( let i = X ; i <= Y ; i ++ ) { diff_arr [ i ] += diff_arr [ i - 1 ] ; if ( diff_arr [ i ] >= 1 ) { diff_arr [ i + L ] += 1 ; diff_arr [ i + R + 1 ] -= 1 ; count ++ ; } } return count ; } let X = 3 , Y = 12 , L = 2 , R = 3 ; document . write ( countReachablePoints ( X , Y , L , R ) ) ;
Multiply a single digit number in place to a number represented as a Linked List | Node of a Linked List ; Function to create a new node with given data ; Initialize new node ; Set the data field of the node ; Return the new node ; Function to reverse the linked list ; Traverse until curr != null ; Return the head of the reversed linked list ; Utility function to multiply a single digit to a linked list ; Store the head of list ; Stores the address of previous node ; Initially set carry as 0 and product as 1 ; Multiply M with each digit ; Add carry to product if carry exist ; Update carry for next calculation ; Update the value of each nodes ; Move head and temp pointers to next nodes ; If some carry is still there , add a new node to list ; Return head of the resultant list ; Function to multiply a single digit to a linked list ; Reverse linked list ; Multiply M from left to right of reversed list ; Reverse the modified list and return its head ; Function to prvar the linked list ; Given Input list : 1.2 . 7.3 ; Function Call ; Prvar resultant list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } function reverse ( head ) { var prev = null ; var current = head ; var next ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } function multiplyHelp ( head , M ) { var res = head ; var prev = null ; var carry = 0 , product = 1 ; while ( head != null ) { product = head . data * M ; product += carry ; carry = parseInt ( product / 10 ) ; head . data = product % 10 ; prev = head ; head = head . next ; } if ( carry > 0 ) prev . next = newNode ( carry ) ; return res ; } function multiply ( head , M ) { head = reverse ( head ) ; head = multiplyHelp ( head , M ) ; return reverse ( head ) ; } function printList ( node ) { while ( node != null ) { document . write ( node . data ) ; node = node . next ; } } var head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 7 ) ; head . next . next . next = newNode ( 3 ) ; var M = 3 ; head = multiply ( head , M ) ; printList ( head ) ;
Count of triplets having sum of product of any two numbers with the third number equal to N | Javascript program for the above approach ; Function to find the SPF [ i ] using the Sieve Of Erastothenes ; Stores whether i is prime or not ; Initializing smallest factor as 2 for all even numbers ; Iterate for all odd numbers < N ; SPF of i for a prime is the number itself ; Iterate for all the multiples of the current prime number ; The value i is smallest prime factor for i * j ; Function to generate prime factors and its power ; Current prime factor of N ; Stores the powers of the current prime factor ; Find all the prime factors and their powers ; Return the total count of factors ; Function to count the number of triplets satisfying the given criteria ; Stores the count of resultant triplets ; Add the count all factors of N - z to the variable CountTriplet ; Return total count of triplets ; Driver Code ; Find the SPF [ i ] ; Function Call
let s = new Array ( 11 ) . fill ( 0 ) function sieveOfEratosthenes ( N ) { let prime = new Array ( N + 1 ) . fill ( false ) ; for ( let i = 2 ; i < N + 1 ; i += 2 ) s [ i ] = 2 ; for ( let i = 3 ; i < N + 1 ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( let j = i ; j < Math . floor ( N / i + 1 ) ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } function generatePrimeFactors ( N ) { let curr = s [ N ] ; let cnt = new Map ( ) ; cnt . set ( s [ N ] , 1 ) ; while ( N > 1 ) { N = Math . floor ( N / s [ N ] ) ; if ( N != 0 && s [ N ] != 0 ) if ( ! cnt . has ( s [ N ] ) ) cnt . set ( s [ N ] , 1 ) ; else cnt . set ( s [ N ] , cnt . get ( s [ N ] ) + 1 ) ; } if ( cnt . has ( 0 ) ) cnt . delete ( 0 ) ; let totfactor = 1 ; for ( i of cnt . values ( ) ) totfactor *= i + 1 ; return totfactor ; } function countTriplets ( N ) { let CountTriplet = 0 ; for ( let z = 1 ; z < N + 1 ; z ++ ) { let p = generatePrimeFactors ( N - z ) ; if ( p > 1 ) CountTriplet += p ; } return CountTriplet + 1 ; } let N = 10 ; sieveOfEratosthenes ( N ) ; document . write ( ( countTriplets ( N ) ) ) ;
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | Function to find the minimum length of the subarray to be changed . ; Stores the frequencies of array elements ; Stores the number of array elements that are present more than N / M times ; Iterate over the range ; Increment the frequency ; If the frequency of all array elements are already N / M ; Stores the resultant length of the subarray ; The left and right pointers ; Iterate over the range ; If the current element is ; If the value of c is 0 , then find the possible answer ; Iterate over the range ; If the element at left is making it extra ; Update the left pointer ; Update the right pointer ; Return the resultant length ; Driver Code
function minimumSubarray ( arr , n , m ) { let mapu = new Array ( m + 1 ) . fill ( 0 ) ; let c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { mapu [ arr [ i ] ] ++ ; if ( mapu [ arr [ i ] ] == ( n / m ) + 1 ) c ++ ; } if ( c == 0 ) return 0 ; let ans = n ; let l = 0 , r = 0 ; while ( r < n ) { if ( -- mapu [ arr [ r ] ] == ( n / m ) ) c -- ; if ( c == 0 ) { while ( l <= r && c == 0 ) { ans = Math . min ( ans , r - l + 1 ) ; if ( ++ mapu [ arr [ l ] ] > ( n / m ) ) c ++ ; l ++ ; } } r ++ ; } return ans ; } let arr = [ 1 , 1 , 2 , 1 , 1 , 2 ] ; let M = 2 ; let N = arr . length ; document . write ( minimumSubarray ( arr , N , M ) ) ;
Check if the sum of K least and most frequent array elements are equal or not | Function to compare the sum of K most and least occurrences ; Stores frequency of array element ; Stores the frequencies as indexes and putelements with the frequency in a vector ; Find the frequency ; Insert in the vector ; Stores the count of elements ; Traverse the frequency array ; Find the kleastfreqsum ; If the count is K , break ; Reinitialize the count to zero ; Traverse the frequency ; Find the kmostfreqsum ; If the count is K , break ; Comparing the sum ; Otherwise , return No ; Driver Code
function checkSum ( arr , n , k ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( m . get ( arr [ i ] ) , m . get ( arr [ i ] ) + 1 ) } else { m . set ( arr [ i ] , 1 ) ; } } let freq = new Array ( n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) { let f = m . get ( arr [ i ] ) ; if ( f != - 1 ) { freq [ f ] = new Array ( ) ; freq [ f ] . push ( arr [ i ] ) ; m . set ( arr [ i ] , - 1 ) ; } } let count = 0 ; let kleastfreqsum = 0 ; for ( let i = 0 ; i <= n ; i ++ ) { for ( let x in freq [ i ] ) { kleastfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } count = 0 ; let kmostfreqsum = 0 ; for ( let i = n ; i >= 0 ; i -- ) { for ( let x in freq [ i ] ) { kmostfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } if ( kleastfreqsum == kmostfreqsum ) return " " ; return " " ; } let arr = [ 3 , 2 , 1 , 2 , 3 , 3 , 4 ] ; let N = arr . length ; let K = 2 ; document . write ( checkSum ( arr , N , K ) ) ;
Minimum distance a person has to move in order to take a picture of every racer | Function to return the minimum distance the person has to move int order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver code
function minDistance ( start , intend , n , d ) { let left = Number . MIN_SAFE_INTEGER ; let right = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { left = Math . max ( left , start [ i ] ) ; right = Math . min ( right , end [ i ] ) ; } if ( left > right ) return - 1 ; if ( d >= left && d <= right ) return 0 ; if ( d < left ) return left - d ; if ( d > right ) return d - right ; } let start = [ 0 , 2 , 4 ] ; let end = [ 7 , 14 , 6 ] ; let n = start . length ; let d = 3 ; document . write ( minDistance ( start , end , n , d ) ) ;
Maximum number of Armstrong Numbers present in a subarray of size K | Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code
function power ( x , y ) { if ( y == 0 ) return 1 ; if ( y % 2 == 0 ) return power ( x , Math . floor ( y / 2 ) ) * power ( x , Math . floor ( y / 2 ) ) ; return x * power ( x , Math . floor ( y / 2 ) ) * power ( x , Math . floor ( y / 2 ) ) ; } function order ( num ) { let count = 0 ; while ( num ) { count ++ ; num = Math . floor ( num / 10 ) ; } return count ; } function isArmstrong ( N ) { let r = order ( N ) ; let temp = N , sum = 0 ; while ( temp ) { let d = temp % 10 ; sum += power ( d , r ) ; temp = Math . floor ( temp / 10 ) ; } return sum == N ; } function maxSum ( arr , N , K ) { if ( N < K ) { return - 1 ; } let res = 0 ; for ( let i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } let curr_sum = res ; for ( let i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . max ( res , curr_sum ) ; } return res ; } function maxArmstrong ( arr , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = isArmstrong ( arr [ i ] ) ; } return maxSum ( arr , N , K ) ; } let arr = [ 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 ] ; let K = 6 ; let N = arr . length ; document . write ( maxArmstrong ( arr , N , K ) ) ;
Count N | Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code
function power ( x , y , p ) { let res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function countArrays ( n , k ) { let mod = 1000000007 ; let ans = power ( n , k , mod ) ; return ans ; } let n = 3 , k = 5 ; let ans = countArrays ( n , k ) ; document . write ( ans ) ;
Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | Function to find the factorial of the given number N ; Stores the factorial ; Iterate over the range [ 2 , N ] ; Return the resultant result ; Function to find the number of ways of choosing r objects from n distinct objects ; Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T ; Traverse the string s ; If the current character is 1 ; Update the value of the sum1 ; Otherwise ; Traverse the string t ; If the current character is 1 , then update the value of sum2 ; If the current character is 0 ; Otherwise , update the value of K ; Check if P is greater than K or if K - P is odd ; Print the count of ways ; Driver Code
function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { res = res * i ; } return res ; } function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function countWays ( s , t ) { let n = s . length ; let sum1 = 0 , sum2 = 0 , K = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { sum1 ++ ; } else sum1 -- ; } let m = t . length ; for ( let i = 0 ; i < m ; i ++ ) { if ( t [ i ] == ' ' ) { sum2 ++ ; } else if ( t [ i ] == ' ' ) { sum2 -- ; } else K ++ ; } let P = Math . abs ( sum1 - sum2 ) ; if ( P > K || ( K - P ) % 2 ) { document . write ( 0 ) ; return ; } document . write ( nCr ( K , ( P + K ) / 2 ) ) ; } let S1 = " " ; let S2 = " " ; countWays ( S1 , S2 ) ;
Count pairs of indices having sum of indices same as the sum of elements at those indices | Function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; Stores the total count of pairs ; Iterate over the range ; Iterate over the range ; Print the total count ; Driver Code
function countPairs ( arr , N ) { let answer = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] + arr [ j ] == i + j ) { answer ++ ; } } } document . write ( answer ) ; } let arr = [ 0 , 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; countPairs ( arr , N ) ;