codeparrot/xlcost-text-to-code huggingface dataset:
XOR of a submatrix queries | C # implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
using System ; class GfG { static int n = 3 ; static void preComputeXor ( int [ , ] arr , int [ , ] prefix_xor ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) prefix_xor [ i , j ] = arr [ i , j ] ; else prefix_xor [ i , j ] = ( prefix_xor [ i , j - 1 ] ^ arr [ i , j ] ) ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) prefix_xor [ j , i ] = ( prefix_xor [ j - 1 , i ] ^ prefix_xor [ j , i ] ) ; } static int ansQuerie ( int [ , ] prefix_xor , int x1 , int y1 , int x2 , int y2 ) { int xor_1 = 0 , xor_2 = 0 , xor_3 = 0 ; if ( x1 != 0 ) xor_1 = prefix_xor [ x1 - 1 , y2 ] ; if ( y1 != 0 ) xor_2 = prefix_xor [ x2 , y1 - 1 ] ; if ( x1 != 0 && y1 != 0 ) xor_3 = prefix_xor [ x1 - 1 , y1 - 1 ] ; return ( ( prefix_xor [ x2 , y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) ; } public static void Main ( ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int [ , ] prefix_xor = new int [ n , n ] ; preComputeXor ( arr , prefix_xor ) ; Console . WriteLine ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) ) ; Console . WriteLine ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) ; } }
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | C # implementation of the approach ; Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
using System ; class GFG { static bool isDigitSumOne ( int nm ) { if ( nm % 9 == 1 ) return true ; else return false ; } static void printValidNums ( int n ) { int cbrt_n = ( int ) Math . Ceiling ( Math . Pow ( n , ( double ) 1 / 3 ) ) ; for ( int i = 1 ; i <= cbrt_n ; i ++ ) { int cube = ( int ) Math . Pow ( i , 3 ) ; if ( cube >= 1 && cube <= n && isDigitSumOne ( cube ) ) Console . Write ( cube + " ▁ " ) ; } } static public void Main ( ) { int n = 1000 ; printValidNums ( n ) ; } }
Count the number of rhombi possible inside a rectangle of given size | C # program to find the frequency of minimum element in the array ; Function to return the count of rhombi possible ; All possible diagonal lengths ; Update rhombi possible with the current diagonal lengths ; Return the total count of rhombi possible ; Driver code
using System ; class GFG { static int countRhombi ( int h , int w ) { int ct = 0 ; for ( int i = 2 ; i <= h ; i += 2 ) for ( int j = 2 ; j <= w ; j += 2 ) ct += ( h - i + 1 ) * ( w - j + 1 ) ; return ct ; } public static void Main ( ) { int h = 2 , w = 2 ; Console . WriteLine ( countRhombi ( h , w ) ) ; } }
Program to calculate the area between two Concentric Circles | C # program to find area between the two given concentric circles ; Function to find area between the two given concentric circles ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver code
using System ; class GFG { static double calculateArea ( int x , int y ) { double pi = 3.1415926536 ; double arx = pi * x * x ; double ary = pi * y * y ; return arx - ary ; } public static void Main ( ) { int x = 2 ; int y = 1 ; Console . WriteLine ( calculateArea ( x , y ) ) ; } }
Find the winner by adding Pairwise difference of elements in the array until Possible | C # implementation of the approach ; Function to calculate gcd ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static char getWinner ( int [ ] arr , int n ) { int gcd = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( gcd , arr [ i ] ) ; maxEle = Math . Max ( maxEle , arr [ i ] ) ; } int totalMoves = ( maxEle / gcd ) - n ; if ( totalMoves % 2 == 1 ) return ' A ' ; return ' B ' ; } public static void Main ( ) { int [ ] arr = { 5 , 6 , 7 } ; int n = arr . Length ; Console . Write ( getWinner ( arr , n ) ) ; } }
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | C # implementation of the above approach ; Function to return the count of required pairs ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; To store the count of possible pairs ; Check all possible pairs ; Calculating the value of ( ( n % i ) % j ) % n ; If value is equal to maximum ; Return the number of possible pairs ; Driver code
using System ; class GFG { static int countPairs ( int n ) { int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { int val = ( ( n % i ) % j ) % n ; if ( val == max ) count ++ ; } } return count ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( countPairs ( n ) ) ; } }
Program to check if a number is divisible by any of its digits | C # implementation of above approach ; Function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if K divides N ; Driver Code
using System ; class GFG { static String isDivisible ( int n ) { int temp = n ; while ( n > 0 ) { int k = n % 10 ; if ( temp % k == 0 ) { return " YES " ; } n /= 10 ; } return " NO " ; } public static void Main ( String [ ] args ) { int n = 9876543 ; Console . WriteLine ( isDivisible ( n ) ) ; } }
Program to find sum of harmonic series | C # Program to find sum of harmonic series ; Function to return sum of harmonic series ; Driven Program
using System ; class GFG { static float sum ( int n ) { double i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return ( float ) s ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " Sum ▁ is ▁ " + sum ( n ) ) ; } }
Check if a number can be expressed as sum two abundant numbers | C # program to check if number n is expressed as sum of two abundant numbers ; Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 100005 ; static HashSet < int > ABUNDANT ( ) { HashSet < int > v = new HashSet < int > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { int sum = 1 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum += j ; if ( i / j != j ) { sum += i / j ; } } } if ( sum > i ) { v . Add ( i ) ; } } return v ; } static void SumOfAbundant ( int n ) { HashSet < int > v = ABUNDANT ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( v . Contains ( i ) & v . Contains ( n - i ) ) { Console . Write ( i + " ▁ " + ( n - i ) ) ; return ; } } Console . Write ( - 1 ) ; } public static void Main ( ) { int n = 24 ; SumOfAbundant ( n ) ; } }
Find nth term of the series 5 2 13 41 | C # program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; to store the nth term of series ; if n is even number ; if n is odd number ; return nth term ; Driver code
using System ; class GFG { static long nthTermOfTheSeries ( int n ) { long nthTerm ; if ( n % 2 == 0 ) nthTerm = ( long ) Math . Pow ( n - 1 , 2 ) + n ; else nthTerm = ( long ) Math . Pow ( n + 1 , 2 ) + n ; return nthTerm ; } public static void Main ( ) { int n ; n = 8 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 12 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 102 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 999 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 9999 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; } }
Find cost price from given selling price and profit or loss percentage | Csharp implementation to find Cost price ; Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
using System ; class solution { static float CPwithProfit ( int sellingPrice , int profit ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 + profit ) ; return costPrice ; } static float CPwithLoss ( int sellingPrice , int loss ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 - loss ) ; return costPrice ; } public static void Main ( ) { int SP , profit , loss ; SP = 1020 ; profit = 20 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; SP = 900 ; loss = 10 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithLoss ( SP , loss ) ) ; SP = 42039 ; profit = 8 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; }
Check whether a number is Non | C # program to check if a given number is Non - Hypotenuse number or not . ; Function to find prime factor and check if it is of the form 4 k + 1 or not ; 2 is a prime number but not of the form 4 k + 1 so , keep Dividing n by 2 until n is divisible by 2 ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; if i divides n check if i is of the form 4 k + 1 or not ; while i divides n divide n by i and update n ; This condition is to handle the case when n is a prime number greater than 2 ; Test function ; Driver code
using System ; class GFG { static bool isNonHypotenuse ( int n ) { while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { if ( ( i - 1 ) % 4 == 0 ) return false ; while ( n % i == 0 ) { n = n / i ; } } } if ( n > 2 && ( n - 1 ) % 4 == 0 ) return false ; else return true ; } public static void test ( int n ) { Console . WriteLine ( " Testing ▁ for ▁ " + n + " ▁ : ▁ " ) ; if ( isNonHypotenuse ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int n = 11 ; test ( n ) ; n = 10 ; test ( n ) ; } }
Find the n | C # Implementation of the above approach ; Function to return the nth string in the required sequence ; Length of the resultant string ; Relative Index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Utility reverse function ; Driver function
using System ; using System . Text ; class GFG { static String obtain_str ( int n ) { int len = ( int ) Math . Floor ( ( Math . Log ( n + 1 ) / Math . Log ( 2 ) ) ) ; int rel_ind = n + 1 - ( int ) Math . Pow ( 2 , len ) ; int i = 0 ; StringBuilder str = new StringBuilder ( ) ; for ( i = 0 ; i < len ; i ++ ) { str . Append ( ' a ' ) ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' b ' ; rel_ind /= 2 ; i ++ ; } return reverse ( str . ToString ( ) ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int n = 11 ; Console . Write ( obtain_str ( n ) ) ; } }
Program to find the Nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | C # implementation of the approach ; Function to return the nth term of the given series ; nth term ; Driver code
using System ; public class GFG { static void NthTerm ( int n ) { int numerator = ( ( int ) Math . Pow ( n , 2 ) ) - 1 ; int denomeanator = 2 * n - 3 ; Console . WriteLine ( numerator + " / " + denomeanator ) ; } public static void Main ( ) { int n = 3 ; NthTerm ( n ) ; } }
Sum of each element raised to ( prime | C # implementation of the approach ; Function to return the required sum ; Driver code
using System ; public class GFG { public static int getSum ( int [ ] arr , int p ) { return arr . Length ; } static public void Main ( ) { int [ ] arr = { 5 , 6 , 8 } ; int p = 7 ; Console . WriteLine ( getSum ( arr , p ) ) ; } }
Count numbers upto N which are both perfect square and perfect cube | C # implementation of the above approach ; Function to return required count ; Driver Code ; function call to print required answer
using System ; public class GFG { static int SquareCube ( long N ) { int cnt = 0 , i = 1 ; while ( ( int ) ( Math . Pow ( i , 6 ) ) <= N ) { ++ cnt ; ++ i ; } return cnt ; } public static void Main ( ) { long N = 100000 ; Console . WriteLine ( SquareCube ( N ) ) ; } }
Sum of integers upto N with given unit digit | C # implementation of the approach ; Function to return the required sum ; Driver code
using System ; class GFG { static long getSum ( int n , int d ) { long sum = 0 ; while ( d <= n ) { sum += d ; d += 10 ; } return sum ; } public static void Main ( ) { int n = 30 , d = 3 ; Console . Write ( getSum ( n , d ) ) ; } }
Summing the sum series | C # program to calculate the terms of summing of sum series ; Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static long sum ( long N ) { long val = N * ( N + 1 ) ; val = val % MOD ; return val ; } static int sumX ( int N , int M , int K ) { for ( int i = 0 ; i < M ; i ++ ) { N = ( int ) sum ( K + N ) ; } N = N % MOD ; return N ; } public static void Main ( ) { int N = 1 , M = 2 , K = 3 ; Console . WriteLine ( sumX ( N , M , K ) ) ; } }
Logarithm | C # program to find log ( n ) using Recursion ; Driver Code
using System ; class GFG { static int Log2n ( int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } public static void Main ( ) { int n = 32 ; Console . Write ( Log2n ( n ) ) ; } }
Mode | C # Program for Mode using Counting Sort technique ; Function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver Code
using System ; using System . Linq ; public class GFG { static void printMode ( int [ ] a , int n ) { int max = a . Max ( ) ; int t = max + 1 ; int [ ] count = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } Console . WriteLine ( " mode ▁ = ▁ " + mode ) ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . Length ; printMode ( a , n ) ; } }
Harmonic Progression | C # program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; Driver code ; series to check whether it is in H . P ; Checking a series is in H . P or not
using System ; using System . Collections ; class GFG { static bool checkIsHP ( double [ ] arr ) { int n = arr . Length ; if ( n == 1 ) return true ; ArrayList rec = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) rec . Add ( ( int ) ( 1 / arr [ i ] ) ) ; rec . Sort ( ) ; int d = ( int ) rec [ 1 ] - ( int ) rec [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( ( int ) rec [ i ] - ( int ) rec [ i - 1 ] != d ) return false ; return true ; } public static void Main ( ) { double [ ] arr = { 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 } ; if ( checkIsHP ( arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Arithmetic Mean | C # program to illustrate n arithmetic mean between A and B ; insert function for calculating the means ; Finding the value of d Common difference ; for finding N the Arithmetic mean between A and B ; Driver code
using System ; public class GFG { static void printAMeans ( int A , int B , int N ) { float d = ( float ) ( B - A ) / ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( ( A + i * d ) + " ▁ " ) ; } public static void Main ( ) { int A = 20 , B = 32 , N = 5 ; printAMeans ( A , B , N ) ; } }
Prime Factor | C # Program to print all prime factors ; A function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
using System ; namespace prime { public class GFG { public static void primeFactors ( int n ) { while ( n % 2 == 0 ) { Console . Write ( 2 + " ▁ " ) ; n /= 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { Console . Write ( i + " ▁ " ) ; n /= i ; } } if ( n > 2 ) Console . Write ( n ) ; } public static void Main ( ) { int n = 315 ; primeFactors ( n ) ; } } }
Time taken by two persons to meet on a circular track | C # implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function
using System ; public class GFG { static int startingPoint ( int Length , int Speed1 , int Speed2 ) { int result1 = 0 , result2 = 0 ; int time1 = Length / Speed1 ; int time2 = Length / Speed2 ; result1 = __gcd ( time1 , time2 ) ; result2 = time1 * time2 / ( result1 ) ; return result2 ; } static int __gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return __gcd ( b , a % b ) ; } static float firstTime ( int Length , int Speed1 , int Speed2 ) { float result = 0 ; int relativeSpeed = Math . Abs ( Speed1 - Speed2 ) ; result = ( ( float ) Length / relativeSpeed ) ; return result ; } public static void Main ( ) { int L = 30 , S1 = 5 , S2 = 2 ; float first_Time = firstTime ( L , S1 , S2 ) ; int starting_Point = startingPoint ( L , S1 , S2 ) ; Console . WriteLine ( " Met ▁ first ▁ time ▁ after ▁ " + first_Time + " ▁ hrs " ) ; Console . WriteLine ( " Met ▁ at ▁ starting ▁ point ▁ after ▁ " + starting_Point + " ▁ hrs " ) ; } }
Check if the array has an element which is equal to product of remaining elements | C # implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver Code
class GFG { static bool CheckArray ( int [ ] arr , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) prod *= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] == prod / arr [ i ] ) return true ; return false ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 12 , 3 , 2 } ; int n = arr . Length ; if ( CheckArray ( arr , n ) ) System . Console . WriteLine ( " YES " ) ; else System . Console . WriteLine ( " NO " ) ; } }
Sum of common divisors of two numbers A and B | C # implementation of above approach ; print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors
using System ; class GFG { static int sum ( int a , int b ) { int sum = 0 ; for ( int i = 1 ; i <= Math . Min ( a , b ) ; i ++ ) if ( a % i == 0 && b % i == 0 ) sum += i ; return sum ; } public static void Main ( ) { int A = 10 , B = 15 ; Console . WriteLine ( " Sum ▁ = ▁ " + sum ( A , B ) ) ; } }
Minimum number of cuts required to make circle segments equal sized | C # program to find the minimum number of additional cuts required to make circle segments equal sized ; Recursive function to return gcd of two nos ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver Code
using System ; class GFG { static int findgcd ( int a , int b ) { if ( b == 0 ) return a ; return findgcd ( b , a % b ) ; } static int minimumCuts ( int [ ] a , int n ) { Array . Sort ( a ) ; int gcd = a [ 1 ] - a [ 0 ] ; int s = gcd ; for ( int i = 2 ; i < n ; i ++ ) { gcd = findgcd ( gcd , a [ i ] - a [ i - 1 ] ) ; s += a [ i ] - a [ i - 1 ] ; } if ( 360 - s > 0 ) gcd = findgcd ( gcd , 360 - s ) ; return ( 360 / gcd ) - n ; } static void Main ( ) { int [ ] arr = new int [ ] { 30 , 60 , 180 } ; int n = arr . Length ; Console . WriteLine ( minimumCuts ( arr , n ) ) ; } }
Find a number that divides maximum array elements | C # program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Function to find a number that divides maximum array elements ; precalculating Smallest Prime Factor ; Hash to store frequency of each divisors ; Traverse the array and get spf of each element ; calling getFactorization function ; Returns Set view ; Driver program
using System ; using System . Collections . Generic ; class Solution { static readonly int MAXN = 100001 ; static int [ ] spf = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static List < int > getFactorization ( int x ) { List < int > ret = new List < int > ( ) ; while ( x != 1 ) { int temp = spf [ x ] ; ret . Add ( temp ) ; while ( x % temp == 0 ) x = x / temp ; } return ret ; } static int maxElement ( int [ ] A , int n ) { sieve ( ) ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int j = 0 ; j < n ; ++ j ) { List < int > p = getFactorization ( A [ j ] ) ; for ( int i = 0 ; i < p . Count ; i ++ ) if ( m . ContainsKey ( p [ i ] ) ) m [ p [ i ] ] = m [ p [ i ] ] + 1 ; else m . Add ( p [ i ] , 1 ) ; } int cnt = 0 , ans = 10000000 ; foreach ( KeyValuePair < int , int > me in m ) { if ( me . Value >= cnt ) { cnt = me . Value ; if ( ans > me . Key ) ans = me . Key ; else ans = ans ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 5 , 10 } ; int n = A . Length ; Console . Write ( maxElement ( A , n ) ) ; } }
Find Selling Price from given Profit Percentage and Cost | C # implementation of above approach ; calculate Nth term of series ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Driver Code ; Get the CP Profit % ; Printing the returned value
using System ; class GFG { static float SellingPrice ( float CP , float PP ) { float P_decimal = 1 + ( PP / 100 ) ; float res = P_decimal * CP ; return res ; } public static void Main ( ) { float C = 720 , P = 13 ; Console . Write ( SellingPrice ( C , P ) ) ; } }