codeparrot/xlcost-text-to-code huggingface dataset:
Check if two vectors are collinear or not | C # program for the above approach ; Function to calculate cross product of two vectors ; Update cross_P [ 0 ] ; Update cross_P [ 1 ] ; Update cross_P [ 2 ] ; Function to check if two given vectors are collinear or not ; Store the first and second vectors ; Store their cross product ; Calculate their cross product ; Check if their cross product is a NULL Vector or not ; Driver Code ; Given coordinates of the two vectors
using System ; class GFG { static void crossProduct ( int [ ] vect_A , int [ ] vect_B , int [ ] cross_P ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } static void checkCollinearity ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int [ ] A = { x1 , y1 , z1 } ; int [ ] B = { x2 , y2 , z2 } ; int [ ] cross_P = new int [ 3 ] ; crossProduct ( A , B , cross_P ) ; if ( cross_P [ 0 ] == 0 && cross_P [ 1 ] == 0 && cross_P [ 2 ] == 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int x1 = 4 , y1 = 8 , z1 = 12 ; int x2 = 8 , y2 = 16 , z2 = 24 ; checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) ; } }
Program to calculate Kinetic Energy and Potential Energy | C # program to implement the above approach ; / Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static double kineticEnergy ( double M , double V ) { double KineticEnergy ; KineticEnergy = 0.5 * M * V * V ; return KineticEnergy ; } static double potentialEnergy ( double M , double H ) { double PotentialEnergy ; PotentialEnergy = M * 9.8 * H ; return PotentialEnergy ; } public static void Main ( ) { double M = 5.5 , H = 23.5 , V = 10.5 ; Console . WriteLine ( " Kinetic ▁ Energy ▁ = ▁ " + kineticEnergy ( M , V ) ) ; Console . Write ( " Potential ▁ Energy ▁ = ▁ " + potentialEnergy ( M , H ) ) ; } }
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | C # program for the above approach ; Function to count the number of pairs required to be removed from the array and replaced by their Bitwise OR values ; Stores the count of even array elements ; Traverse the given array ; Increment the count of even array elements ; If the array contains at least one odd array element ; Otherwise , print 0 ; Driver code
using System ; class GFG { static void countPairs ( int [ ] arr , int N ) { int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( N - even >= 1 ) { Console . WriteLine ( even ) ; return ; } Console . WriteLine ( 0 ) ; } static void Main ( ) { int [ ] arr = { 5 , 4 , 7 , 2 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | C # program for the above approach ; Function to find distribution of M objects among all array elements ; Stores the distribution of M objects ; Stores the indices of distribution ; Stores the remaining objects ; Iterate until rem is positive ; If the number of remaining objects exceeds required the number of objects ; Increase the number of objects for the index ptr by arr [ ptr ] ; Decrease remaining objects by arr [ ptr ] ; Increase the number of objects for the index ptr by rem ; Decrease remaining objects to 0 ; Increase ptr by 1 ; Print the final distribution ; Driver Code
using System ; class GFG { static void distribute ( int N , int K , int M , int [ ] arr ) { int [ ] distribution = new int [ N ] ; int ptr = K - 1 ; int rem = M ; while ( rem > 0 ) { if ( rem >= arr [ ptr ] ) { distribution [ ptr ] += arr [ ptr ] ; rem -= arr [ ptr ] ; } else { distribution [ ptr ] += rem ; rem = 0 ; } ptr = ( ptr + 1 ) % N ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( distribution [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 2 , 1 , 4 } ; int M = 11 , S = 2 ; int N = arr . Length ; distribute ( N , S , M , arr ) ; } }
Sum of squares of differences between all pairs of an array | C # program for the above approach ; Function to calculate sum of squares of differences of all possible pairs ; Stores the final sum ; Stores temporary values ; Traverse the array ; Final sum ; Print the answer ; Driver Code ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
using System ; class GFG { static void sumOfSquaredDifferences ( int [ ] arr , int N ) { int ans = 0 ; int sumA = 0 , sumB = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumA += ( arr [ i ] * arr [ i ] ) ; sumB += arr [ i ] ; } sumA = N * sumA ; sumB = ( sumB * sumB ) ; ans = sumA - sumB ; Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 8 , 4 } ; int N = arr . Length ; sumOfSquaredDifferences ( arr , N ) ; } }
Count ways to remove objects such that exactly M equidistant objects remain | C # program for the above approach ; Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If len > n ; Update the number of ways ; Print the result ; Driver code
using System ; class GFG { static void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { Console . Write ( n ) ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } Console . Write ( ans ) ; } static void Main ( ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; } }
Count unique stairs that can be reached by moving given number of steps forward or backward | C # program for the above approach ; Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; / Push the starting position ; Mark the starting position S as visited ; Iterate until queue is not empty ; Store the current stair number ; Pop it from the queue ; Check for all possible moves from the current stair ; Store the new stair number ; If it is valid and unvisited ; Push it into queue ; Mark the stair as visited ; Store the result ; Count the all visited stairs ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countStairs ( int n , int x , int a , int b ) { int [ ] vis = new int [ n + 1 ] ; Array . Clear ( vis , 0 , vis . Length ) ; int [ ] moves = { + a , - a , + b , - b } ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( x ) ; vis [ x ] = 1 ; while ( q . Count > 0 ) { int currentStair = q . Peek ( ) ; q . Dequeue ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && vis [ newStair ] == 0 ) { q . Enqueue ( newStair ) ; vis [ newStair ] = 1 ; } } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; Console . WriteLine ( cnt ) ; } public static void Main ( ) { int N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ; } }
Count smaller elements present in the array for each array element | C # program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver Code
using System ; public class GFG { static void smallerNumbers ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { count ++ ; } } Console . Write ( count + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . Length ; smallerNumbers ( arr , N ) ; } }
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | C # program for the above approach ; Function to calculate GCD of two numbers ; Base Case ; Calculate GCD recursively ; Function to count the minimum number of divisions required to make X and Y equal ; Calculate GCD of X and Y ; Divide X and Y by their GCD ; Stores the number of divisions ; Iterate until X != Y ; Maintain the order X <= Y ; If X is divisible by 2 , then divide X by 2 ; If X is divisible by 3 , then divide X by 3 ; If X is divisible by 5 , then divide X by 5 ; If X is not divisible by 2 , 3 , or 5 , then print - 1 ; Increment count by 1 ; Print the value of count as the minimum number of operations ; Driver Code
using System ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void minimumOperations ( int X , int Y ) { int GCD = gcd ( X , Y ) ; X = X / GCD ; Y = Y / GCD ; int count = 0 ; while ( X != Y ) { if ( Y > X ) { int t = X ; X = Y ; Y = t ; } if ( X % 2 == 0 ) { X = X / 2 ; } else if ( X % 3 == 0 ) { X = X / 3 ; } else if ( X % 5 == 0 ) { X = X / 5 ; } else { Console . WriteLine ( " - 1" ) ; return ; } count ++ ; } Console . WriteLine ( count ) ; } static public void Main ( ) { int X = 15 , Y = 20 ; minimumOperations ( X , Y ) ; } }
Minimum increments or decrements required to convert a sorted array into a power sequence | C # program for the above approach ; Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; Calculate F ( x ) ; Check if X ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; Update ans to store the minimum of ans and F ( x ) ; Return the minimum number of operations required ; Driver Code
using System ; class GFG { static int minOperations ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += a [ i ] ; } ans -= n ; for ( int x = 1 ; ; x ++ ) { int curPow = 1 , curCost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curCost += Math . Abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = Math . Min ( ans , curCost ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 7 } ; int N = arr . Length ; Console . WriteLine ( minOperations ( arr , N ) ) ; } }
Modulo Operations in Programming With Negative Results | C # program to illustrate modulo operations using the above equation ; Function to calculate and return the remainder of a % n ; Type casting is necessary as ( int ) / ( int ) will give int result , i . e . - 3 / 2 will give - 1 and not - 1.5 ; Return the resultant remainder ; Driver code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
using System ; class GFG { static int floorMod ( int a , int n ) { int q = ( int ) Math . Floor ( ( double ) a / n ) ; return a - n * q ; } static public void Main ( ) { int a , b ; a = 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; } }
Find the absolute difference between the nearest powers of two given integers for every array element | C # program for the above approach ; Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ] ; Find the log a of arr [ i ] ; Find the power of a less than and greater than a ; Find the log b of arr [ i ] ; Find the power of b less than and greater than b ; Update arr [ i ] with absolute difference of log_a & log _b ; Print the modified array ; Driver Code
using System ; public class GFG { static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void nearestPowerDiff ( int [ ] arr , int N , int a , int b ) { for ( int i = 0 ; i < N ; i ++ ) { int log_a = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( a ) ) ; int A = ( int ) ( Math . Pow ( a , log_a ) ) ; int B = ( int ) ( Math . Pow ( a , log_a + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_a = A ; else log_a = B ; int log_b = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( b ) ) ; A = ( int ) ( Math . Pow ( b , log_b ) ) ; B = ( int ) ( Math . Pow ( b , log_b + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_b = A ; else log_b = B ; arr [ i ] = Math . Abs ( log_a - log_b ) ; } printArray ( arr , N ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 12 , 25 } ; int A = 2 , B = 3 ; int N = arr . Length ; nearestPowerDiff ( arr , N , A , B ) ; } }
Count subarrays having even Bitwise OR | C # program for the above approach ; Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver code ; Function Call
using System ; class GFG { static int bitOr ( int [ ] arr , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } static void Main ( ) { int [ ] arr = { 1 , 5 , 4 , 2 , 6 } ; int N = arr . Length ; Console . Write ( bitOr ( arr , N ) ) ; } }
Find all pairs raised to power K differs by exactly N | C # program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
using System ; class GFG { static void ValidPairs ( int X , int K ) { int count = 0 ; for ( int A = - 1000 ; A <= 1000 ; A ++ ) { for ( int B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . Pow ( A , K ) - Math . Pow ( B , K ) == X ) { count ++ ; Console . WriteLine ( A + " ▁ " + B ) ; } } } if ( count == 0 ) { Console . WriteLine ( " - 1" ) ; } } static public void Main ( ) { int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; } }
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | C # program for the above approach ; Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
using System ; class GFG { static int maximumAND ( int [ ] arr , int n , int m ) { int tot = 1 << n ; int mx = 0 ; for ( int bm = 0 ; bm < tot ; bm ++ ) { int andans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . Max ( mx , andans ) ; } } return mx ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; int M = 2 ; Console . WriteLine ( maximumAND ( arr , N , M ) ) ; } }
Calculate sum of the array generated by given operations | C # program for the above approach ; Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the stack ; If the character is D , then push 2 * top element into stack ; If the character is + , add sum of top two elements from the stack ; Otherwise , push x and add it to ans ; Print the resultant sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findTotalSum ( string [ ] ops ) { if ( ops . Length == 0 ) { Console . WriteLine ( 0 ) ; return ; } Stack < int > pts = new Stack < int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < ops . Length ; i ++ ) { if ( ops [ i ] == " C " ) { ans -= pts . Pop ( ) ; } else if ( ops [ i ] == " D " ) { pts . Push ( pts . Peek ( ) * 2 ) ; ans += pts . Peek ( ) ; } else if ( ops [ i ] == " + " ) { int a = pts . Pop ( ) ; int b = pts . Peek ( ) ; pts . Push ( a ) ; ans += ( a + b ) ; pts . Push ( a + b ) ; } else { int n = Int32 . Parse ( ops [ i ] ) ; ans += n ; pts . Push ( n ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { string [ ] arr = { "5" , " - 2" , " C " , " D " , " + " } ; findTotalSum ( arr ) ; } }
XOR of major diagonal elements of a 3D Matrix | C # program for the above approach ; Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
using System ; class GFG { static void findXOR ( int [ , , ] mat , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) { if ( ( i == j && j == k ) ) { XOR ^= mat [ i , j , k ] ; XOR ^= mat [ i , j , N - k - 1 ] ; } } } } Console . WriteLine ( XOR ) ; } public static void Main ( string [ ] args ) { int [ , , ] mat = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . GetLength ( 0 ) ; findXOR ( mat , N ) ; } }
XOR of major diagonal elements of a 3D Matrix | C # program for the above approach ; Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
using System ; class GFG { static void findXOR ( int [ , , ] mat , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XOR ^= mat [ i , i , i ] ; XOR ^= mat [ i , i , N - i - 1 ] ; } Console . Write ( XOR ) ; } static public void Main ( ) { int [ , , ] mat = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . GetLength ( 0 ) ; findXOR ( mat , N ) ; } }
Count of subtrees possible from an N | C # program of above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the root ; Traverse the adjacency list ; Iterate over every ancestor ; Calculate product of the number of subtrees for each child node ; Update the value of ans ; Return the resultant count ; Function to count the number of subtrees in the given tree ; Initialize an adjacency matrix ; Add the edges ; Function Call to count the number of subtrees possible ; Print count of subtrees ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int MAX = 300004 ; static List < List < int > > graph ; static long mod = ( long ) 1e9 + 7 ; static int ans = 0 ; static int countSubtreesUtil ( int cur , int par ) { int res = 1 ; for ( int i = 0 ; i < graph [ cur ] . Count ; i ++ ) { int v = graph [ cur ] [ i ] ; if ( v == par ) continue ; res = ( int ) ( ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod ) ; } ans = ( int ) ( ( ans + res ) % mod ) ; return res ; } static void countSubtrees ( int N , int [ , ] adj ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = adj [ i , 0 ] ; int b = adj [ i , 1 ] ; graph [ a ] . Add ( b ) ; graph [ b ] . Add ( a ) ; } countSubtreesUtil ( 1 , 1 ) ; Console . WriteLine ( ans + 1 ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ , ] adj = { { 0 , 1 } , { 1 , 2 } } ; graph = new List < List < int > > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) graph . Add ( new List < int > ( ) ) ; countSubtrees ( N , adj ) ; } }
Find the player who wins the game of placing alternate + and | C # program for the above approach ; Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
using System ; class GFG { static void checkWinner ( int [ ] arr , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { Console . Write ( " A " ) ; } else { Console . Write ( " B " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 } ; int N = arr . Length ; checkWinner ( arr , N ) ; } }
Generate an array having sum of Euler Totient Function of all elements equal to N | C # program for the above approach ; Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; Push the current divisor ; If N is not a perfect square ; Push the second divisor ; Print the resultant array ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void constructArray ( int N ) { List < int > ans = new List < int > ( ) ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { ans . Add ( i ) ; if ( N != ( i * i ) ) { ans . Add ( N / i ) ; } } } foreach ( int it in ans ) { Console . Write ( it + " ▁ " ) ; } } public static void Main ( ) { int N = 12 ; constructArray ( N ) ; } }
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | C # Program to implement the above approach ; Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumRowValue ( int n , int m ) { return gcd ( n , m ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int M = 2 ; Console . WriteLine ( maximumRowValue ( N , M ) ) ; } }
Program to generate an array having convolution of two given arrays | C # program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c [ ] ; Driver Code
using System ; class GFG { static int MOD = 998244353 ; static void findConvolution ( int [ ] a , int [ ] b ) { int n = a . Length , m = b . Length ; int [ ] c = new int [ ( n + m - 1 ) ] ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD ; } } for ( int k = 0 ; k < c . Length ; ++ k ) { c [ k ] %= MOD ; Console . Write ( c [ k ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] B = { 5 , 6 , 7 , 8 , 9 } ; findConvolution ( A , B ) ; } }