codeparrot/xlcost-text-to-code huggingface dataset:
Number of pairs whose product is a power of 2 | C # program for the above approach ; Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function call
using System ; using System . Linq ; class GFG { static void countPairs ( int [ ] arr , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( Convert . ToString ( arr [ i ] , 2 ) ) . Count ( f => ( f == '1' ) ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; Console . WriteLine ( desiredPairs + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 7 , 2 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Count ways to split N ! into two distinct co | C # program for the above approach ; Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; MArk all its multiples as non - prime ; Count all primes upto MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into two co - prime factors ; Driver Code ; Calling sieve function ; Given N ; Function call
using System ; class GFG { static int MAXN = 1000000 ; static int [ ] is_prime ; static int [ ] count_of_primes ; static void sieve ( ) { is_prime = new int [ MAXN ] ; count_of_primes = new int [ MAXN ] ; Array . Fill ( is_prime , 0 ) ; Array . Fill ( count_of_primes , 0 ) ; for ( int i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( int i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] == 1 ) { for ( int j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } } is_prime [ 2 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } static long power ( long x , long y , long p ) { long result = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } static void numberOfWays ( int N ) { long count = count_of_primes [ N ] - 1 ; long mod = 1000000007 ; long answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; long ans = answer ; Console . Write ( ans ) ; } public static void Main ( ) { sieve ( ) ; int N = 7 ; numberOfWays ( N ) ; } }
Maximum possible sum of squares of stack elements satisfying the given properties | C # program to implement the above approach ; Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Print the result ; Driver Code ; Function call
using System ; class GFG { public static void maxSumOfSquares ( int N , int S ) { int res = 0 ; if ( S < N S > 9 * N ) { Console . WriteLine ( - 1 ) ; return ; } S = S - N ; int c = 0 ; while ( S > 0 ) { c ++ ; if ( S / 8 > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; Console . WriteLine ( res ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int S = 12 ; maxSumOfSquares ( N , S ) ; } }
Unset least significant K bits of a given number | C # program for the above approach ; Function to return the value after unsetting K LSBs ; Create a mask ; Bitwise AND operation with the number and the mask ; Driver Code ; Given N and K ; Function Call
using System ; class GFG { static int clearLastBit ( int N , int K ) { int mask = ( - 1 << K + 1 ) ; return N = N & mask ; } public static void Main ( String [ ] args ) { int N = 730 , K = 3 ; Console . Write ( clearLastBit ( N , K ) ) ; } }
Check if quantities of 3 distinct colors can be converted to a single color by given merge | C # program for the above approach ; Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Driver Code ; Given colors ; Function Call
using System ; class GFG { static bool isPossible ( int r , int b , int g ) { r = r % 3 ; b = b % 3 ; g = g % 3 ; if ( r == b b == g g == r ) { return true ; } else { return false ; } } public static void Main ( String [ ] args ) { int R = 1 , B = 3 , G = 6 ; if ( isPossible ( R , B , G ) ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } }
Minimize steps required to make all array elements same by adding 1 , 2 or 5 | C # program for the above approach ; Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
using System ; public class GFG { static int calculate_steps ( int [ ] arr , int n , int minimum ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += arr [ i ] / 5 ; arr [ i ] = arr [ i ] % 5 ; count += arr [ i ] / 2 ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] > 0 ) { count ++ ; } } arr [ i ] = val ; } return count ; } static int solve ( int [ ] arr , int n ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int minimum = arr [ n - 1 ] ; int count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . Min ( count1 , Math . Min ( count2 , count3 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 6 } ; int N = arr . Length ; Console . Write ( solve ( arr , N ) ) ; } }
Largest palindromic string possible from given strings by rearranging the characters | C # program for the above approach ; Function to find largest palindrome possible from S and P after rearranging characters to make palindromic string T ; Using unordered_map to store frequency of elements mapT will have freq of elements of T ; Size of both the strings ; Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map ; Check if a unique character is present in both string S and P ; Making string T in two halves half1 - first half half2 - second half ; Reverse the half2 to attach with half1 to make palindrome T ; If same unique element is present in both S and P , then taking that only which is already taken through mapT ; If same unique element is not present in S and P , then take characters that make string T lexicographically smallest ; If no unique element is present in both string S and P ; Driver Code ; Given two strings S and P ; Function call
using System ; using System . Collections . Generic ; using System . Text ; class GFG { static string mergePalindromes ( string S , string P ) { Dictionary < char , int > mapS = new Dictionary < char , int > ( ) ; Dictionary < char , int > mapP = new Dictionary < char , int > ( ) ; Dictionary < char , int > mapT = new Dictionary < char , int > ( ) ; int n = S . Length , m = P . Length ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { mapS [ i ] = 0 ; mapP [ i ] = 0 ; mapT [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { mapS [ S [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { mapP [ P [ i ] ] ++ ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] % 2 == 0 ) { mapT [ i ] += mapS [ i ] ; mapS [ i ] = 0 ; } else { mapT [ i ] += mapS [ i ] - 1 ; mapS [ i ] = 1 ; } if ( mapP [ i ] % 2 == 0 ) { mapT [ i ] += mapP [ i ] ; mapP [ i ] = 0 ; } else { mapT [ i ] += mapP [ i ] - 1 ; mapP [ i ] = 1 ; } } int check = 0 ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] > 0 && mapP [ i ] > 0 ) { mapT [ i ] += 2 ; check = 1 ; break ; } } string half1 = " " , half2 = " " ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { for ( int j = 0 ; ( 2 * j ) < mapT [ i ] ; j ++ ) { half1 += i ; half2 += i ; } } char [ ] tmp = half2 . ToCharArray ( ) ; Array . Reverse ( tmp ) ; half2 = new string ( tmp ) ; if ( check != 0 ) { return half1 + half2 ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] > 0 mapP [ i ] > 0 ) { half1 += i ; return half1 + half2 ; } } return half1 + half2 ; } public static void Main ( string [ ] args ) { string S = " aeabb " ; string P = " dfedf " ; Console . Write ( mergePalindromes ( S , P ) ) ; } }
Count of subarrays having sum equal to its length | C # program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Decrementing all the elements of the array by 1 ; Making prefix sum array ; Declare map to store count of elements upto current element ; To count all the subarrays whose prefix sum is 0 ; Iterate the array ; Increment answer by count of current element of prefix array ; Return the answer ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countOfSubarray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] -- ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int answer = 0 ; mp . Add ( 0 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( pref [ i ] ) ) { answer += mp [ pref [ i ] ] ; mp [ pref [ i ] ] = mp [ pref [ i ] ] + 1 ; } else { mp . Add ( pref [ i ] , 1 ) ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 0 } ; int N = arr . Length ; Console . Write ( countOfSubarray ( arr , N ) ) ; } }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C # program to print subset at the nth position ordered by the sum of the elements ; Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; Create a vector for storing the elements of subsets ; Doing until all the set bits of n are used ; This part is executed only when the last bit is set ; Right shift the bit by one position ; Increasing the count each time by one ; Printing the values os elements ; Driver code
using System . Collections . Generic ; using System ; class GFG { static void printsubset ( int n , int k ) { int count = 0 , x = 0 ; List < int > vec = new List < int > ( ) ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . Add ( ( int ) Math . Pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( int i = 0 ; i < vec . Count ; i ++ ) Console . Write ( vec [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 7 , k = 4 ; printsubset ( n , k ) ; } }
Minimum digits to be removed to make either all digits or alternating digits same | C # program for the above approach ; Function to find longest possible subsequence of s beginning with x and y ; Iterate over the String ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Driver Code ; Given String s ; Find the size of the String ; Function Call ; This value is the count of minimum element to be removed
using System ; class GFG { static int solve ( String s , int x , int y ) { int res = 0 ; foreach ( char c in s . ToCharArray ( ) ) { if ( c - '0' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } static int find_min ( String s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = Math . Max ( count , solve ( s , i , j ) ) ; } } return count ; } public static void Main ( String [ ] args ) { String s = "100120013" ; int n = s . Length ; int answer = find_min ( s ) ; Console . Write ( ( n - answer ) ) ; } }
Count total set bits in all numbers from range L to R | C # program for the above approach ; Function to count set bits in x ; Base Case ; Recursive Call ; Function that returns count of set bits present in all numbers from 1 to N ; Initialize the result ; Return the setbit count ; Driver Code ; Given L and R ; Function Call
using System ; class GFG { static int countSetBitsUtil ( int x ) { if ( x <= 0 ) return 0 ; return ( ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( x / 2 ) ) ; } static int countSetBits ( int L , int R ) { int bitCount = 0 ; for ( int i = L ; i <= R ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } public static void Main ( String [ ] args ) { int L = 3 , R = 5 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ { 0 } " , countSetBits ( L , R ) ) ; } }
Possible values of Q such that , for any value of R , their product is equal to X times their sum | C # program for the above approach ; Function to find all possible values of Q ; List initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void values_of_Q ( int X ) { List < int > val_Q = new List < int > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . Add ( X + i ) ; } } Console . WriteLine ( val_Q . Count ) ; for ( int i = 0 ; i < val_Q . Count ; i ++ ) { Console . Write ( val_Q [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int X = 3 ; values_of_Q ( X ) ; } }
Program to find Greatest Common Divisor ( GCD ) of N strings | C # program for the above approach ; Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Driver Code ; Given array of strings ; Function Call
using System ; class GFG { static String gcd ( String str1 , String str2 ) { if ( str1 . Length < str2 . Length ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . StartsWith ( str2 ) ) { return " " ; } else if ( str2 . Length == 0 ) { return str1 ; } else { return gcd ( str1 . Substring ( str2 . Length ) , str2 ) ; } } static String findGCD ( String [ ] arr , int n ) { String result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } public static void Main ( String [ ] args ) { String [ ] arr = new String [ ] { " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " } ; int n = arr . Length ; Console . WriteLine ( findGCD ( arr , n ) ) ; } }
Check whether the given Matrix is balanced or not | C # program for the above approach ; Define size of matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Driver Code ; Given Matrix [ , ] mat ; Function Call
using System ; class GFG { static readonly int N = 4 ; static readonly int M = 4 ; static String balancedMatrix ( int [ , ] mat ) { bool is_balanced = true ; for ( int i = 0 ; i < N && is_balanced ; i ++ ) { for ( int j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i , j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i , j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i , j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " Balanced " ; else return " Unbalanced " ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 3 , 5 , 2 , 6 } , { 5 , 3 , 6 , 1 } , { 9 , 5 , 6 , 0 } } ; Console . Write ( balancedMatrix ( mat ) ) ; } }
Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Unix time is in seconds and Humar Readable Format : DATE : MONTH : YEAR : HOUR : MINUTES : SECONDS , Start of unix time : 01 Jan 1970 , 00 : 00 : 00 ; Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Driver Code ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS
using System ; class GFG { static String unixTimeToHumanReadable ( int seconds ) { String ans = " " ; int [ ] daysOfMonth = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = seconds / ( 24 * 60 * 60 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = extraTime / 3600 ; minutes = ( extraTime % 3600 ) / 60 ; secondss = ( extraTime % 3600 ) % 60 ; ans += String . Join ( " " , date ) ; ans += " / " ; ans += String . Join ( " " , month ) ; ans += " / " ; ans += String . Join ( " " , currYear ) ; ans += " ▁ " ; ans += String . Join ( " " , hours ) ; ans += " : " ; ans += String . Join ( " " , minutes ) ; ans += " : " ; ans += String . Join ( " " , secondss ) ; return ans ; } public static void Main ( String [ ] args ) { int T = 1595497956 ; String ans = unixTimeToHumanReadable ( T ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | C # program for the above approach ; Function to find area of rectangle inscribed another rectangle of length L and width W ; Area of rectangle ; Return the area ; Driver Code ; Given dimensions ; Function call
using System ; class GFG { static double AreaofRectangle ( int L , int W ) { double area = ( W + L ) * ( W + L ) / 2 ; return area ; } public static void Main ( String [ ] args ) { int L = 18 ; int W = 12 ; Console . Write ( AreaofRectangle ( L , W ) ) ; } }
Minimum number of operations required to reduce N to 0 | C # program to implement the above approach ; Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code
using System ; class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; int sqr ; for ( int i = 4 ; i <= n ; i ++ ) { sqr = ( int ) Math . Sqrt ( i ) ; int best = int . MaxValue ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . Min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . Min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( downToZero ( n ) ) ; } }
Minimum number of operations required to reduce N to 0 | C # Program to implement the above approach ; Function to find the minimum steps required to reduce n ; Base case ; Return answer based on parity of n ; Driver Code
using System ; class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( downToZero ( n ) ) ; } }
Minimum count of elements required to obtain the given Array by repeated mirror operations | C # program to implement the above approach ; Function to find minimum number of elements required to form [ ] A by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the readonly answer ; Driver Code
using System ; class GFG { static int minimumrequired ( int [ ] A , int N ) { int K = N ; int ans = 0 ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } int ispalindrome = 1 ; for ( int i = 0 ; i < K / 2 ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = 0 ; } if ( ispalindrome == 1 ) { ans = K / 2 ; K /= 2 ; } else { ans = K ; break ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 } ; int N = a . Length ; Console . WriteLine ( minimumrequired ( a , N ) ) ; } }
Sum of product of all integers upto N with their count of divisors | C # program for the above approach ; Function to find the sum of the product of all the integers and their positive divisors up to N ; Iterate for every number between 1 and N ; Find the first multiple of i between 1 and N ; Find the last multiple of i between 1 and N ; Find the total count of multiple of in [ 1 , N ] ; Compute the contribution of i using the formula ; Add the contribution of i to the answer ; Return the result ; Driver code ; Given N ; function call
using System ; class GFG { static int sumOfFactors ( int N ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { int first = i ; int last = ( N / i ) * i ; int factors = ( last - first ) / i + 1 ; int totalContribution = ( ( ( factors ) * ( factors + 1 ) ) / 2 ) * i ; ans += totalContribution ; } return ans ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . WriteLine ( sumOfFactors ( N ) ) ; } }
Find a number M < N such that difference between their XOR and AND is maximum | C # program for the above approach ; Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Driver Code ; Given number N ; Function call
using System ; class GFG { static int getMaxDifference ( int N ) { int M = - 1 ; int maxDiff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( getMaxDifference ( N ) ) ; } }