codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost required to move all elements to the same position | Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Given array ; Function Call
function minCost ( arr ) { let odd = 0 , even = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } document . write ( Math . min ( even , odd ) ) ; } let arr = [ 1 , 2 , 3 ] ; minCost ( arr ) ;
Maximize array sum by alternating the signs of adjacent elements | Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Driver Code
function findMaxSumByAlternatingSign ( arr , N ) { let cntNeg = 0 ; let MaxAltSum = 0 ; let SmValue = 0 ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += Math . abs ( arr [ i ] ) ; SmValue = Math . min ( SmValue , Math . abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg % 2 == 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } let arr = [ 1 , 1 , - 2 , - 4 , 5 ] ; let N = arr . length ; document . write ( findMaxSumByAlternatingSign ( arr , N ) ) ;
Construct array with sum of product of same indexed elements in the given array equal to zero | Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Prlet new array elements ; Driver Code
function constructNewArraySumZero ( arr , N ) { let newArr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( newArr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , - 5 , - 6 , 8 ] ; let N = arr . length ; constructNewArraySumZero ( arr , N ) ;
Generate Bitonic Sequence of length N from integers in a given range | Function to construct bitonic sequence of length N from integers in the range [ L , R ] ; If sequence is not possible ; Store the resultant list ; If size of deque < n ; Add elements from start ; Print the stored in the list ; Driver Code ; Function Call
function bitonicSequence ( num , lower , upper ) { if ( num > ( upper - lower ) * 2 + 1 ) { document . write ( - 1 ) ; return ; } var ans = [ ] ; for ( var i = 0 ; i < Math . min ( upper - lower + 1 , num - 1 ) ; i ++ ) ans . push ( upper - i ) ; for ( var i = 0 ; i < num - ans . length ; i ++ ) { ans . splice ( 0 , 0 , upper - i - 1 ) } document . write ( ' ' ) ; ans . forEach ( element => { document . write ( " " + element ) ; } ) ; document . write ( ' ' + ' ' ) ; } var N = 5 , L = 3 , R = 10 ; bitonicSequence ( N , L , R ) ;
Count substrings of same length differing by a single character from two given strings | Function to count the number of subStrings of equal length which differ by a single character ; Stores the count of pairs of subStrings ; Traverse the String s ; Traverse the String t ; Different character ; Increment the answer ; Count equal subStrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the readonly count ; Driver Code ; Function Call
function countSubStrings ( s , t ) { let answ = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { for ( let j = 0 ; j < t . length ; j ++ ) { if ( t [ j ] != s [ i ] ) { answ += 1 ; let k = 1 ; let z = - 1 ; let q = 1 ; while ( j + z >= 0 && 0 <= i + z && s [ i + z ] == t [ j + z ] ) { z -= 1 ; answ += 1 ; q += 1 ; } while ( s . length > i + k && j + k < t . length && s [ i + k ] == t [ j + k ] ) { k += 1 ; answ += q ; z = - 1 ; } } } } return answ ; } let S = " " ; let T = " " ; document . write ( countSubStrings ( S , T ) ) ;
Minimum flips to make all 1 s in right and 0 s in left | Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and 1 s on the right ; Stores count of 1 s on the left side of each index ; Stores count of flips required to make all 0 s on the left and 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver Code
function minimumCntOfFlipsRequired ( str ) { let n = str . length ; let zeros = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } let minFlips = Number . MAX_VALUE ; let currOnes = 0 ; let flips ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . min ( minFlips , flips ) ; } return minFlips ; } let s1 = " " ; document . write ( minimumCntOfFlipsRequired ( s1 ) ) ;
Maximize length of longest non | Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start poleter ; Stores index of end poleter ; Stores previously inserted element leto the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted leto the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted leto the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
function findLongestNonDecreasing ( A , N ) { let res = 0 ; let start = 0 ; let end = N - 1 ; let prev = - 1 ; while ( start <= end ) { if ( A [ start ] <= A [ end ] ) { if ( prev == - 1 ) { prev = A [ start ] ; res ++ ; start ++ ; } else { if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else { break ; } } } else { if ( prev == - 1 ) { prev = A [ end ] ; res ++ ; end -- ; } else { if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else { break ; } } } } return res ; } let A = [ 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 ] ; let N = A . length ; document . write ( findLongestNonDecreasing ( A , N ) ) ;
Non | Function to print all pairs whose sum of Bitwise OR and AND is N ; Iterate from i = 0 to N ; Print pairs ( i , N - i ) ; Driver Code
function findPairs ( N ) { for ( let i = 0 ; i <= N ; i ++ ) { document . write ( " " + i + " " + ( N - i ) + " " ) ; } } let N = 5 ; findPairs ( N ) ;
Minimum index to split array into subarrays with co | Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver Code ; Function call
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function findPartition ( nums , N ) { var prefix = Array ( N ) , suffix = Array ( N ) , i , k ; prefix [ 0 ] = nums [ 0 ] ; for ( i = 1 ; i < N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] * nums [ i ] ; } suffix [ N - 1 ] = nums [ N - 1 ] ; for ( i = N - 2 ; i >= 0 ; i -- ) { suffix [ i ] = suffix [ i + 1 ] * nums [ i ] ; } for ( k = 0 ; k < N - 1 ; k ++ ) { if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) { return k ; } } return - 1 ; } var arr = [ 2 , 3 , 4 , 5 ] ; var N = arr . length ; document . write ( findPartition ( arr , N ) ) ;
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having letegers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to prlet the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Given array arr [ ] ; Size of array ; Function Call
function separate ( arr , n , parity ) { let count = 1 , res = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) != 0 && ( ( arr [ i - 1 ] + parity ) & 1 ) != 0 ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } function prletArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function requiredOps ( arr , N ) { let res1 = separate ( arr , N , 0 ) ; let res2 = separate ( arr , N , 1 ) ; document . write ( Math . max ( res1 , res2 ) ) ; } let arr = [ 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 ] ; let N = arr . length ; requiredOps ( arr , N ) ;
Count prime pairs whose difference is also a Prime Number | Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code
function SieveOfEratosthenes ( N ) { let isPrime = [ ] ; for ( let i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( let p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( let i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } function cntPairsdiffOfPrimeisPrime ( N ) { let cntPairs = 0 ; let isPrime = SieveOfEratosthenes ( N ) ; for ( let i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } let N = 5 ; document . write ( cntPairsdiffOfPrimeisPrime ( N ) ) ;
Length of longest subsequence consisting of distinct adjacent elements | Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Prlet the maximum length ; Driver Code ; Size of Array ; Function Call
function longestSubsequence ( arr , N ) { let count = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } document . write ( count ) ; } let arr = [ 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 ] ; let N = arr . length ; longestSubsequence ( arr , N ) ;
Count of substrings having the most frequent character in the string as first character | Function to find all substrings whose first character occurs maximum number of times ; Stores frequency of characters ; Stores character that appears maximum number of times ; Stores max frequency of character ; Updates frequency of characters ; Update maxfreq ; Character that occures maximum number of times ; Update the maximum frequency character ; Stores all count of substrings ; Traverse over string ; Get the current character ; Update count of substrings ; Return the count of all valid substrings ; Driver Code ; Function Call
function substringCount ( s ) { var freq = new Array ( 26 ) . fill ( 0 ) ; var max_char = " " ; var maxfreq = - 21474836487 ; for ( var i = 0 ; i < s . length ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; if ( maxfreq < freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ) maxfreq = freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( maxfreq === freq [ i ] ) { max_char = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; break ; } } var ans = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { var ch = s [ i ] ; if ( max_char === ch ) { ans += s . length - i ; } } return ans ; } var S = " " ; document . write ( substringCount ( S ) ) ;
Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Given array arr [ ] ; Function Call
function minSum ( arr , n ) { let ans = new Array ( n ) . fill ( 1 ) ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } let s = 0 ; for ( let x in ans ) { s = s + ans [ x ] ; } document . write ( s + " " ) ; } let arr = [ 1 , 2 , 2 ] ; let N = arr . length ; minSum ( arr , N ) ;
Minimum removal of consecutive similar characters required to empty a Binary String | Function to find minimum steps to make the string empty ; Stores the modified string ; Size of string ; Removing substring of same character from modified string ; Prlet the minimum steps required ; Given string S ; Function Call
function minSteps ( S ) { let new_str = " " ; let N = S . length ; let i = 0 ; while ( i < N ) { new_str += S [ i ] ; let j = i ; while ( i < N && S [ i ] == S [ j ] ) ++ i ; } document . write ( Math . ceil ( ( new_str . length + 1 ) / 2.0 ) ) ; } let S = " " ; minSteps ( S )
Count pairs with equal Bitwise AND and Bitwise OR value | Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
function countPairs ( arr , N ) { var cntPairs = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) ; } mp . forEach ( ( value , key ) => { cntPairs += parseInt ( ( value * ( value - 1 ) ) / 2 ) ; } ) ; return cntPairs ; } var arr = [ 1 , 2 , 3 , 1 , 2 , 2 ] ; var N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Minimum increments required to make given matrix palindromic | Javascript program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function palindromeMatrix ( N , M , arr ) { let ans = 0 ; for ( let i = 0 ; i < Math . floor ( ( N + 1 ) / 2 ) ; i ++ ) { for ( let j = 0 ; j < Math . floor ( ( M + 1 ) / 2 ) ; j ++ ) { let s = new Set ( ) ; s . add ( new pair ( i , j ) ) ; s . add ( new pair ( i , M - j - 1 ) ) ; s . add ( new pair ( N - i - 1 , j ) ) ; s . add ( new pair ( N - i - 1 , M - j - 1 ) ) ; let values = [ ] ; for ( let p of s . values ( ) ) { values . push ( arr [ p . first ] [ p . second ] ) ; } values . sort ( function ( a , b ) { return a - b ; } ) ; let max = Math . max ( ... values ) ; for ( let k = 1 ; k < values . length ; k ++ ) { ans += max - values [ k ] ; } } } document . write ( ans ) ; } let N = 3 , M = 3 ; let arr = [ [ 1 , 2 , 1 ] , [ 3 , 4 , 1 ] , [ 1 , 2 , 1 ] ] ; palindromeMatrix ( N , M , arr ) ;
Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Given Number N ; Function Call
function minimumMoves ( n ) { let cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return - 1 ; } let N = 25 ; document . write ( minimumMoves ( N ) ) ;
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code
function check ( n ) { if ( ( n & n - 1 ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var n = 5 ; check ( n ) ;
Minimize adding odd and subtracting even numbers to make all array elements equal to K | Function to find the minimum operations required to make array elements equal to K ; Stores minimum count of operations ; Traverse the given array ; If K is greater than arr [ i ] ; If ( K - arr [ i ] ) is even ; Update cntOpe ; Update cntOpe ; If K is less than arr [ i ] ; If ( arr [ i ] - K ) is even ; Update cntOpe ; Update cntOpe ; Driver Code
function MinOperation ( arr , N , K ) { let cntOpe = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( K > arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 2 ; } else { cntOpe += 1 ; } } else if ( K < arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 1 ; } else { cntOpe += 2 ; } } } return cntOpe ; } let arr = [ 8 , 7 , 2 , 1 , 3 ] ; let K = 5 ; let N = arr . length ; document . write ( MinOperation ( arr , N , K ) ) ;
Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Given vector A ; Given vector B ; Size of the vector ; Function Call
function rearrangeArray ( A , B , N ) { let m = new Map ( ) ; let xor_value = 0 ; for ( let i = 0 ; i < N ; i ++ ) { xor_value ^= A [ i ] ; xor_value ^= B [ i ] ; if ( m . has ( B [ i ] ) ) { m . set ( B [ i ] , m . get ( B [ i ] ) + 1 ) } else { m . set ( B [ i ] , 1 ) } } for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = A [ i ] ^ xor_value ; if ( m . has ( B [ i ] ) ) { m . set ( B [ i ] , m . get ( B [ i ] ) - 1 ) } else return [ ] ; } return B ; } function rearrangeArrayUtil ( A , B , N ) { let ans = rearrangeArray ( A , B , N ) ; if ( ans . length > 0 ) { for ( let x of ans ) { document . write ( x + " " ) ; } } else { document . write ( " " ) ; } } let A = [ 13 , 21 , 33 , 49 , 53 ] ; let B = [ 54 , 50 , 34 , 22 , 14 ] ; let N = A . length ; rearrangeArrayUtil ( A , B , N ) ;
Count sequences of given length having non | Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Prlet the answer ; Given M and X ; Function Call
function binCoff ( n , r ) { let val = 1 ; let i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } function findWays ( M ) { let n = M / 2 ; let a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; document . write ( b ) ; } let M = 4 , X = 5 ; findWays ( M ) ;
XOR of array elements whose modular inverse with a given number exists | Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function countInverse ( arr , N , M ) { var XOR = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var gcdOfMandelement = gcd ( M , arr [ i ] ) ; if ( gcdOfMandelement == 1 ) { XOR ^= arr [ i ] ; } } document . write ( XOR ) ; } var arr = [ 1 , 2 , 3 ] ; var M = 4 ; var N = arr . length ; countInverse ( arr , N , M ) ;
Count positions in a chessboard that can be visited by the Queen which are not visited by the King | Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Dimension of Board ; Position of Cell ; Function Call
function Moves_Calculator ( x , y , row , col ) { let total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += Math . min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += Math . min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; let king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } let n = 8 , m = 8 ; let x = 1 , y = 1 ; document . write ( Moves_Calculator ( x , y , m , n ) ) ;
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number | Function to find the nearest smaller number satisfying the condition ; Driver Code
function clstNum ( N ) { return ( N - 1 ) ; } let N = 11 ; document . write ( clstNum ( N ) ) ;