codeparrot/xlcost-text-to-code huggingface dataset:
Euler 's Totient Function | Javascript program to calculate Euler ' s ▁ Totient ▁ Function ▁ using ▁ Euler ' s product formula ; Initialize result as n ; Consider all prime factors of n and for every prime factor p , multiply result with ( 1 - 1 / p ) ; Check if p is a prime factor . ; If yes , then update n and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Driver Code
function phi ( n ) { let result = n ; for ( let p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result *= ( 1.0 - ( 1.0 / p ) ) ; } } if ( n > 1 ) result *= ( 1.0 - ( 1.0 / n ) ) ; return parseInt ( result ) ; } for ( let n = 1 ; n <= 10 ; n ++ ) document . write ( ` ${ n } ${ phi ( n ) } ` ) ;
Program to find remainder without using modulo or % operator | Function to return num % divisor without using % ( modulo ) operator ; While divisor is smaller than n , keep subtracting it from num ; Driver code
function getRemainder ( num , divisor ) { while ( num >= divisor ) num -= divisor ; return num ; } let num = 100 , divisor = 7 ; document . write ( getRemainder ( num , divisor ) ) ;
Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | Utility function to find ; A Simple Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Driver Code
function factorial ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } function sum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += 1.0 / factorial ( i ) ; return sum ; } let n = 5 ; document . write ( sum ( n ) . toFixed ( 5 ) ) ;
Find the number of valid parentheses expressions of given length | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 'th  Catalan Number ; Driver Code
function binomialCoeff ( n , k ) { let res = 1 ; if ( k > n - k ) k = n - k ; for ( let i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function catalan ( n ) { let c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } function findWays ( n ) { if ( n & 1 ) return 0 ; return catalan ( n / 2 ) ; } let n = 6 ; document . write ( " " + n + " " + findWays ( 6 ) ) ;
Program to evaluate simple expressions | A utility function to check if a given character is operand ; utility function to find value of and operand ; This function evaluates simple expressions . It returns - 1 if the given expression is invalid . ; Base Case : Given expression is empty ; The first character must be an operand , find its value ; Traverse the remaining characters in pairs ; The next character must be an operator , and next to next an operand ; If next to next character is not an operand ; Update result according to the operator ; If not a valid operator ; Driver program to test above function
function isOperand ( c ) { return ( c . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && c . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) ; } function value ( c ) { return ( c . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } function evaluate ( exp ) { if ( exp . length == 0 ) return - 1 ; let res = value ( exp [ 0 ] ) ; for ( let i = 1 ; i < exp . length ; i += 2 ) { let opr = exp [ i ] , opd = exp [ i + 1 ] ; if ( isOperand ( opd ) == false ) return - 1 ; if ( opr == ' ' ) res += value ( opd ) ; else if ( opr == ' ' ) res -= value ( opd ) ; else if ( opr == ' ' ) res *= value ( opd ) ; else if ( opr == ' ' ) res /= value ( opd ) ; else return - 1 ; } return res ; } let expr1 = " " ; let res = evaluate ( expr1 ) ; if ( res == - 1 ) document . write ( expr1 + " " ) ; else document . write ( " " + expr1 + " " + res + " " ) ; let expr2 = " " ; res = evaluate ( expr2 ) ; if ( res == - 1 ) document . write ( expr2 + " " ) ; else document . write ( " " + expr2 + " " + res + " " ) ; let expr3 = " " ; res = evaluate ( expr3 ) ; if ( res == - 1 ) document . write ( expr3 + " " ) ; else document . write ( " " + expr3 + " " + res + " " ) ; let expr4 = " " ; res = evaluate ( expr4 ) ; if ( res == - 1 ) document . write ( expr4 + " " ) ; else document . write ( " " + expr4 + " " + res + " " ) ;
Program to print first n Fibonacci Numbers | Set 1 | Function to print first n Fibonacci Numbers ; Driver Code
function printFibonacciNumbers ( n ) { let f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; document . write ( f1 + " " ) ; for ( i = 1 ; i < n ; i ++ ) { document . write ( f2 + " " ) ; let next = f1 + f2 ; f1 = f2 ; f2 = next ; } } printFibonacciNumbers ( 7 ) ;
Program to find LCM of two numbers | Recursive function to return gcd of a and b ; Function to return LCM of two numbers ; Driver program to test above function
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function lcm ( a , b ) { return ( a / gcd ( a , b ) ) * b ; } let a = 15 , b = 20 ; document . write ( " " + a + " " + b + " " + lcm ( a , b ) ) ;
Program to find sum of elements in a given array | javascript Program to find sum of elements in a given array ; Driver code ; calling accumulate function , passing first , last element and initial sum , which is 0 in this case
function accumulate ( arr ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { sum = sum + arr [ i ] ; } return sum ; } let arr = [ 12 , 3 , 4 , 15 ] ; let n = arr . length ; document . write ( " " + accumulate ( arr ) ) ;
Check if a number is multiple of 5 without using / and % operators | Assuming that integer takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code
MAX = 11 ; function isMultipleof5 ( n ) { str = Array ( n ) . fill ( ' ' ) ; var len = str . length ; if ( str [ len - 1 ] == ' ' str [ len - 1 ] == ' ' ) return true ; return false ; } var n = 19 ; if ( isMultipleof5 ( n ) == true ) document . write ( n + " " + " " ) ; else document . write ( n + " " + " " ) ;
Check if there are T number of continuous of blocks of 0 s or not in given Binary Matrix | Javascript program for the above approach ; Stores the moves in the matrix ; Function to find if the current cell lies in the matrix or not ; Function to perform the DFS Traversal ; Iterate over the direction vector ; DFS Call ; Function to check if it satisfy the given criteria or not ; Keeps the count of cell having value as 0 ; If condition doesn 't satisfy ; Keeps the track of unvisted cell having values 0 ; Increasing count of black_spot ; Find the GCD of N and M ; Prlet the result ; Driver Code
let M = 1e9 + 7 ; let directions = [ [ 0 , 1 ] , [ - 1 , 0 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ 1 , 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , - 1 ] , ] ; function isInside ( i , j , N , M ) { if ( i >= 0 && i < N && j >= 0 && j < M ) { return true ; } return false ; } function DFS ( mat , N , M , i , j , visited ) { if ( visited [ i ] [ j ] == true ) { return ; } visited [ i ] [ j ] = true ; for ( let it of directions ) { let I = i + it [ 0 ] ; let J = j + it [ 1 ] ; if ( isInside ( I , J , N , M ) ) { if ( mat [ I ] [ J ] == 0 ) { DFS ( mat , N , M , I , J , visited ) ; } } } } function check ( N , M , mat ) { let black = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) { black ++ ; } } } if ( black < 2 * Math . max ( N , M ) ) { document . write ( " " ) ; return ; } let visited = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < M ; j ++ ) { temp . push ( false ) ; } visited . push ( temp ) ; } let black_spots = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( visited [ i ] [ j ] == false && mat [ i ] [ j ] == 0 ) { black_spots ++ ; DFS ( mat , N , M , i , j , visited ) ; } } } let T = __gcd ( N , M ) ; cout << ( black_spots >= T ? " " : " " ) ; } let N = 3 ; M = 3 ; let mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 0 , 0 , 1 ] , ] ; check ( M , N , mat ) ;
Count subsequences having odd Bitwise OR values in an array | Function to count the subsequences having odd bitwise OR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array arr [ ] ; If element is odd ; Return the final answer ; Given array arr [ ]
function countSubsequences ( arr ) { let odd = 0 ; let even = 0 ; for ( let x of arr ) { if ( x & 1 ) odd ++ ; else even ++ ; } return ( ( 1 << odd ) - 1 ) * ( 1 << even ) ; } let arr = [ 2 , 4 , 1 ] ; document . write ( countSubsequences ( arr ) ) ;
Compress a Binary Tree from top to bottom with overlapping condition | Structure of a node of th tree ; Function to compress all the nodes on the same vertical line ; Stores node by compressing all nodes on the current vertical line ; Check if i - th bit of current bit set or not ; Iterate over the range [ 0 , 31 ] ; Stores count of set bits at i - th positions ; Stores count of clear bits at i - th positions ; Traverse the array ; If i - th bit of current element is set ; Update S ; Update NS ; If count of set bits at i - th position is greater than count of clear bits ; Update ans ; Update getBit ; Function to compress all the nodes on the same vertical line with a single node that satisfies the condition ; Map all the nodes on the same vertical line ; Function to traverse the tree and map all the nodes of same vertical line to vertical distance ; Storing the values in the map ; Recursive calls on left and right subtree ; Getting the range of horizontal distances ; Driver Code ; Function Call
class TreeNode { constructor ( val = " " , left = null , right = null ) { this . val = val ; this . left = left ; this . right = right ; } } function evalComp ( arr ) { let ans = 0 ; let getBit = 1 ; for ( let i = 0 ; i < 32 ; i ++ ) { let S = 0 ; let NS = 0 ; for ( j of arr ) { if ( getBit & j ) S += 1 ; else NS += 1 ; } if ( S > NS ) ans += 2 ** i ; getBit <<= 1 ; } document . write ( ans + " " ) ; } function compressTree ( root ) { let mp = new Map ( ) ; function Trav ( root , hd ) { if ( ! root ) return ; if ( ! mp . has ( hd ) ) mp . set ( hd , [ root . val ] ) ; else { let temp = mp . get ( hd ) ; temp . push ( root . val ) ; mp . set ( hd , temp ) ; } Trav ( root . left , hd - 1 ) ; Trav ( root . right , hd + 1 ) ; } Trav ( root , 0 ) ; let lower = [ ... mp . keys ( ) ] . sort ( ( a , b ) => a - b ) [ 0 ] ; let upper = [ ... mp . keys ( ) ] . sort ( ( a , b ) => b - a ) [ 0 ] ; for ( let i = lower ; i <= upper ; i ++ ) evalComp ( mp . get ( i ) ) ; } let root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 3 ) ; root . right = new TreeNode ( 2 ) ; root . left . left = new TreeNode ( 1 ) ; root . left . right = new TreeNode ( 4 ) ; root . right . left = new TreeNode ( 1 ) ; root . right . right = new TreeNode ( 2 ) ; compressTree ( root ) ;
Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Prlet the answer ; Given Input ; Function Call
function Or_of_Ands_for_each_query ( arr , n , queries , q ) { let bits = new Array ( 32 ) . fill ( 0 ) ; for ( let i = 0 ; i < 32 ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( 1 << i ) & arr [ j ] ) { bits [ i ] ++ ; } } } for ( let p = 0 ; p < q ; p ++ ) { for ( let i = 0 ; i < 32 ; i ++ ) { if ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) { bits [ i ] -- ; } if ( queries [ p ] [ 1 ] & ( 1 << i ) ) { bits [ i ] ++ ; } } arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ] ; let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { if ( bits [ i ] != 0 ) { ans |= ( 1 << i ) ; } } document . write ( ans + " " ) ; } } let n = 3 , q = 2 ; let arr = [ 3 , 5 , 7 ] ; let queries = [ [ 1 , 2 ] , [ 2 , 1 ] ] ; Or_of_Ands_for_each_query ( arr , n , queries , q ) ;
Find XOR sum of Bitwise AND of all pairs from given two Arrays | Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; variable to store anshu ; when there has been no AND of pairs before this ; Input ; Function call
function XorSum ( A , B , N , M ) { let ans = - 1 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( ans == - 1 ) ans = ( A [ i ] & B [ j ] ) ; else ans ^= ( A [ i ] & B [ j ] ) ; } } return ans ; } let A = [ 3 , 5 ] ; let B = [ 2 , 3 ] ; let N = A . length ; let M = B . length ; document . write ( XorSum ( A , B , N , M ) ) ;
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 | Function to count set bits in a number ; Stores count of set bits in xo ; Iterate over each bits of xo ; If current bit of xo is 1 ; Update count ; Update xo ; Function to find length of shortest path between the nodes a and b ; Stores XOR of a and b ; Stores the count of set bits in xorVal ; If cnt is an even number ; Given N ; Given a and b ; Function call
function countbitdiff ( xo ) { let count = 0 ; while ( xo ) { if ( xo % 2 == 1 ) { count ++ ; } xo = Math . floor ( xo / 2 ) ; } return count ; } function shortestPath ( n , a , b ) { let xorVal = a ^ b ; let cnt = countbitdiff ( xorVal ) ; if ( cnt % 2 == 0 ) document . write ( cnt / 2 + " " ) ; else document . write ( " " + " " ) ; } let n = 15 ; let a = 15 , b = 3 ; shortestPath ( n , a , b ) ;
Modify a matrix by converting each element to XOR of its digits | JavaScript program to implement the above approach ; Function to calculate Bitwise XOR of digits present in X ; Stores the Bitwise XOR ; While X is true ; Update Bitwise XOR of its digits ; Return the result ; Function to print matrix after converting each matrix element to XOR of its digits ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Function to convert the given matrix to required XOR matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Find the xor of digits present in X ; Stores the XOR value ; Print resultant matrix ; Driver code
let M = 3 ; let N = 3 ; function findXOR ( X ) { let ans = 0 ; while ( X != 0 ) { ans ^= ( X % 10 ) ; X /= 10 ; } return ans ; } function printXORmatrix ( arr ) { for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { document . write ( arr [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function convertXOR ( arr ) { for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { let X = arr [ i ] [ j ] ; let temp = findXOR ( X ) ; arr [ i ] [ j ] = temp ; } } prletXORmatrix ( arr ) ; } let arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ] ; convertXOR ( arr ) ; ;
Count subarrays made up of elements having exactly K set bits | Function to count the number of set bits in an integer N ; Stores the count of set bits ; While N is non - zero ; If the LSB is 1 , then increment ans by 1 ; Return the total set bits ; Function to count the number of subarrays having made up of elements having K set bits ; Stores the total count of resultant subarrays ; Traverse the given array ; If the current element has K set bits ; Otherwise ; Increment count of subarrays ; Return total count of subarrays ; Driver Code ; Function Call
function countSet ( N ) { let ans = 0 ; while ( N ) { ans += N & 1 ; N >>= 1 ; } return ans ; } function countSub ( arr , k ) { let ans = 0 ; let setK = 0 ; for ( let i = 0 ; i < 5 ; i ++ ) { if ( countSet ( arr [ i ] ) == k ) setK += 1 ; else setK = 0 ; ans += setK ; } return ans ; } let arr = [ 4 , 2 , 1 , 5 , 6 ] ; let K = 2 ; document . write ( ( countSub ( arr , K ) ) ) ;
Count subarrays having odd Bitwise XOR | Function to count the number of subarrays of the given array having odd Bitwise XOR ; Stores number of odd numbers upto i - th index ; Stores number of required subarrays starting from i - th index ; Store the required result ; Find the number of subarrays having odd Bitwise XOR values starting at 0 - th index ; Check if current element is odd ; If the current value of odd is not zero , increment c_odd by 1 ; Find the number of subarrays having odd bitwise XOR value starting at ith index and add to result ; Add c_odd to result ; Print the result ; Given array ; Stores the size of the array
function oddXorSubarray ( a , n ) { var odd = 0 ; var c_odd = 0 ; var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 ) { odd = ( odd == 0 ) ? 1 : 0 ; } if ( odd != 0 ) { c_odd ++ ; } } for ( i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 != 0 ) { c_odd = ( n - i - c_odd ) ; } } document . write ( result ) ; } var arr = [ 1 , 4 , 7 , 9 , 10 ] ; var N = arr . length ; oddXorSubarray ( arr , N ) ;
Sum of Bitwise OR of every array element paired with all other array elements | Function to prlet required sum for every valid index i ; Store the required sum for current array element ; Generate all possible pairs ( arr [ i ] , arr [ j ] ) ; Update the value of req_sum ; Print the required sum ; Given array ; Size of the array ; Function Call
function prletORSumforEachElement ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let req_sum = 0 ; for ( let j = 0 ; j < N ; j ++ ) { req_sum += ( arr [ i ] arr [ j ] ) ; } document . write ( req_sum + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; prletORSumforEachElement ( arr , N ) ;
Sum of Bitwise OR of each array element of an array with all elements of another array | Function to compute sum of Bitwise OR of each element in arr1 [ ] with all elements of the array arr2 [ ] ; Declaring an array of size 32 to store the count of each bit ; Traverse the array arr1 [ ] ; Current bit position ; While num exceeds 0 ; Checks if i - th bit is set or not ; Increment the count at bit_position by one ; Increment bit_position ; Right shift the num by one ; Traverse in the arr2 [ ] ; Store the ith bit value ; Total required sum ; Traverse in the range [ 0 , 31 ] ; Check if current bit is set ; Increment the Bitwise sum by N * ( 2 ^ i ) ; Right shift num by one ; Left shift valee_at_that_bit by one ; Print the sum obtained for ith number in arr1 [ ] ; Given arr1 [ ] ; Given arr2 [ ] ; Size of arr1 [ ] ; Size of arr2 [ ] ; Function Call
function Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) { let frequency = new Array ( 32 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { let bit_position = 0 ; let num = arr1 [ i ] ; while ( num ) { if ( num & 1 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( let i = 0 ; i < M ; i ++ ) { let num = arr2 [ i ] ; let value_at_that_bit = 1 ; let bitwise_OR_sum = 0 ; for ( let bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( num & 1 ) { bitwise_OR_sum += N * value_at_that_bit ; } else { bitwise_OR_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } document . write ( bitwise_OR_sum + ' ' ) ; } return ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 1 , 2 , 3 ] ; let N = arr1 . length ; let M = arr2 . length ; Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) ;
Count pairs from given array with Bitwise OR equal to K | Function that counts the pairs from the array whose Bitwise OR is K ; Stores the required count of pairs ; Generate all possible pairs ; Perform OR operation ; If Bitwise OR is equal to K , increment count ; Prlet the total count ; Driver Code ; Function Call
function countPairs ( arr , k , size ) { let count = 0 , x ; for ( let i = 0 ; i < size - 1 ; i ++ ) { for ( let j = i + 1 ; j < size ; j ++ ) { x = arr [ i ] | arr [ j ] ; if ( x == k ) count ++ ; } } document . write ( count ) ; } let arr = [ 2 , 38 , 44 , 29 , 62 ] ; let K = 46 ; let N = arr . length ; countPairs ( arr , K , N ) ;
Count nodes having Bitwise XOR of all edges in their path from the root equal to K | Initialize the adjacency list to represent the tree ; Marks visited / unvisited vertices ; Stores the required count of nodes ; DFS to visit each vertex ; Mark the current node as visited ; Update the counter xor is K ; Visit adjacent nodes ; Calculate Bitwise XOR of edges in the path ; Recursive call to dfs function ; Function to construct the tree and print required count of nodes ; Add edges ; Print answer ; Given K and R ; Given edges ; Number of vertices ; Function call
let adj = [ ] ; for ( let i = 0 ; i < 100005 ; i ++ ) { adj . push ( [ ] ) } let visited = new Array ( 100005 ) . fill ( 0 ) ; let ans = 0 ; function dfs ( node , xorr , k ) { visited [ node ] = 1 ; if ( node != 1 && xorr == k ) ans ++ ; for ( let x of adj [ node ] ) { if ( ! visited [ x [ 0 ] ] ) { let xorr1 = xorr ^ x [ 1 ] ; dfs ( x [ 0 ] , xorr1 , k ) ; } } } function countNodes ( N , K , R , edges ) { for ( let i = 0 ; i < N - 1 ; i ++ ) { let u = edges [ i ] [ 0 ] , v = edges [ i ] [ 1 ] , w = edges [ i ] [ 2 ] ; adj [ u ] . push ( [ v , w ] ) ; adj [ v ] . push ( [ u , w ] ) ; } dfs ( R , 0 , K ) ; document . write ( ans + " " ) ; } let K = 0 , R = 1 ; let edges = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 1 ] , [ 2 , 4 , 3 ] , [ 2 , 5 , 4 ] , [ 3 , 6 , 1 ] , [ 3 , 7 , 2 ] ] ; let N = edges . length ; countNodes ( N , K , R , edges ) ;
Bitwise operations on Subarrays of size K | Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize the beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Print the minimum XOR ; Given array arr [ ] ; Given subarray size K ; Function Call
function findMinXORSubarray ( arr , n , k ) { if ( n < k ) return ; let res_index = 0 ; let curr_xor = 0 ; for ( let i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; let min_xor = curr_xor ; for ( let i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } document . write ( min_xor ) ; } let arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] ; let k = 3 ; let n = arr . length ; findMinXORSubarray ( arr , n , k ) ;
Maximum number of consecutive 1 s after flipping all 0 s in a K length subarray | Function to find the maximum number of consecutive 1 's after flipping all zero in a K length subarray ; Initialize variable ; Iterate unil n - k + 1 as we have to go till i + k ; Iterate in the array in left direction till you get 1 else break ; Iterate in the array in right direction till you get 1 else break ; Compute the maximum length ; Return the length ; Driver code ; Array initialization ; Size of array
function findmax ( arr , n , k ) { let trav , i ; let c = 0 , maximum = 0 ; for ( i = 0 ; i < n - k + 1 ; i ++ ) { trav = i - 1 ; c = 0 ; while ( trav >= 0 && arr [ trav ] == 1 ) { trav -- ; c ++ ; } trav = i + k ; while ( trav < n && arr [ trav ] == 1 ) { trav ++ ; c ++ ; } c += k ; if ( c > maximum ) maximum = c ; } return maximum ; } let k = 3 ; let arr = [ 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ] ; let n = arr . length ; let ans = findmax ( arr , n , k ) ; document . write ( ans )