codeparrot/xlcost-text-to-code huggingface dataset:
Maximize frequency sum of K chosen characters from given string | Function to find the maximum sum of frequencies of the exactly K chosen characters from the string S ; Stores the resultant maximum sum ; Stores the frequency of array elements ; Find the frequency of character ; Sort the frequency array in the descending order ; Iterate to choose K elements greedily ; If the freq [ i ] cards are chosen ; K cards have been picked ; Return the resultant sum ; Driver Code
function maximumSum ( S , N , K ) { let sum = 0 ; let freq = Array . from ( { length : 256 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ S [ i ] . charCodeAt ( ) ] ++ ; } freq . sort ( ( a , b ) => b - a ) ; for ( let i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } let S = " " ; let K = 10 ; let N = S . length ; document . write ( maximumSum ( S . split ( ' ' ) , N , K ) )
Print all numbers that can be obtained by adding A or B to N exactly M times | Function to find all possible numbers that can be obtained by adding A or B to N exactly N times ; If number of steps is 0 and only possible number is N ; Add A to N and make a recursive call for M - 1 steps ; Add B to N and make a recursive call for M - 1 steps . ; Given Inputs ; Stores all possible numbers ; Function call ; Print all possible numbers
function possibleNumbers ( numbers , N , M , A , B ) { if ( M == 0 ) { numbers . add ( N ) ; return ; } possibleNumbers ( numbers , N + A , M - 1 , A , B ) ; possibleNumbers ( numbers , N + B , M - 1 , A , B ) ; } var N = 5 , M = 3 , A = 4 , B = 6 ; var numbers = new Set ( ) ; possibleNumbers ( numbers , N , M , A , B ) ; for ( let x of numbers ) { document . write ( x + ' ' ) ; }
Maximum sum of array after removing a positive or negative subarray | Function to find the maximum sum of array after removing either the contiguous positive or negative elements ; Store the total sum of array ; Store the maximum contiguous negative sum ; Store the sum of current contiguous negative elements ; Store the minimum element of array ; Traverse the array , arr [ ] ; Update the overall sum ; Store minimum element of array ; If arr [ i ] is positive ; Update temp_sum to 0 ; Add arr [ i ] to temp_sum ; Update max_neg ; If no negative element in array then remove smallest positive element ; Print the required sum ; Given Input ; Function Call
function maxSum ( arr , n ) { let sum = 0 ; let max_neg = Number . MAX_SAFE_INTEGER ; let tempsum = 0 ; let small = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = Math . min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = Math . min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } document . write ( sum - max_neg ) ; } let arr = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n = arr . length ; maxSum ( arr , n ) ;
Lexicographically largest string possible by at most K replacements | JavaScript implementation of the above approach ; Traverse each element of the string ; If the current character can be replaced with ' z ' ; Return the modified string ; Driver code
function largestString ( s , k ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != ' ' && k > 0 ) { s = s . substring ( 0 , i ) + ' ' + s . substring ( i + 1 ) ; k -- ; } } return s ; } var s = " " ; var k = 1 ; document . write ( largestString ( s , k ) ) ;
Count characters of a string which when removed individually makes the string equal to another string | Function to count characters from string A whose removal makes the strings A and B equal ; Stores the index of the longest prefix ; Stores the index of the longest suffix ; Traverse the string B ; Traverse the string B ; If N - M is equal to 1 and Y is less than or equal to X ; Print the count of characters ; Print the positions of the characters ; Otherwise ; Driver Code
function RemoveOneChar ( A , B , N , M ) { var X = 0 ; var Y = N - 1 ; var i ; for ( i = 0 ; i < M ; i ++ ) { if ( A [ X ] != B [ i ] ) break ; X ++ ; } for ( i = M - 1 ; i >= 0 ; i -- ) { if ( A [ Y ] != B [ i ] ) break ; Y -- ; } if ( N - M == 1 && Y < X ) { document . write ( X - Y + 1 + " " ) ; for ( i = Y ; i <= X ; i ++ ) document . write ( i + 1 + " " ) ; document . write ( " " ) ; } else document . write ( - 1 ) ; } var A = " " ; var B = " " ; var N = A . length ; var M = B . length ; RemoveOneChar ( A , B , N , M ) ;
Minimum flips or swapping of adjacent characters required to make a string equal to another | Function to find minimum operations required to convert string A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Given Input ; Function Call
function minimumOperation ( a , b ) { var n = a . length ; var i = 0 ; var minoperation = 0 ; while ( i < n ) { if ( a [ i ] == b [ i ] ) { i = i + 1 ; continue ; } else if ( a [ i ] == b [ i + 1 ] && a [ i + 1 ] == b [ i ] && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a [ i ] != b [ i ] ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } document . write ( minoperation ) ; } var a = " " , b = " " ; minimumOperation ( a , b ) ;
Minimum replacements required to make sum of all K | Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Given Input ; Function Call
function findMinOperations ( arr , N , K ) { var operations = 0 ; var i , j ; for ( i = 0 ; i < K ; i ++ ) { var freq = new Map ( ) ; for ( j = i ; j < N ; j += K ) { if ( freq . has ( arr [ j ] ) ) freq . set ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) ; else freq . set ( arr [ j ] , 1 ) ; } var max1 = 0 , num ; for ( const [ key , value ] of freq . entries ( ) ) { if ( value > max1 ) { max1 = value ; num = key ; } } for ( const [ key , value ] of freq . entries ( ) ) { if ( key != num ) operations += value ; } } document . write ( operations ) ; } var arr = [ 3 , 4 , 3 , 5 , 6 ] ; var K = 2 ; var N = arr . length ; findMinOperations ( arr , N , K ) ;
Count of pairs of integers up to X and Y that generates equal Quotient and Remainder | Function to calculate the number of pairs satisfying ( m / n = m % n ) ; Iterate from 1 to sqrt ( x ) ; Combining the conditions - 1 ) n > k 2 ) n <= y 3 ) n <= ( x / k - 1 ) ; Driver code
function countOfPairs ( x , y ) { var count = 0 ; var k ; for ( k = 1 ; k * k <= x ; ++ k ) { count += Math . max ( 0 , Math . min ( y , x / k - 1 ) - k ) ; } document . write ( count + " " ) ; } var x = 4 ; var y = 5 ; countOfPairs ( x , y ) ;
Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square | Function to count total number of permutation of the first N natural number having the sum of adjacent elements as perfect square ; Create an adjacency matrix ; Count elements whose indegree is 1 ; Generate adjacency matrix ; Find the sum of i and j ; If sum is perfect square . then move from i to j ; Add it in adjacency list of i ; If any list is of size 1 , then the indegree is 1 ; If there is no element whose indegree is 1 , then N such permutations are possible ; If there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; If there are more than 2 elements whose indegree is 1 , then return 0 ; Driver Code
function countPermutations ( N ) { let adj = [ ] ; for ( let i = 0 ; i < 105 ; i ++ ) adj . push ( [ ] ) ; let indeg = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( i == j ) continue ; let sum = i + j ; if ( Math . ceil ( Math . sqrt ( sum ) ) == Math . floor ( Math . sqrt ( sum ) ) ) { adj [ i ] . push ( j ) ; } } if ( adj [ i ] . length == 1 ) indeg ++ ; } if ( indeg == 0 ) return N ; else if ( indeg <= 2 ) return 2 ; else return 0 ; } let N = 17 ; document . write ( countPermutations ( N ) ) ;
Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | Function to construct the list with exactly K unique adjacent element differences ; Stores the resultant array ; Stores the left and the right most element of the range ; Traverse the array ; If k is even , the add left to array and increment the left ; If k is odd , the add right to array and decrement the right ; Repeat the steps for k - 1 times ; Print the resultant list ; Driver code
function makeList ( N , K ) { var list = new Array ( N ) ; var left = 1 ; var right = N ; for ( var i = 0 ; i < N ; i ++ ) { if ( K % 2 == 0 ) { list [ i ] = left ; left = left + 1 ; } else { list [ i ] = right ; right = right - 1 ; } if ( K > 1 ) K -- ; } for ( var i = 0 ; i < N ; i ++ ) { document . write ( list [ i ] + " " ) ; } } var N = 6 ; var K = 3 ; makeList ( N , K ) ;
Find the date after next half year from a given date | Function to find the date after the next half - year ; Stores the number of days in the months of a leap year ; List of months ; Days in half of a year ; Index of current month ; Starting day ; Decrement the value of cnt by 1 ; Increment cur_date ; If cnt is equal to 0 , then break out of the loop ; Update cur_month ; Update cur_date ; Print the resultant date ; Driver Code ; Function Call
function getDate ( d , m ) { let days = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; let month = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let cnt = 183 ; let cur_month = 0 ; for ( let i = 0 ; i < 12 ; i ++ ) if ( m == month [ i ] ) cur_month = i ; let cur_date = d ; while ( true ) { while ( cnt > 0 && cur_date <= days [ cur_month ] ) { cnt -= 1 ; cur_date += 1 ; } if ( cnt == 0 ) break ; cur_month = ( cur_month + 1 ) % 12 ; cur_date = 1 ; } document . write ( cur_date + " " + month [ cur_month ] ) ; } let D = 15 ; let M = " " ; getDate ( D , M ) ;
Maximum number made up of distinct digits whose sum is equal to N | Function to find the largest positive number made up of distinct digits having the sum of its digits as N ; If given number is greater than 45 , prlet - 1 ; Store the required number and the digit to be considered ; Loop until N > 0 and digit > 0 ; If the current digit is at most N then , add it to number num ; Update the value of num ; Decrement N by digit ; Consider the next lower digit ; Add 0 at the end and return the number num ; Driver Code
function largestNumber ( N ) { if ( N > 45 ) return - 1 ; let num = 0 , digit = 9 ; while ( N > 0 && digit > 0 ) { if ( digit <= N ) { num *= 10 ; num += digit ; N -= digit ; } digit -= 1 ; } return num * 10 ; } let N = 25 ; document . write ( largestNumber ( N ) ) ;
Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S ; Stores the count of multiplication operator in the string ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
function minimumSum ( A , N , S ) { let mul = 0 ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == " " ) mul += 1 ; } let ans = 1000000 ; for ( let i = 0 ; i < 1 << ( N - 1 ) ; i ++ ) { let cnt = 0 ; let v = [ ] ; for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( ( 1 << j ) & i ) { cnt += 1 ; v . push ( " " ) ; } else { v . push ( " " ) ; } } if ( cnt == mul ) { let d = [ ] ; d . push ( A [ 0 ] ) ; for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( v [ j ] == " " ) { let x = d [ d . length - 1 ] ; d . pop ( ) ; x = x * A [ j + 1 ] ; d . push ( x ) ; } else { d . push ( A [ j + 1 ] ) ; } } let sum = 0 ; while ( d . length > 0 ) { let x = d [ 0 ] ; sum += x ; d . shift ( ) ; } ans = Math . min ( ans , sum ) ; } } return ans ; } let A = [ 2 , 2 , 2 , 2 ] ; let S = " " ; let N = A . length ; document . write ( minimumSum ( A , N , S ) ) ;
Minimum sum of medians of all possible K length subsequences of a sorted array | Function to find the minimum sum of all the medians of the K sized sorted arrays formed from the given array ; Stores the distance between the medians ; Stores the number of subsequences required ; Stores the resultant sum ; Iterate from start and add all the medians ; Add the value of arr [ i ] to the variable minsum ; Increment i by select the median to get the next median index ; Decrement the value of totalArrays by 1 ; Print the resultant minimum sum ; Driver Code
function sumOfMedians ( arr , N , K ) { let selectMedian = Math . floor ( ( K + 1 ) / 2 ) ; let totalArrays = Math . floor ( N / K ) ; let minSum = 0 ; let i = selectMedian - 1 ; while ( i < N && totalArrays != 0 ) { minSum = minSum + arr [ i ] ; i = i + selectMedian ; totalArrays -- ; } document . write ( minSum ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let N = arr . length ; let K = 2 ; sumOfMedians ( arr , N , K ) ;
Find K positive integers not exceeding N and having sum S | Function to represent S as the sum of K positive letegers less than or equal to N ; If S can cannot be represented as sum of K letegers ; If sum of first i natural numbers exceeds S ; Insert i leto nums [ ] ; Insert first K - 1 positive numbers leto answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Prleting the K numbers ; Driver Code
function solve ( S , K , N ) { if ( K > N ) { document . write ( " " ) ; return ; } let max_sum = 0 , min_sum = 0 ; for ( let i = 1 ; i <= K ; i ++ ) { min_sum += i ; max_sum += N - i + 1 ; } if ( S < min_sum S > max_sum ) { document . write ( " " ) ; return ; } let s1 = 0 ; let nums = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) { if ( s1 > S ) break ; s1 += i ; nums . push ( i ) ; } let answer = [ ] ; let s2 = 0 ; for ( let i = 0 ; i < K - 1 ; i ++ ) { answer . push ( nums [ i ] ) ; s2 += nums [ i ] ; } answer . push ( S - s2 ) ; let Max = N ; for ( let i = answer . length - 1 ; i >= 0 ; i -- ) { if ( answer [ i ] > Max ) { let extra = answer [ i ] - Max ; if ( i - 1 >= 0 ) answer [ i - 1 ] += extra ; answer [ i ] = Max ; Max -- ; } else break ; } for ( let x in answer ) document . write ( answer [ x ] + " " ) ; document . write ( " " ) ; } let S = 15 , K = 4 , N = 8 ; solve ( S , K , N ) ;
Generate an N | Function to minimize the maximum element present in an N - length array having sum of elements divisible by K ; Return the ceil value of ( K / N ) ; Driver Code
function minimumValue ( N , K ) { return Math . ceil ( K / N ) ; } let N = 4 , K = 50 ; document . write ( minimumValue ( N , K ) ) ;
Minimum removal of elements from end of an array required to obtain sum K | Function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver code ; Given Array ; Given target sum
function minSizeArr ( A , N , K ) { let leftTaken = N , rightTaken = N ; let leftSum = 0 , rightSum = 0 ; for ( let left = - 1 ; left < N ; left ++ ) { if ( left != - 1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( let right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( let i = 0 ; i < leftTaken ; i ++ ) document . write ( A [ i ] + " " ) ; for ( let i = 0 ; i < rightTaken ; i ++ ) document . write ( A [ N - i - 1 ] + " " ) ; } else document . write ( - 1 ) ; } let N = 7 ; let A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ] ; let K = 10 ; minSizeArr ( A , N , K ) ;
Minimum removal of elements from end of an array required to obtain sum K | Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver Code ; Given Array ; Given target sum
function minSizeArr ( A , N , K ) { var sum = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) sum += A [ i ] ; if ( K > sum ) { cout << - 1 ; return ; } if ( K == sum ) { for ( i = 0 ; i < N ; i ++ ) { document . write ( A [ i ] + ' ' ) ; } return ; } var tar = sum - K ; var um = new Map ( ) ; um [ 0 ] = - 1 ; var left , right ; var cur = 0 , maxi = - 1 ; for ( i = 0 ; i < N ; i ++ ) { cur += A [ i ] ; if ( um . has ( cur - tar ) && i - um . get ( cur - tar ) > maxi ) { maxi = i - um . get ( cur - tar ) ; right = i ; left = um . get ( cur - tar ) ; } if ( ! um . has ( cur ) ) um . set ( cur , i ) ; } if ( maxi == - 1 ) cout << - 1 ; else { for ( i = 0 ; i <= left ; i ++ ) document . write ( A [ i ] + ' ' ) ; for ( i = 0 ; i < right ; i ++ ) document . write ( A [ N - i - 1 ] + ' ' ) ; } } var N = 7 ; var A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ] ; var K = 10 ; minSizeArr ( A , N , K ) ;
Minimum product modulo N possible for any pair from a given range | Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Prlet the minimum value of remainder ; If R - L >= N ; Driver Code
function minModulo ( L , R , N ) { if ( R - L < N ) { let ans = Number . MAX_VALUE ; for ( let i = L ; i <= R ; i ++ ) for ( let j = L ; j <= R ; j ++ ) if ( i != j ) ans = Math . min ( ans , ( i * j ) % N ) ; document . write ( ans ) ; } else { document . write ( 0 ) ; } } let L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ;
Count numbers having GCD with N equal to the number itself | Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
function countNumbers ( N ) { var count = 0 ; for ( i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( parseInt ( N / i ) != i ) { count ++ ; } } } return count ; } var N = 10 ; document . write ( countNumbers ( N ) ) ;
Maximum length of all possible K equal length ropes generated by cutting N ropes | Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array arr [ ] ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
function maximumSize ( a , k ) { let low = 1 ; let high = Math . max . apply ( Math , a ) ; let ans = - 1 ; while ( low <= high ) { let mid = Math . floor ( low + ( high - low ) / 2 ) ; let count = 0 ; for ( let c = 0 ; c < a . length ; c ++ ) { count += Math . floor ( a / mid ) ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 9 ] ; let K = 6 ; document . write ( maximumSize ( arr , K ) )
Quadratic equation whose roots are K times the roots of given equation | Function to find the quadratic equation whose roots are K times the roots of the given equation ; Print quadratic equation ; Driver Code
function findEquation ( A , B , C , K ) { document . write ( A + " " + K * B + " " + K * K * C ) ; } var A = 1 , B = 2 , C = 1 , K = 2 ; findEquation ( A , B , C , K ) ;
Maximum the value of a given expression for any pair of coordinates on a 2D plane | Function to find the maximum value of the given expression possible for any pair of co - ordinates ; Stores the differences between pairs ; Stores the maximum value ; Traverse the array arr [ ] [ ] ; While pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > K ; Removes the top element ; If pq is not empty ; Update the value res ; Push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; Print the result ; Driver Code
function findMaxValueOfEquation ( arr , K ) { let pq = [ ] ; let res = Number . MIN_VALUE ; for ( let point = 0 ; point < arr . length ; point ++ ) { while ( pq . length != 0 && arr [ point ] [ 0 ] - pq [ 0 ] [ 1 ] > K ) { pq . shift ( ) ; } if ( pq . length != 0 ) { res = Math . max ( res , pq [ 0 ] [ 0 ] + arr [ point ] [ 0 ] + arr [ point ] [ 1 ] ) ; } pq . push ( [ arr [ point ] [ 1 ] - arr [ point ] [ 0 ] , arr [ point ] [ 0 ] ] ) ; pq . sort ( function ( a , b ) { if ( a [ 0 ] != b [ 0 ] ) return b [ 0 ] - a [ 0 ] ; return b [ 1 ] - a [ 1 ] ; } ) } document . write ( res ) ; } let arr = [ [ 1 , 3 ] , [ 2 , 0 ] , [ 5 , 10 ] , [ 6 , - 10 ] ] ; let K = 1 ; findMaxValueOfEquation ( arr , K ) ;
Minimum increments required to make absolute difference of all pairwise adjacent array elements even | Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver code
function minOperations ( arr , n ) { var oddcount = 0 , evencount = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return Math . min ( oddcount , evencount ) ; } var arr = [ 2 , 4 , 3 , 1 , 8 ] ; var N = arr . length ; document . write ( minOperations ( arr , N ) ) ;
Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function countDistinctNumbers ( A , B , C ) { var g = gcd ( A , B ) ; var count = C / g ; document . write ( count ) ; } var A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ;