"An in | C # implementation of the above approach ; A utility function to reverse String str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; Keep the back - up of element at new position ; The main function to transform a String . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - String . ; Step 4.3 Reverse the second half of first sub - String and first half of second sub - String together ; Increase the length of first subarray ; Driver code"
Solution in C#:

using System ; class GFG { static char [ ] str ; static void reverse ( int low , int high ) { while ( low < high ) { char t = str [ low ] ; str [ low ] = str [ high ] ; str [ high ] = t ; ++ low ; -- high ; } } static void cycleLeader ( int shift , int len ) { int j ; char item ; for ( int i = 1 ; i < len ; i *= 3 ) { j = i ; item = str [ j + shift ] ; do { if ( j % 2 == 1 ) j = len / 2 + j / 2 ; else j /= 2 ; char t = str [ j + shift ] ; str [ j + shift ] = item ; item = t ; } while ( j != i ) ; } } static void moveNumberToSecondHalf ( ) { int k , lenFirst ; int lenRemaining = str . Length ; int shift = 0 ; while ( lenRemaining > 0 ) { k = 0 ; while ( Math . Pow ( 3 , k ) + 1 <= lenRemaining ) k ++ ; lenFirst = ( int ) Math . Pow ( 3 , k - 1 ) + 1 ; lenRemaining -= lenFirst ; cycleLeader ( shift , lenFirst ) ; reverse ( shift / 2 , shift - 1 ) ; reverse ( shift , shift + lenFirst / 2 - 1 ) ; reverse ( shift / 2 , shift + lenFirst / 2 - 1 ) ; shift += lenFirst ; } } public static void Main ( String [ ] args ) { String st = " a1b2c3d4e5f6g7" ; str = st . ToCharArray ( ) ; moveNumberToSecondHalf ( ) ; Console . Write ( str ) ; } }


"An interesting method to print reverse of a linked list | C # program to print reverse of list ; Represents node of a linkedlist ; Function to reverse the linked list ; For each node , print proper number of spaces before printing it ; use of carriage return to move back and print . ; Function to push a node ; Function to print linked list and find its length ; i for finding length of list ; Driver code ; Start with the empty list ; list nodes are as 6 5 4 3 2 1 ; printlist print the list and return the size of list ; print reverse list with help of carriage return function"
Solution in C#:

using System ; public class Node { public int data ; public Node next ; public Node ( int val ) { data = val ; next = null ; } } public class GFG { static void printReverse ( Node head , int n ) { int j = 0 ; Node current = head ; while ( current != null ) { for ( int i = 0 ; i < 2 * ( n - j ) ; i ++ ) Console . Write ( " ▁ " ) ; Console . Write ( " \r " + current . data ) ; current = current . next ; j ++ ; } } static Node push ( Node head , int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; return head ; } static int printList ( Node head ) { int i = 0 ; Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; i ++ ; } return i ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 3 ) ; head = push ( head , 4 ) ; head = push ( head , 5 ) ; head = push ( head , 6 ) ; Console . WriteLine ( " Given ▁ linked ▁ list : ▁ " ) ; int n = printList ( head ) ; Console . WriteLine ( " Reversed ▁ Linked ▁ list : ▁ " ) ; printReverse ( head , n ) ; Console . WriteLine ( ) ; } }


"An interesting solution to get all prime numbers smaller than n | C # program prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code"
Solution in C#:

class GFG { static void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) System . Console . WriteLine ( k ) ; } } static void Main ( ) { int n = 15 ; primesInRange ( n ) ; } }


"Anagram Substring Search ( Or Search for all permutations ) | C # program to search all anagrams of a pattern in a text ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver Code"
Solution in C#:

using System ; class GFG { public const int MAX = 256 ; public static bool compare ( char [ ] arr1 , char [ ] arr2 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { return false ; } } return true ; } public static void search ( string pat , string txt ) { int M = pat . Length ; int N = txt . Length ; char [ ] countP = new char [ MAX ] ; char [ ] countTW = new char [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { Console . WriteLine ( " Found ▁ at ▁ Index ▁ " + ( i - M ) ) ; } ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) { Console . WriteLine ( " Found ▁ at ▁ Index ▁ " + ( N - M ) ) ; } } public static void Main ( string [ ] args ) { string txt = " BACDGABCDA " ; string pat = " ABCD " ; search ( pat , txt ) ; } }


"Analysis of Algorithms | Big | C # program for above approach ; Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static bool linearSearch ( int [ ] a , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = arr . Length ; if ( linearSearch ( arr , n , x ) ) Console . Write ( " Element ▁ is ▁ present ▁ in ▁ array " ) ; else Console . Write ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; } }


"Analysis of Algorithms | Big | C # program for above approach ; Function to print all possible pairs ; Driver Code ; Given array ; Store the size of the array ; Function Call"
Solution in C#:

using System ; class GFG { static void print ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j ) Console . WriteLine ( a [ i ] + " ▁ " + a [ j ] ) ; } } } static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; print ( a , n ) ; } }


"Analysis of Algorithms | Set 2 ( Worst , Average and Best Cases ) | C # implementation of the approach ; Linearly search x in arr [ ] . If x is present then return the index , otherwise return - 1 ; Driver program to test above functions"
Solution in C#:

using System ; public class GFG { static int search ( int [ ] arr , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { return i ; } } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 10 , 30 , 15 } ; int x = 30 ; int n = arr . Length ; Console . WriteLine ( x + " ▁ is ▁ present ▁ at ▁ index ▁ " + search ( arr , n , x ) ) ; } }


"Analysis of Algorithms | Set 5 ( Practice Problems ) |"
Solution in C#:

static void function ( int n ) { int count = 0 ; for ( int i = n / 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j = 2 * j ) for ( int k = 1 ; k <= n ; k = k * 2 ) count ++ ; }


"Angle between a Pair of Lines in 3D | C # program for the above approach ; Function to find the angle between the two lines ; Find direction ratio of line AB ; Find direction ratio of line BC ; Find the dotProduct of lines AB & BC ; Find magnitude of line AB and BC ; Find the cosine of the angle formed by line AB and BC ; Find angle in radian ; Print the angle ; Driver code ; Given coordinates Points A ; Points B ; Points C ; Function Call"
Solution in C#:

using System ; class GFG { static void calculateAngle ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int x3 , int y3 , int z3 ) { int ABx = x1 - x2 ; int ABy = y1 - y2 ; int ABz = z1 - z2 ; int BCx = x3 - x2 ; int BCy = y3 - y2 ; int BCz = z3 - z2 ; double dotProduct = ABx * BCx + ABy * BCy + ABz * BCz ; double magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz ; double magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz ; double angle = dotProduct ; angle /= Math . Sqrt ( magnitudeAB * magnitudeBC ) ; angle = ( angle * 180 ) / 3.14 ; Console . Write ( String . Format ( " { 0 : F4 } " , Math . Abs ( angle ) ) ) ; } public static void Main ( ) { int x1 = 1 , y1 = 3 , z1 = 3 ; int x2 = 3 , y2 = 4 , z2 = 5 ; int x3 = 5 , y3 = 6 , z3 = 9 ; calculateAngle ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) ; } }


"Angle between a Pair of Lines | C # program for the above approach ; Function to find the angle between two lines ; Store the tan value of the angle ; Calculate tan inverse of the angle ; Convert the angle from radian to degree ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static double PI = 3.14159265 ; static void findAngle ( double M1 , double M2 ) { double angle = Math . Abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) ; double ret = Math . Atan ( angle ) ; double val = ( ret * 180 ) / PI ; Console . Write ( val ) ; } public static void Main ( ) { double M1 = 1.75 , M2 = 0.27 ; findAngle ( M1 , M2 ) ; } }


"Angle between a chord and a tangent when angle in the alternate segment is given | C # program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code"
Solution in C#:

using System ; class GFG { static void anglechordtang ( int z ) { Console . WriteLine ( " The ▁ angle ▁ between ▁ tangent " + " ▁ and ▁ the ▁ chord ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void Main ( ) { int z = 48 ; anglechordtang ( z ) ; } }


"Angle between two Planes in 3D | C # program to find the Angle between two Planes in 3 D . ; Function to find Angle ; Driver code"
Solution in C#:

using System ; class GFG { static void distance ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { float d = ( a1 * a2 + b1 * b2 + c1 * c2 ) ; float e1 = ( float ) Math . Sqrt ( a1 * a1 + b1 * b1 + c1 * c1 ) ; float e2 = ( float ) Math . Sqrt ( a2 * a2 + b2 * b2 + c2 * c2 ) ; d = d / ( e1 * e2 ) ; float pi = ( float ) 3.14159 ; float A = ( 180 / pi ) * ( float ) ( Math . Acos ( d ) ) ; Console . Write ( " Angle ▁ is ▁ " + A + " ▁ degree " ) ; } public static void Main ( ) { float a1 = 1 ; float b1 = 1 ; float c1 = 2 ; float a2 = 2 ; float b2 = - 1 ; float c2 = 1 ; distance ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }


"Angle of intersection of two circles having their centers D distance apart | C # program for the above approach ; Function to find the cosine of the angle of the intersection of two circles with radius R1 and R2 ; Return the cosine of the angle ; Driver Code"
Solution in C#:

using System ; class GFG { static float angle ( float R1 , float R2 , float D ) { float ans = ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ; return ans ; } public static void Main ( string [ ] args ) { float R1 = 3 , R2 = 4 ; float D = 5 ; Console . Write ( angle ( R1 , R2 , D ) ) ; } }


"Angle subtended by an arc at the centre of a circle | C # implementation of the approach ; Function to find Angle subtended by an arc at the centre of a circle ; Driver code"
Solution in C#:

using System ; class GFG { static int angle ( int n ) { return 2 * n ; } public static void Main ( ) { int n = 30 ; Console . Write ( angle ( n ) ) ; } }


"Angle subtended by the chord when the angle subtended by another chord of same length is given | C # program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code"
Solution in C#:

using System ; class GFG { static void angleequichord ( int z ) { Console . WriteLine ( " The ▁ angle ▁ subtended ▁ at ▁ the ▁ center ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void Main ( String [ ] args ) { int z = 48 ; angleequichord ( z ) ; } }


"Apocalyptic Number | C # program for the above approach ; Function to check if a number N is Apocalyptic ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static bool isApocalyptic ( int n ) { if ( Math . Pow ( 2 , n ) . ToString ( ) . Contains ( "666" ) ) return true ; return false ; } static public void Main ( ) { int N = 157 ; if ( isApocalyptic ( N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Append X digits to the end of N to make it divisible by M | C # program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver code ; Stores the number by appending X digits on the right side of N"
Solution in C#:

using System ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void Main ( ) { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; Console . WriteLine ( res ) ; } }


"Append a digit in the end to make the number equal to the length of the remaining string | C # implementation of the approach ; Function to return the required digit ; To store the position of the first numeric digit in the string ; To store the length of the string without the numeric digits in the end ; pw stores the current power of 10 and num is to store the number which is appended in the end ; If current character is a numeric digit ; Get the current digit ; Build the number ; If number exceeds the length ; Next power of 10 ; Append 0 in the end ; Required number that must be added ; If number is not a single digit ; Driver code"
Solution in C#:

using System ; class GFG { static int find_digit ( String s , int n ) { int first_digit = - 1 , i ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] < '0' s [ i ] > '9' ) { first_digit = i ; break ; } } first_digit ++ ; int s_len = first_digit ; int num = 0 , pw = 1 ; i = n - 1 ; while ( i >= 0 ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { int digit = s [ i ] - '0' ; num = num + ( pw * digit ) ; if ( num >= s_len ) return - 1 ; pw = pw * 10 ; } i -- ; } num = num * 10 ; int req = s_len - num ; if ( req > 9 req < 0 ) return - 1 ; return req ; } public static void Main ( String [ ] args ) { String s = " abcd0" ; int n = s . Length ; Console . Write ( find_digit ( s , n ) ) ; } }


"Append digits to the end of duplicate strings to make all strings in an array unique | C # program for the above approach ; Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void replaceDuplicates ( string [ ] names ) { Dictionary < string , int > hash = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < names . Length ; i ++ ) { if ( ! hash . ContainsKey ( names [ i ] ) ) hash [ names [ i ] ] = 1 ; else { int count = hash [ names [ i ] ] ; hash [ names [ i ] ] += 1 ; names [ i ] += count . ToString ( ) ; } } for ( int i = 0 ; i < names . Length ; i ++ ) { Console . Write ( names [ i ] + ' ▁ ' ) ; } } public static void Main ( ) { string [ ] str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; } }


"Append two elements to make the array satisfy the given condition | C # implementation of the approach ; Function to find the required numbers ; Find the sum and xor ; Print the required elements ; Driver code"
Solution in C#:

using System ; class GFG { static void findNums ( int [ ] arr , int n ) { int S = 0 , X = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += arr [ i ] ; X ^= arr [ i ] ; } Console . WriteLine ( X + " ▁ " + ( X + S ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 7 } ; int n = arr . Length ; findNums ( arr , n ) ; } }


"Area of Circumcircle of a Right Angled Triangle | C # code to find the area of circumscribed circle of right angled triangle ; Function to find the area of circumscribed circle ; Driver code"
Solution in C#:

using System ; class GFG { static double PI = 3.14159265 ; public static double area_cicumscribed ( double c ) { return ( c * c * ( PI / 4 ) ) ; } public static void Main ( ) { double c = 8.0 ; Console . Write ( area_cicumscribed ( c ) ) ; } }


"Area of Circumcircle of an Equilateral Triangle using Median | C # implementation to find the equation of circle which inscribes equilateral triangle of median M ; Function to find the equation of circle whose center is ( x1 , y1 ) and the radius of circle is r ; Function to find the equation of circle which inscribes equilateral triangle of median M ; Function call to find the circle equation ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static double circleArea ( double r ) { double pi = 3.14159265358979323846 ; return ( pi * r * r ) ; } static double findCircleAreaByMedian ( int m ) { double r = 2 * m / 3 ; return circleArea ( r ) ; } public static void Main ( string [ ] args ) { int m = 3 ; Console . WriteLine ( " { 0 : f4 } " , findCircleAreaByMedian ( m ) ) ; } }


"Area of Incircle of a Right Angled Triangle | C # code to find the area of inscribed circle of right angled triangle ; Function to find the area of inscribed circle ; Driver code"
Solution in C#:

using System ; class GFG { static double PI = 3.14159265 ; public static double area_inscribed ( double P , double B , double H ) { return ( ( P + B - H ) * ( P + B - H ) * ( PI / 4 ) ) ; } public static void Main ( ) { double P = 3.0 , B = 4.0 , H = 5.0 ; Console . Write ( area_inscribed ( P , B , H ) ) ; } }


"Area of Largest rectangle that can be inscribed in an Ellipse | C # Program to find the biggest rectangle which can be inscribed within the ellipse ; Function to find the area of the rectangle ; a and b cannot be negative ; area of the rectangle ; Driver code"
Solution in C#:

using System ; class GFG { static float rectanglearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; return 2 * a * b ; } public static void Main ( ) { float a = 10 , b = 8 ; Console . WriteLine ( rectanglearea ( a , b ) ) ; } }


"Area of Reuleaux Triangle | C # Program to find the area of Reuleaux triangle ; Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code"
Solution in C#:

using System ; public class GFG { static double ReuleauxArea ( float a ) { if ( a < 0 ) return - 1 ; double A = ( double ) 0.70477 * Math . Pow ( a , 2 ) ; return A ; } public static void Main ( ) { float a = 6 ; Console . WriteLine ( ReuleauxArea ( a ) ) ; } }


"Area of Triangle using Side | C # program to calculate the area of a triangle when the length of two adjacent sides and the angle between them is provided ; Function to return the area of triangle using Side - Angle - Side formula ; Driver Code ; Function Call ; Print the readonly answer"
Solution in C#:

using System ; class GFG { static float Area_of_Triangle ( int a , int b , int k ) { float area = ( float ) ( ( 1 / 2.0 ) * a * b * Math . Sin ( k ) ) ; return area ; } public static void Main ( String [ ] args ) { int a = 9 ; int b = 12 ; int k = 2 ; float ans = Area_of_Triangle ( a , b , k ) ; Console . Write ( " { 0 : F1 } " , ans ) ; } }


"Area of a Triangle from the given lengths of medians | C # program to calculate area of a triangle from the given lengths of medians ; Function to return the area of triangle using medians ; Driver Code ; Function call ; Print the final answer"
Solution in C#:

using System ; class GFG { static double Area_of_Triangle ( int a , int b , int c ) { int s = ( a + b + c ) / 2 ; int x = s * ( s - a ) ; x = x * ( s - b ) ; x = x * ( s - c ) ; double area = ( 4 / ( double ) 3 ) * Math . Sqrt ( x ) ; return area ; } public static void Main ( String [ ] args ) { int a = 9 ; int b = 12 ; int c = 15 ; double ans = Area_of_Triangle ( a , b , c ) ; Console . WriteLine ( ans ) ; } }


"Area of a largest square fit in a right angle triangle | C # Program to find the area of the biggest square which can fit inside the right angled triangle ; Function to find the area of the biggest square ; the height or base or hypotenuse cannot be negative ; side of the square ; squaring to get the area ; Driver code"
Solution in C#:

using System ; class GFG { static float squareArea ( float l , float b , float h ) { if ( l < 0 b < 0 h < 0 ) return - 1 ; float a = ( l * b ) / ( l + b ) ; return a * a ; } public static void Main ( ) { float l = 5 , b = 12 , h = 13 ; Console . WriteLine ( squareArea ( l , b , h ) ) ; } }


"Area of a leaf inside a square | C # code to find the area of leaf inside square ; Function to find the area of leaf ; Driver code"
Solution in C#:

using System ; class GFG { static double PI = 3.14159265 ; public static double area_leaf ( double a ) { return ( a * a * ( PI / 2 - 1 ) ) ; } public static void Main ( ) { double a = 7 ; Console . Write ( area_leaf ( a ) ) ; } }


"Area of a polygon with given n ordered vertices | C # program to evaluate area of a polygon using shoelace formula ; ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j is previous vertex to i ; Return absolute value ; Driver program"
Solution in C#:

using System ; class GFG { public static double polygonArea ( double [ ] X , double [ ] Y , int n ) { double area = 0.0 ; int j = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ; j = i ; } return Math . Abs ( area / 2.0 ) ; } public static void Main ( ) { double [ ] X = { 0 , 2 , 4 } ; double [ ] Y = { 1 , 3 , 7 } ; int n = 3 ; Console . WriteLine ( polygonArea ( X , Y , n ) ) ; } }


"Area of a square from diagonal length | C # Program to find the area of square when its diagonal is given . ; Returns area of square from given diagonal ; Driver code"
Solution in C#:

using System ; class GFG { static double findArea ( double d ) { return ( d * d ) / 2 ; } public static void Main ( ) { double d = 10 ; Console . WriteLine ( findArea ( d ) ) ; } }


"Area of a triangle inscribed in a rectangle which is inscribed in an ellipse | C # Program to find the area of the triangle inscribed within the rectangle which in turn is inscribed in an ellipse ; Function to find the area of the triangle ; length of a and b cannot be negative ; area of the triangle ; Driver code"
Solution in C#:

using System ; class GFG { static float area ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; float A = a * b ; return A ; } static public void Main ( ) { float a = 5 , b = 2 ; Console . WriteLine ( area ( a , b ) ) ; } }


"Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square | C # program for the above approach ; Function to find the area of the triangle that inscribed in square ; Stores the length of the first side of triangle ; Stores the length of the second side of triangle ; Stores the length of the third side of triangle ; Stores the area of the triangle ; Return the resultant area ; Driver code"
Solution in C#:

using System ; class GFG { static double areaOftriangle ( int side ) { double a = Math . Sqrt ( Math . Pow ( side / 2 , 2 ) + Math . Pow ( side / 2 , 2 ) ) ; double b = Math . Sqrt ( Math . Pow ( side , 2 ) + Math . Pow ( side / 2 , 2 ) ) ; double c = Math . Sqrt ( Math . Pow ( side , 2 ) + Math . Pow ( side / 2 , 2 ) ) ; double s = ( a + b + c ) / 2 ; double area = Math . Sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; return area ; } public static void Main ( string [ ] args ) { int N = 10 ; Console . WriteLine ( areaOftriangle ( N ) ) ; } }


"Area of circle inscribed within rhombus | C # Program to find the area of the circle which can be inscribed within the rhombus ; Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code"
Solution in C#:

using System ; public class GFG { public static float circlearea ( double a , double b ) { if ( a < 0 b < 0 ) return - 1 ; float A = ( float ) ( ( 3.14 * Math . Pow ( a , 2 ) * Math . Pow ( b , 2 ) ) / ( 4 * ( Math . Pow ( a , 2 ) + Math . Pow ( b , 2 ) ) ) ) ; return A ; } public static void Main ( ) { float a = 8 , b = 10 ; Console . WriteLine ( circlearea ( a , b ) ) ; } }


"Area of circle which is inscribed in equilateral triangle | C # program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double PI = 3.14 ; static double circle_inscribed ( int a ) { return PI * ( a * a ) / 12 ; } public static void Main ( ) { int a = 4 ; Console . WriteLine ( circle_inscribed ( a ) ) ; } }


"Area of decagon inscribed within the circle | C # Program to find the area of the decagon inscribed within a circle ; Function to find the area of the decagon ; radius cannot be negative ; area of the decagon ; Driver code"
Solution in C#:

using System ; class GFG { static double area ( double r ) { if ( r < 0 ) return - 1 ; double area = ( 5 * Math . Pow ( r , 2 ) * ( 3 - Math . Sqrt ( 5 ) ) * ( Math . Sqrt ( 5 ) + ( ( 2 * Math . Sqrt ( 5 ) ) ) ) / 4 ) ; return area ; } static public void Main ( ) { double r = 8 ; Console . WriteLine ( area ( r ) ) ; } }


"Area of hexagon with given diagonal length | C # program to find the area of Hexagon with given diagonal ; Function to calculate area ; Formula to find area ; Code driven"
Solution in C#:

using System ; public class GFG { public static float hexagonArea ( float d ) { return ( float ) ( ( 3 * Math . Sqrt ( 3 ) * d * d ) / 8 ) ; } static public void Main ( ) { float d = 10 ; Console . WriteLine ( " Area ▁ of ▁ hexagon : ▁ " + hexagonArea ( d ) ) ; } }


"Area of largest isosceles triangle that can be inscribed in an Ellipse whose vertex coincides with one extremity of the major axis | C # program for the above approach ; Function to calculate area of the isosceles triangle ; If a and b are negative ; Stores the area of the triangle ; Print the area ; Driver Code ; Given value of a & b ; Function call to find the area of the isosceles triangle"
Solution in C#:

using System ; public class GFG { static void triangleArea ( float a , float b ) { if ( a < 0 b < 0 ) { Console . WriteLine ( - 1 ) ; return ; } float area = ( 3 * ( float ) Math . Sqrt ( 3 ) * a * b ) / ( 4 ) ; Console . WriteLine ( area ) ; } public static void Main ( string [ ] args ) { float a = 1 , b = 2 ; triangleArea ( a , b ) ; } }


"Area of largest semicircle that can be drawn inside a square | C # program to find Area of semicircle in a square ; Function to find area of semicircle ; Driver code ; side of a square ; Call Function to find the area of semicircle"
Solution in C#:

using System ; class GFG { static float find_Area ( float a ) { float R = a * ( float ) ( 2.0 - Math . Sqrt ( 2 ) ) ; float area = ( float ) ( ( 3.14 * R * R ) / 2.0 ) ; return area ; } public static void Main ( string [ ] args ) { float a = 4 ; Console . WriteLine ( " ▁ Area ▁ of ▁ semicircle ▁ = ▁ " + find_Area ( a ) ) ; } }


"Area of largest triangle that can be inscribed within a rectangle | C # Program to find the biggest triangle which can be inscribed within the rectangle ; Function to find the area of the triangle ; a and b cannot be negative ; area of the triangle ; Driver code"
Solution in C#:

using System ; class GFG { static float trianglearea ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float area = ( l * b ) / 2 ; return area ; } public static void Main ( ) { float l = 5 , b = 4 ; Console . WriteLine ( trianglearea ( l , b ) ) ; } }


"Area of plot remaining at the end | C # implementation of the approach ; Function to return the area of the remaining plot ; Continue while plot has positive area and there are persons left ; If length > breadth then subtract breadth from length ; Else subtract length from breadth ; Driver code"
Solution in C#:

using System ; class GFG { static int remainingArea ( int N , int M , int K ) { while ( K -- > 0 && N > 0 && M > 0 ) { if ( N > M ) N = N - M ; else M = M - N ; } if ( N > 0 && M > 0 ) return N * M ; else return 0 ; } static public void Main ( ) { int N = 5 , M = 3 , K = 2 ; Console . WriteLine ( remainingArea ( N , M , K ) ) ; } }


"Area of square Circumscribed by Circle | C # program to find Area of square Circumscribed by Circle ; Function to find area of square ; Driver code ; Radius of a circle ; Call Function to find an area of square"
Solution in C#:

using System ; class GFG { static int find_Area ( int r ) { return ( 2 * r * r ) ; } public static void Main ( ) { int r = 3 ; Console . WriteLine ( " ▁ Area ▁ of ▁ square ▁ = ▁ " + find_Area ( r ) ) ; } }


"Area of the Largest Triangle inscribed in a Hexagon | C # Program to find the biggest triangle which can be inscribed within the hexagon ; Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code"
Solution in C#:

using System ; class GFG { static double trianglearea ( double a ) { if ( a < 0 ) return - 1 ; double area = ( 3 * Math . Sqrt ( 3 ) * Math . Pow ( a , 2 ) ) / 4 ; return Math . Round ( area , 4 ) ; } public static void Main ( ) { double a = 6 ; Console . WriteLine ( trianglearea ( a ) ) ; } }


"Area of the Largest square that can be inscribed in an ellipse | C # Program to find the biggest square which can be inscribed within the ellipse ; Function to find the area of the square ; a and b cannot be negative ; area of the square ; Driver code"
Solution in C#:

using System ; class GFG { static float squarearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; float area = 4 * ( float ) ( ( Math . Pow ( a , 2 ) + Math . Pow ( b , 2 ) ) / ( Math . Pow ( a , 2 ) * Math . Pow ( b , 2 ) ) ) ; return area ; } public static void Main ( ) { float a = 4 , b = 2 ; Console . WriteLine ( squarearea ( a , b ) ) ; } }


"Area of the biggest ellipse inscribed within a rectangle | C # Program to find the biggest rectangle which can be inscribed within the ellipse ; Function to find the area of the rectangle ; The sides cannot be negative ; Area of the ellipse ; Driver code"
Solution in C#:

using System ; class GFG { static float ellipse ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float x = ( float ) ( 3.14 * l * b ) / 4 ; return x ; } public static void Main ( ) { float a = 5 , b = 3 ; Console . WriteLine ( ellipse ( a , b ) ) ; } }


"Area of the circle that has a square and a circle inscribed in it | C # implementation of the approach ; Function to return the required area ; Calculate the area ; Driver code"
Solution in C#:

using System ; class GFG { static float getArea ( int a ) { float area = ( float ) ( Math . PI * a * a ) / 4 ; return area ; } public static void Main ( ) { int a = 3 ; Console . Write ( getArea ( a ) ) ; } }


"Area of the circumcircle of any triangles with sides given | C # Program to find the area the circumcircle of the given triangle ; Function to find the area of the circumcircle ; the sides cannot be negative ; semi - perimeter of the circle ; area of triangle ; area of the circle ; Driver code ; Get the sides of the triangle ; Find and print the area of the circumcircle"
Solution in C#:

using System ; class gfg { public double circlearea ( double a , double b , double c ) { if ( a < 0 b < 0 c < 0 ) return - 1 ; double p = ( a + b + c ) / 2 ; double At = Math . Sqrt ( p * ( p - a ) * ( p - b ) * ( p - c ) ) ; double A = 3.14 * Math . Pow ( ( ( a * b * c ) / ( 4 * At ) ) , 2 ) ; return A ; } } class geek { public static int Main ( ) { gfg g = new gfg ( ) ; double a = 4 , b = 5 , c = 3 ; Console . WriteLine ( g . circlearea ( a , b , c ) ) ; return 0 ; } }


"Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points | C # program for the above approach ; Function to return the area of the largest rectangle formed by lines parallel to X and Y axis from given set of points ; Initialize two arrays ; Store x and y coordinates ; Sort arrays ; Initialize max differences ; Find max adjacent differences ; Return answer ; Driver Code ; Given points ; Total points ; Function call"
Solution in C#:

using System ; class GFG { static int maxRectangle ( int [ , ] sequence , int size ) { int [ ] X_Cord = new int [ size ] ; int [ ] Y_Cord = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { X_Cord [ i ] = sequence [ i , 0 ] ; Y_Cord [ i ] = sequence [ i , 1 ] ; } Array . Sort ( X_Cord ) ; Array . Sort ( Y_Cord ) ; int X_Max = 0 , Y_Max = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { X_Max = Math . Max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] ) ; Y_Max = Math . Max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] ) ; } return X_Max * Y_Max ; } public static void Main ( String [ ] args ) { int [ , ] point = { { - 2 , 0 } , { 2 , 0 } , { 4 , 0 } , { 4 , 2 } } ; int n = point . GetLength ( 0 ) ; Console . Write ( maxRectangle ( point , n ) ) ; } }


"Area of the largest rectangle possible from given coordinates | C # program for the above approach ; Function to find the maximum possible area of a rectangle ; Initialize variables ; Sort array arr1 [ ] ; Sort array arr2 [ ] ; Traverse arr1 [ ] and arr2 [ ] ; If arr1 [ i ] is same as arr2 [ j ] ; If no starting point is found yet ; Update maximum end ; If arr [ i ] > arr2 [ j ] ; If no rectangle is found ; Return the area ; Driver code ; Given point ; Given length ; Given points ; Given length ; Function Call"
Solution in C#:

using System ; class GFG { static int largestArea ( int [ ] arr1 , int n , int [ ] arr2 , int m ) { int end = 0 , start = 0 , i = 0 , j = 0 ; Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; while ( i < n && j < m ) { if ( arr1 [ i ] == arr2 [ j ] ) { if ( start == 0 ) start = arr1 [ i ] ; else end = arr1 [ i ] ; i ++ ; j ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) j ++ ; else i ++ ; } if ( end == 0 start == 0 ) return 0 ; else return ( end - start ) ; } static void Main ( ) { int [ ] arr1 = { 1 , 2 , 4 } ; int N = arr1 . Length ; int [ ] arr2 = { 1 , 3 , 4 } ; int M = arr2 . Length ; Console . WriteLine ( largestArea ( arr1 , N , arr2 , M ) ) ; } }


"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"
Solution in C#:

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 ) ; } }


"Arithmetic Number | C # program to check if a number is arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver code"
Solution in C#:

using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] prime , bool [ ] primesquare , int [ ] a ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } static int countDivisors ( int n ) { if ( n == 1 ) return 1 ; bool [ ] prime = new bool [ n + 1 ] ; bool [ ] primesquare = new bool [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static bool checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } public static void Main ( String [ ] args ) { int n = 6 ; if ( checkArithmetic ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Arithmetic Progression containing X and Y with least possible first term | C # program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function call"
Solution in C#:

using System ; class GFG { static int minFirstTerm ( int X , int diff , int N ) { int first_term = int . MaxValue ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } static void printAP ( int N , int X , int Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } int maxDiff = Y - X ; int first_term = int . MaxValue , diff = 0 ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( first_term + " ▁ " ) ; first_term += diff ; } } public static void Main ( String [ ] args ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; } }


"Arithmetic Progression | C # program to check if a given array can form arithmetic progression ; Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkIsAP ( int [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } public static void Main ( ) { int [ ] arr = { 20 , 15 , 5 , 0 , 10 } ; int n = arr . Length ; if ( checkIsAP ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Armstrong Numbers between two integers | C # program to find Armstrong numbers in a range ; Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code"
Solution in C#:

using System ; class GFG { static void findArmstrong ( int low , int high ) { for ( int i = low + 1 ; i < high ; ++ i ) { int x = i ; int n = 0 ; while ( x != 0 ) { x /= 10 ; ++ n ; } int pow_sum = 0 ; x = i ; while ( x != 0 ) { int digit = x % 10 ; pow_sum += ( int ) Math . Pow ( digit , n ) ; x /= 10 ; } if ( pow_sum == i ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int num1 = 100 ; int num2 = 400 ; findArmstrong ( num1 , num2 ) ; Console . WriteLine ( ) ; } }


"Aronson 's Sequence | C # program to generate the first n terms of Aronson 's sequence ; Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; here len can be 3 or 4 ; last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; check if character is alphabet or not ; convert number to words in ordinal format and append ; Driver code"
Solution in C#:

using System ; class GFG { static String convert_to_words ( char [ ] num ) { int len = num . Length ; if ( len == 0 len > 4 ) { return " " ; } String [ ] single_digits_temp = { " " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String [ ] single_digits = { " " , " first " , " second " , " third " , " fourth " , " fifth " , " sixth " , " seventh " , " eighth " , " ninth " } ; String [ ] two_digits = { " " , " tenth " , " eleventh " , " twelfth " , " thirteenth " , " fourteenth " , " fifteenth " , " sixteenth " , " seventeenth " , " eighteenth " , " nineteenth " } ; String [ ] tens_multiple = { " " , " tenth " , " twentieth " , " thirtieth " , " fortieth " , " fiftieth " , " sixtieth " , " seventieth " , " eightieth " , " ninetieth " } ; String [ ] tens_power = { " hundred " , " thousand " } ; String word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] - '0' ] ; return word ; } int i = 0 , ctr = 0 ; String s = " ▁ " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != '0' ) { word += single_digits_temp [ num [ i ] - '0' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; Array . Copy ( num , 1 , num , 0 , num . Length - 1 ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word = word . Substring ( 0 , word . Length - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; else if ( num [ i ] == '1' ) word += s + two_digits [ num [ i + 1 ] - '0' + 1 ] ; else { if ( num [ i ] != '0' ) word += s + tens_multiple [ num [ i ] - '0' ] . Substring ( 0 , tens_multiple [ num [ i ] - '0' ] . Length - 4 ) + " y ▁ " ; else word += s ; word += single_digits [ num [ i + 1 ] - '0' ] ; } i += 2 ; } if ( i == len ) { if ( word [ 0 ] == ' ▁ ' ) word = word . Substring ( 1 , word . Length - 1 ) ; } } return word ; } static void Aronsons_sequence ( int n ) { String str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( char ....


"Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | C # implementation of the approach ; Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code"
Solution in C#:

using System ; class GFG { static void printArrangement ( int [ ] a , int n ) { Array . Sort ( a ) ; int [ ] b = new int [ n ] ; int low = 0 , high = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 1 , 4 , 4 , 3 , 2 } ; int n = a . Length ; printArrangement ( a , n ) ; } }


"Arrange a binary string to get maximum value within a range of indices | C # implementation of the approach ; Storing the count of 1 's in the String ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged String ; if after maximizing the ranges any 1 is left then we maximize the String lexicographically . ; Driver Code"
Solution in C#:

using System ; class GFG { static void arrange ( String s ) { int cc = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) cc ++ ; } int [ ] a = new int [ s . Length + 1 ] ; int [ , ] qq = { { 2 , 3 } , { 5 , 5 } } ; int n = qq . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = qq [ i , 0 ] , r = qq [ i , 1 ] ; l -- ; r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } int len_a = a . Length ; for ( int i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } int [ ] zz = new int [ s . Length ] ; for ( int i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( int i = 0 ; i < s . Length ; i ++ ) Console . Write ( zz [ i ] ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { String str = "11100" ; arrange ( str ) ; } }


"Arrange numbers to form a valid sequence | C # implementation of the approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int [ ] orgazineInOrder ( int [ ] vec , int [ ] op , int n ) { int [ ] result = new int [ n ] ; Array . Sort ( vec ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } public static void Main ( ) { int [ ] vec = { 8 , 2 , 7 , 1 , 5 , 9 } ; int [ ] op = { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ; int [ ] result = orgazineInOrder ( vec , op , vec . Length ) ; for ( int i = 0 ; i < result . Length ; i ++ ) { Console . Write ( result [ i ] + " ▁ " ) ; } } }


"Arrange the array such that upon performing given operations an increasing order is obtained | C # program to find the desired output after performing given operations ; Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; Insert last element at ith position ; Print desired Array ; Driver code ; Given Array"
Solution in C#:

using System ; class GFG { public static void Desired_Array ( int [ ] v ) { int n = v . Length ; Array . Sort ( v ) ; int i = n - 1 ; while ( i > 0 ) { int p = v [ n - 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { v [ j ] = v [ j - 1 ] ; } v [ i ] = p ; i -- ; } for ( int x = 0 ; x < v . Length ; x ++ ) { Console . Write ( v [ x ] + " ▁ " ) ; } Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] v = { 1 , 2 , 3 , 4 , 5 } ; Desired_Array ( v ) ; int [ ] v1 = { 1 , 12 , 2 , 10 , 4 , 16 , 6 } ; Desired_Array ( v1 ) ; } }


"Arrange the numbers in the Array as per given inequalities | C # program for the above approach ; Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the readonly integer ; Return the answer ; Driver Code ; Given List of ints ; Given list of inequalities ; Function call ; Print the output"
Solution in C#:

using System ; using System . Text ; class GFG { static String formAnInequality ( int [ ] integers , char [ ] inequalities ) { Array . Sort ( integers ) ; int lowerIndex = 0 ; int higherIndex = integers . Length - 1 ; StringBuilder sb = new StringBuilder ( ) ; foreach ( char ch in inequalities ) { if ( ch == ' < ' ) { sb . Append ( " ▁ " + integers [ lowerIndex ++ ] + " ▁ " + ch ) ; } else { sb . Append ( " ▁ " + integers [ higherIndex -- ] + " ▁ " + ch ) ; } } sb . Append ( " ▁ " + integers [ lowerIndex ] ) ; return sb . ToString ( ) ; } public static void Main ( String [ ] args ) { int [ ] integers = { 2 , 5 , 1 , 0 } ; char [ ] inequalities = { ' < ' , ' > ' , ' < ' } ; String output = formAnInequality ( integers , inequalities ) ; Console . WriteLine ( output ) ; } }


"Arrangement of the characters of a word such that all vowels are at odd places | C # program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } static int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countPermutations ( String str ) { int even = ( int ) Math . Floor ( ( double ) ( str . Length / 2 ) ) ; int odd = str . Length - even ; int ways = 0 ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ ( int ) ( str [ i ] - ' a ' ) ] ++ ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . Length - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } static void Main ( ) { String str = " geeks " ; Console . WriteLine ( countPermutations ( str ) ) ; } }


"Arrangement of words without changing the relative position of vowel and consonants | C # program for Arrangement of words without changing the relative position of vowel and consonants ; this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; Driver function ; string contains only capital letters ; this will contain ans"
Solution in C#:

using System ; class GFG { static long factorial ( long n ) { long res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } static long count ( string word ) { int [ ] freq = new int [ 27 ] ; for ( int i = 0 ; i < 27 ; i ++ ) freq [ i ] = 0 ; long vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < word . Length ; i ++ ) { freq [ word [ i ] - ' A ' ] ++ ; if ( word [ i ] == ' A ' word [ i ] == ' E ' word [ i ] == ' I ' word [ i ] == ' O ' word [ i ] == ' U ' ) { vowel ++ ; } else consonant ++ ; } long vowelArrange ; vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; long consonantArrange ; consonantArrange = factorial ( consonant ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } long total = vowelArrange * consonantArrange ; return total ; } public static void Main ( ) { string word = " COMPUTER " ; long ans = count ( word ) ; Console . WriteLine ( ans ) ; } }


"Array containing power of 2 whose XOR and Sum of elements equals X | C # implementation of the approach ; Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < long > getArray ( int n ) { List < long > ans = new List < long > ( ) ; long p2 = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) ans . Add ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } public static void Main ( String [ ] args ) { int n = 15 ; List < long > ans = getArray ( n ) ; foreach ( long i in ans ) Console . Write ( i + " ▁ " ) ; } }


"Array element moved by k using single moves | C # program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if it wins ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver program to test the above function"
Solution in C#:

using System ; class GFG { static int winner ( int [ ] a , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i == 1 ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; } public static void Main ( ) { int [ ] a = { 2 , 1 , 3 , 4 , 5 } ; int n = a . Length ; int k = 2 ; Console . WriteLine ( winner ( a , n , k ) ) ; } }


"Array element with minimum sum of absolute differences | C # implementation of the approach ; Function to return the minimized sum ; Sort the array ; Median of the array ; Calculate the minimized sum ; Return the required sum ; Driver code"
Solution in C#:

using System ; class GFG { static int minSum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int x = arr [ ( int ) ( n / 2 ) ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . Abs ( arr [ i ] - x ) ; return sum ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 9 , 3 , 6 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }


"Array element with minimum sum of absolute differences | Set 2 | C # program for the above approach ; Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumDiff ( int [ ] arr , int N ) { int res = arr [ 0 ] , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int min_diff = sum ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . Abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = Math . Abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } Console . Write ( res ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; minimumDiff ( arr , N ) ; } }


"Array formed using sum of absolute differences of that element with all other elements | C # program for the above approach ; Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { private static List < int > calculate ( int [ ] arr ) { int n = arr . Length ; List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) { sum += Math . Abs ( arr [ i ] - arr [ j ] ) ; } ans . Add ( sum ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; List < int > tmp = calculate ( arr ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { if ( i != tmp . Count - 1 ) { Console . Write ( tmp [ i ] + " , ▁ " ) ; } else { Console . Write ( tmp [ i ] ) ; } } Console . Write ( " ] " ) ; } }


"Array obtained by repeatedly reversing array after every insertion from given array | C # program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void generateArray ( int [ ] arr , int n ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . Insert ( 0 , arr [ i ] ) ; else ans . Add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { ans . Reverse ( ) ; } foreach ( int x in ans ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }


"Array range queries for elements with frequency same as value | C # Program to answer Q queries to find number of times an element x appears x times in a Query subarray ; Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int solveQuery ( int start , int end , int [ ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = start ; i <= end ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] ++ ; else mp . Add ( arr [ i ] , 1 ) ; } int count = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Key == entry . Value ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = A . Length ; int [ , ] queries = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i , 0 ] ; int end = queries [ i , 1 ] ; Console . WriteLine ( " Answer ▁ for ▁ Query ▁ " + ( i + 1 ) + " ▁ = ▁ " + solveQuery ( start , end , A ) ) ; } } }


"Array range queries for searching an element | C # program to determine if the element exists for different range queries ; Structure to represent a query range ; Find the root of the group containing the element at index x ; merge the two groups containing elements at indices x and y into one group ; make n subsets with every element as its root ; consecutive elements equal in value are merged into one single group ; Driver code ; check if the current element in consideration is equal to x or not if it is equal , then x exists in the range ; Print if x exists or not"
Solution in C#:

using System ; class GFG { public class Query { public int L , R , X ; public Query ( int L , int R , int X ) { this . L = L ; this . R = R ; this . X = X ; } } ; static int maxn = 100 ; static int [ ] root = new int [ maxn ] ; static int find ( int x ) { if ( x == root [ x ] ) return x ; else return root [ x ] = find ( root [ x ] ) ; } static void uni ( int x , int y ) { int p = find ( x ) , q = find ( y ) ; if ( p != q ) { root [ p ] = root [ q ] ; } } static void initialize ( int [ ] a , int n , Query [ ] q , int m ) { for ( int i = 0 ; i < n ; i ++ ) root [ i ] = i ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] == a [ i - 1 ] ) uni ( i , i - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 5 , 4 , 5 } ; int n = a . Length ; Query [ ] q = { new Query ( 0 , 2 , 2 ) , new Query ( 1 , 4 , 1 ) , new Query ( 2 , 4 , 5 ) } ; int m = q . Length ; initialize ( a , n , q , m ) ; for ( int i = 0 ; i < m ; i ++ ) { int flag = 0 ; int l = q [ i ] . L , r = q [ i ] . R , x = q [ i ] . X ; int p = r ; while ( p >= l ) { if ( a [ p ] == x ) { flag = 1 ; break ; } p = find ( p ) - 1 ; } if ( flag != 0 ) Console . WriteLine ( x + " ▁ exists ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; else Console . WriteLine ( x + " ▁ does ▁ not ▁ exist ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; } } }


"Array range queries over range queries | C # program to perform range queries over range queries . ; For prefix sum array ; This function is used to apply square root decomposition in the record array ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Function to print the resultant array ; Driver code ; If query is of type 2 then function call to record_func ; If query is of type 1 then simply add 1 to the record array ; Merging the value of the block in the record array ; If query is of type 1 then the array elements are over - written by the record array ; The prefix sum of the array ; Printing the resultant array"
Solution in C#:

using System ; public class GFG { static readonly int max = 10000 ; static void update ( int [ ] arr , int l ) { arr [ l ] += arr [ l - 1 ] ; } static void record_func ( int block_size , int [ ] block , int [ ] record , int l , int r , int value ) { while ( l < r && l % block_size != 0 && l != 0 ) { record [ l ] += value ; l ++ ; } while ( l + block_size <= r + 1 ) { block [ l / block_size ] += value ; l += block_size ; } while ( l <= r ) { record [ l ] += value ; l ++ ; } } static void print ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 5 , m = 5 ; int [ ] arr = new int [ n ] ; int [ ] record = new int [ m ] ; int block_size = ( int ) Math . Sqrt ( m ) ; int [ ] block = new int [ max ] ; int [ , ] command = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( command [ i , 0 ] == 2 ) { int x = i / ( block_size ) ; record_func ( block_size , block , record , command [ i , 1 ] - 1 , command [ i , 2 ] - 1 , ( block [ x ] + record [ i ] + 1 ) ) ; } else { record [ i ] ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { int check = ( i / block_size ) ; record [ i ] += block [ check ] ; } for ( int i = 0 ; i < m ; i ++ ) { if ( command [ i , 0 ] == 1 ) { arr [ command [ i , 1 ] - 1 ] += record [ i ] ; if ( ( command [ i , 2 ] - 1 ) < n - 1 ) { arr [ ( command [ i , 2 ] ) ] -= record [ i ] ; } } } for ( int i = 1 ; i < n ; i ++ ) { update ( arr , i ) ; } print ( arr , n ) ; } }


"Array range queries over range queries | C # program to perform range queries over range queries . ; Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array"
Solution in C#:

using System ; class GFG { static void record_sum ( int [ ] record , int l , int r , int n , int adder ) { for ( int i = l ; i <= r ; i ++ ) { record [ i ] += adder ; } } public static void Main ( String [ ] args ) { int n = 5 , m = 5 ; int [ ] arr = new int [ n ] ; int [ , ] query = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; int [ ] record = new int [ m ] ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i , 0 ] == 2 ) { record_sum ( record , query [ i , 1 ] - 1 , query [ i , 2 ] - 1 , m , record [ i ] + 1 ) ; } else { record_sum ( record , i , i , m , 1 ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( query [ i , 0 ] == 1 ) { record_sum ( arr , query [ i , 1 ] - 1 , query [ i , 2 ] - 1 , n , record [ i ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Array range queries over range queries | C # program to perform range queries over range queries . ; Function to execute type 1 query ; incrementing the array by 1 for type 1 queries ; Function to execute type 2 query ; If the query is of type 1 function call to type 1 query ; If the query is of type 2 recursive call to type 2 query ; Driver Code ; Input size of array amd number of queries ; Build query matrix ; Perform queries ; printing the result"
Solution in C#:

using System ; class GFG { static void type1 ( int [ ] arr , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) arr [ i ] ++ ; } static void type2 ( int [ ] arr , int [ , ] query , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) { if ( query [ i , 0 ] == 1 ) type1 ( arr , query [ i , 1 ] , query [ i , 2 ] ) ; else if ( query [ i , 0 ] == 2 ) type2 ( arr , query , query [ i , 1 ] , query [ i , 2 ] ) ; } } public static void Main ( ) { int n = 5 , m = 5 ; int [ ] arr = new int [ n + 1 ] ; int [ ] temp = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ , ] query = new int [ 6 , 4 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { query [ i , 0 ] = temp [ j ++ ] ; query [ i , 1 ] = temp [ j ++ ] ; query [ i , 2 ] = temp [ j ++ ] ; } for ( int i = 1 ; i <= m ; i ++ ) if ( query [ i , 0 ] == 1 ) type1 ( arr , query [ i , 1 ] , query [ i , 2 ] ) ; else if ( query [ i , 0 ] == 2 ) type2 ( arr , query , query [ i , 1 ] , query [ i , 2 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } }


"Array range queries over range queries | C # program to perform range queries over range queries . ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add element of BITree to sum ; Move index to parent node in getSum View ; Function to update the BITree ; Driver code ; BITree for query of type 2 ; BITree for query of type 1 ; If query is of type 2 then function call to update with BITree ; If query is of type 1 then function call to update with BITree2"
Solution in C#:

using System ; class GFG { static void updateBIT ( int [ ] BITree , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] = ( val + BITree [ index ] ) ; index = ( index + ( index & ( - index ) ) ) ; } return ; } static int [ ] constructBITree ( int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; return BITree ; } static int getSum ( int [ ] BITree , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum = ( sum + BITree [ index ] ) ; index -= index & ( - index ) ; } return sum ; } static void update ( int [ ] BITree , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; return ; } static public void Main ( ) { int n = 5 , m = 5 ; int [ ] temp = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ , ] q = new int [ 6 , 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i , 0 ] = temp [ j ++ ] ; q [ i , 1 ] = temp [ j ++ ] ; q [ i , 2 ] = temp [ j ++ ] ; } int [ ] BITree = constructBITree ( m ) ; int [ ] BITree2 = constructBITree ( n ) ; for ( int i = m ; i >= 1 ; i -- ) if ( q [ i , 0 ] == 2 ) update ( BITree , q [ i , 1 ] - 1 , q [ i , 2 ] - 1 , m , 1 ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i , 0 ] == 2 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree , q [ i , 1 ] - 1 , q [ i , 2 ] - 1 , m , val ) ; } } for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i , 0 ] == 1 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree2 , q [ i , 1 ] - 1 , q [ i , 2 ] - 1 , n , ( val + 1 ) ) ; } } for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( getSum ( BITree2 , i - 1 ) + " ▁ " ) ; } }


"Array range queries to find the number of perfect square elements with updates | C # program to find number of perfect square numbers in a subarray and performing updates ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; A utility function to get the middle index from corner indexes . ; where st - . Pointer to segment tree index - . Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se - . Starting and ending indexes of the segment represented by current node i . e . st [ index ] qs & qe - . Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the number of perfect square numbers in the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; where st , si , ss & se are same as getSumUtil ( ) i - . index of the element to be updated . This index is in input array . diff - . Value to be added to all nodes which have i in range ; Base Case : If the input index lies outside the range of this segment ; If the input index is in range of this node , then update the value of the node and its children ; Function to update a value in the input array and segment tree . It uses updateValueUtil ( ) to update the value in segment tree ; Check for erroneous input index ; Update the value in array ; Case 1 : Old and new values both are perfect square numbers ; Case 2 : Old and new values both not perfect square numbers ; Case 3 : Old value was perfect square , new value is not a perfect square ; Case 4 : Old value was non - perfect square , new_val is perfect square ; Update values of nodes in segment tree ; Return no . of perfect square numbers in range from index qs ( query start ) to qe ( query end ) . It mainly uses queryUtil ( ) ; Recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is..."
Solution in C#:

 using System ; class GFG { static readonly int MAX = 1000 ; static bool isPerfectSquare ( int x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ? true : false ; } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int [ ] arr , int [ ] st , int n , int i , int new_val ) { if ( i < 0 i > n - 1 ) { Console . Write ( " Invalid ▁ Input " ) ; return ; } int diff = 0 , oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPerfectSquare ( oldValue ) && isPerfectSquare ( new_val ) ) return ; if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) return ; if ( isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = - 1 ; } if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void query ( int [ ] st , int n , int qs , int qe ) { int perfectSquareInRange = queryUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; Console . Write ( perfectSquareInRange + "
 " ) ; } static int constructSTUtil ( int [ ] arr , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( isPerfectSquare ( arr [ ss ] ) ) st [ si ] = 1 ; else st [ si ] = 0 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid...


"Array sum after replacing all occurrences of X by Y for Q queries | C # implementation to find the sum of the array for the given Q queries ; Function that print the sum of the array for Q queries ; Stores the frequencies of array elements Create an empty hash map ; Calculate the sum of the initial array and store the frequency of each element in map ; Iterate for all the queries ; Store query values ; Decrement the sum accordingly ; Increment the sum accordingly ; Set count of Y [ i ] ; Reset count of X [ i ] ; Print the sum ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void sumOfTheArrayForQuery ( int [ ] A , int N , int [ ] X , int [ ] Y , int Q ) { int sum = 0 ; Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . ContainsKey ( A [ i ] ) ) { count [ A [ i ] ] = count [ A [ i ] ] + 1 ; } else { count . Add ( A [ i ] , 1 ) ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = X [ i ] , y = Y [ i ] ; if ( count . ContainsKey ( X [ i ] ) ) { sum -= count [ X [ i ] ] * X [ i ] ; sum += count [ X [ i ] ] * Y [ i ] ; } if ( count . ContainsKey ( Y [ i ] ) && count . ContainsKey ( X [ i ] ) ) { count [ Y [ i ] ] = count [ Y [ i ] ] + count [ X [ i ] ] ; } if ( count . ContainsKey ( X [ i ] ) ) { count [ X [ i ] ] = 0 ; } Console . Write ( sum + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 3 , 2 } ; int [ ] X = { 2 , 3 , 5 } ; int [ ] Y = { 3 , 1 , 2 } ; int N = arr . Length ; int Q = X . Length ; sumOfTheArrayForQuery ( arr , N , X , Y , Q ) ; } }


"Array value by repeatedly replacing max 2 elements with their absolute difference | C # program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; Function that return last value of array ; Build a binary max_heap ; For max 2 elements ; Iterate until queue is not empty ; If only 1 element is left ; Return the last remaining value ; Check that difference is non zero ; Finally return 0 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int lastElement ( int [ ] arr ) { Queue < int > pq = new Queue < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) pq . Enqueue ( arr [ i ] ) ; int m1 , m2 ; while ( pq . Contains ( 0 ) ) { if ( pq . Count == 1 ) { return pq . Peek ( ) ; } m1 = pq . Dequeue ( ) ; m2 = pq . Peek ( ) ; if ( m1 != m2 ) pq . Enqueue ( m1 - m2 ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 4 , 1 , 8 , 1 , 1 } ; Console . WriteLine ( lastElement ( arr ) ) ; } }


"Array with GCD of any of its subset belongs to the given array | C # implementation to generate the required array ; Function to return gcd of a and b ; Function to find gcd of array of numbers ; Function to generate the array with required constraints . ; computing GCD of the given set ; Solution exists if GCD of array is equal to the minimum element of the array ; Printing the built array ; Driver Code ; Taking in the input and initializing the set STL set in cpp has a property that it maintains the elements in sorted order , thus we do not need to sort them externally ; Calling the computing function ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( List < int > arr , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) result = gcd ( arr [ i ] , result ) ; return result ; } static void compute ( List < int > arr , int n ) { List < int > answer = new List < int > ( ) ; int GCD_of_array = findGCD ( arr , n ) ; if ( GCD_of_array == arr [ 0 ] ) { answer . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { answer . Add ( arr [ 0 ] ) ; answer . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < answer . Count ; i ++ ) Console . Write ( answer [ i ] + " ▁ " ) ; } else Console . Write ( " No ▁ array ▁ " + " can ▁ be ▁ build " ) ; } static void Main ( ) { int n = 3 ; int [ ] input = new int [ ] { 2 , 5 , 6 , 7 , 11 } ; HashSet < int > GCD = new HashSet < int > ( input ) ; List < int > arr = new List < int > ( ) ; foreach ( int b in GCD ) arr . Add ( b ) ; compute ( arr , n ) ; } }


"Aspiring Number | C # implementation to check whether a number is aspiring or not ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; checking condition for aspiring ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static int getAliquot ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; s . Add ( n ) ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . Contains ( n ) ) { return n ; } s . Add ( n ) ; } return 0 ; } static bool isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { sum = sum + i + n / i ; } } if ( sum == n && n != 1 ) { return true ; } return false ; } static bool isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) { return true ; } else { return false ; } } public static void Main ( String [ ] args ) { int n = 25 ; if ( isAspiring ( n ) ) { Console . WriteLine ( " Aspiring " ) ; } else { Console . WriteLine ( " Not ▁ Aspiring " ) ; } } }


"Assembly Line Scheduling | DP | A C # program to find minimum possible time by the car chassis to complete ; Utility function to find minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and retutn minimum ; Driver code"
Solution in C#:

using System ; class GFG { static int NUM_STATION = 4 ; static int min ( int a , int b ) { return a < b ? a : b ; } static int carAssembly ( int [ , ] a , int [ , ] t , int [ ] e , int [ ] x ) { int [ ] T1 = new int [ NUM_STATION ] ; int [ ] T2 = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 , 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 , 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 , i ] , T2 [ i - 1 ] + t [ 1 , i ] + a [ 0 , i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 , i ] , T1 [ i - 1 ] + t [ 0 , i ] + a [ 1 , i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } public static void Main ( ) { int [ , ] a = { { 4 , 5 , 3 , 2 } , { 2 , 10 , 1 , 4 } } ; int [ , ] t = { { 0 , 7 , 4 , 5 } , { 0 , 9 , 2 , 8 } } ; int [ ] e = { 10 , 12 } ; int [ ] x = { 18 , 7 } ; Console . Write ( carAssembly ( a , t , e , x ) ) ; } }


"Assign other value to a variable from two possible values | C # program to change value of x according to its current value . ; Function to alternate the values ; Main function"
Solution in C#:

 using System ; public class GFG { static int alternate ( int a , int b , int x ) { return x = a ^ b ^ x ; } public static void Main ( ) { int a = - 10 ; int b = 15 ; int x = a ; Console . Write ( " x ▁ is ▁ : ▁ " + x ) ; x = alternate ( a , b , x ) ; Console . Write ( "
 After ▁ exchange ▁ " ) ; Console . Write ( " x is : " } }


using System ; class gfg { public void alternate ( ref int a , ref int b , ref int x ) { x = a + b - x ; } } class geek { public static int Main ( ) { gfg g = new gfg ( ) ; int a = - 10 ; int b = 15 ; int x = a ; Console . WriteLine ( " x ▁ is ▁ : ▁ { 0 } " , x ) ; g . alternate ( ref a , ref b , ref x ) ; Console . WriteLine ( " After ▁ change ▁ " ) ; Console . WriteLine ( " x ▁ is ▁ : ▁ { 0 } " , x ) ; return 0 ; } }


"Astonishing Numbers | C # implementation for the above approach ; Function to concatenate two integers into one ; Convert both the integers to String ; Concatenate both Strings ; Convert the concatenated String to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for Astonishing number ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static int concat ( int a , int b ) { String s1 = a . ToString ( ) ; String s2 = b . ToString ( ) ; String s = s1 + s2 ; int c = Int32 . Parse ( s ) ; return c ; } static bool isAstonishing ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += j ; if ( sum == n ) { int concatenation = concat ( i , j ) ; if ( concatenation == n ) { return true ; } } } } return false ; } public static void Main ( String [ ] args ) { int n = 429 ; if ( isAstonishing ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Auxiliary Space with Recursive Functions |"
Solution in C#:

static int sum ( int sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }


"Average of ASCII values of characters of a given string | C # code to find average of ASCII characters ; Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code"
Solution in C#:

using System ; class GFG { public static int averageValue ( String s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { sum_char += ( int ) s [ i ] ; } return sum_char / s . Length ; } public static void Main ( ) { String s = " GeeksforGeeks " ; Console . Write ( averageValue ( s ) ) ; } }


"Average of Cubes of first N natural numbers | C # program for the above approach ; Function to find average of cubes ; Storing sum of cubes of numbers in sum ; Calculate sum of cubes ; Return average ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static double findAverageOfCube ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } public static void Main ( ) { int n = 3 ; Console . Write ( findAverageOfCube ( n ) ) ; } }


"Average of Squares of Natural Numbers | C # program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of square number ; Driver code"
Solution in C#:

using System ; public class GFG { static float AvgofSquareN ( int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } static public void Main ( String [ ] args ) { int n = 2 ; Console . WriteLine ( AvgofSquareN ( n ) ) ; } }


"Average of Squares of Natural Numbers | C # program to get the Average of squareof first n natural numbers ; Function to calculate average ; Driver Code"
Solution in C#:

using System ; public class GFG { static float AvgofSquareN ( int n ) { return ( float ) ( ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } static public void Main ( String [ ] args ) { int n = 2 ; Console . WriteLine ( AvgofSquareN ( n ) ) ; } }


"Average of first n even natural numbers | C # Program to find the average of sum of first n even numbers ; Return the average of sum of first n even numbers ; driver code"
Solution in C#:

using System ; class GFG { static int avg_of_even_num ( int n ) { return n + 1 ; } public static void Main ( ) { int n = 8 ; Console . Write ( avg_of_even_num ( n ) ) ; } }


"Average of first n even natural numbers | C # implementation to find Average of sum of first n natural even number ; function to find average of sum of first n even numbers ; sum of first n even numbers ; calculating Average ; driver code"
Solution in C#:

using System ; class GFG { static int avg_of_even_num ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 2 * i ; return ( sum / n ) ; } public static void Main ( ) { int n = 9 ; Console . Write ( avg_of_even_num ( n ) ) ; } }


"Average of remaining elements after removing K largest and K smallest elements from array | C # implementation of the above approach ; Function to find average ; base case if 2 * k >= n means all element get removed ; first sort all elements ; sum of req number ; find average ; Driver code"
Solution in C#:

using System ; public class GFG { static double average ( int [ ] arr , int n , int k ) { double total = 0 ; if ( 2 * k >= n ) return 0 ; Array . Sort ( arr ) ; int start = k , end = n - k - 1 ; for ( int i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 4 , 5 , 6 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( average ( arr , n , k ) ) ; } }


"Average value of set bit count in given Binary string after performing all possible choices of K operations | C # program for the above approach ; Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array [ ] arr and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static double averageSetBits ( int N , int K , int [ ] arr ) { double p = N ; double q = 0 ; for ( int i = 0 ; i < K ; i ++ ) { double _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 1 , 2 , 3 } ; int K = arr . Length ; Console . WriteLine ( String . Format ( " { 0 : F10 } " , averageSetBits ( N , K , arr ) ) ) ; } }


"Bakhshali Approximation for computing square roots | C # program gives result approximated to 5 decimal places . ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver Code"
Solution in C#:

using System ; class GFG { static float sqroot ( float s ) { int pSq = 0 ; int N = 0 ; for ( int i = ( int ) ( s ) ; i > 0 ; i -- ) { for ( int j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } float d = s - pSq ; float P = d / ( 2.0f * N ) ; float A = N + P ; float sqrt_of_s = A - ( ( P * P ) / ( 2.0f * A ) ) ; return sqrt_of_s ; } public static void Main ( ) { float num = 9.2345f ; float sqroot_of_num = sqroot ( num ) ; Console . Write ( " Square ▁ root ▁ of ▁ " + num + " ▁ = ▁ " + Math . Round ( sqroot_of_num * 100000.0 ) / 100000.0 ) ; } }


"Balance a string after removing extra brackets | C # implementation of above approach ; Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str [ i ] and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str [ i ] not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver Code"
Solution in C#:

using System ; class GFG { public static void balancedString ( String str ) { int count = 0 , i ; int n = str . Length ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) { Console . Write ( str [ i ] ) ; count ++ ; } else if ( str [ i ] == ' ) ' && count != 0 ) { Console . Write ( str [ i ] ) ; count -- ; } else if ( str [ i ] != ' ) ' ) Console . Write ( str [ i ] ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) Console . Write ( " ) " ) ; } public static void Main ( ) { String str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; } }


"Balance pans using given weights that are powers of a number | C # code to check whether scale can be balanced or not ; method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a  in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isBalancePossible ( int T , int a ) { List < int > baseForm = new List < int > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . Add ( T % a ) ; T /= a ; s ++ ; } baseForm . Add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm [ i ] != 0 && baseForm [ i ] != 1 && baseForm [ i ] != ( a - 1 ) && baseForm [ i ] != a ) { return false ; } if ( baseForm [ i ] == a || baseForm [ i ] == ( a - 1 ) ) { baseForm . Insert ( i + 1 , baseForm [ i + 1 ] + 1 ) ; } } return true ; } public static void Main ( ) { int T = 11 ; int a = 4 ; bool balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { Console . WriteLine ( " Balance ▁ is ▁ possible " ) ; } else { Console . WriteLine ( " Balance ▁ is ▁ not ▁ possible " ) ; } } }


"Balanced Prime | C # Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int MAX = 501 ; public static int balancedprime ( int n ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int k = 0 ; k < MAX + 1 ; k ++ ) prime [ k ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } List < int > v = new List < int > ( ) ; for ( int p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . Add ( p ) ; int c = 0 ; for ( int i = 1 ; i < v . Count - 1 ; i ++ ) { if ( ( int ) v [ i ] == ( int ) ( v [ i + 1 ] + v [ i - 1 ] ) / 2 ) c ++ ; if ( c == n ) return ( int ) v [ i ] ; } return 1 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( balancedprime ( n ) ) ; } }


"Balanced Ternary Number System | C # program to convert positive decimals into balanced ternary system ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String balancedTernary ( int n ) { String output = " " ; while ( n > 0 ) { int rem = n % 3 ; n = n / 3 ; if ( rem == 2 ) { rem = - 1 ; n ++ ; } output = ( rem == 0 ? '0' : ( rem == 1 ) ? '1' : ' Z ' ) + output ; } return output ; } public static void Main ( String [ ] args ) { int n = 238 ; Console . Write ( " Equivalent ▁ Balanced ▁ Ternary ▁ of ▁ " + n + " ▁ is : ▁ " + balancedTernary ( n ) ) ; } }


"Balanced expressions such that given positions have opening brackets | C # code to find number of ways of arranging bracket with proper expressions ; function to calculate the number of proper bracket sequence ; hash array to mark the positions of opening brackets ; dp 2d array ; mark positions in hash array ; first position marked as 1 ; iterate and formulate the recurrences ; if position has a opening bracket ; return answer ; driver code ; positions where opening braces will be placed"
Solution in C#:

using System ; class GFG { static int N = 1000 ; public static long arrangeBraces ( int n , int [ ] pos , int k ) { bool [ ] h = new bool [ N ] ; int [ , ] dp = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) h [ i ] = false ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < k ; i ++ ) h [ pos [ i ] ] = true ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= 2 * n ; j ++ ) { if ( h [ i ] ) { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 0 ; } else { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 1 , j + 1 ] ; else dp [ i , j ] = dp [ i - 1 , j + 1 ] ; } } } return dp [ 2 * n , 0 ] ; } static void Main ( ) { int n = 3 ; int [ ] pos = new int [ ] { 2 } ; int k = pos . Length ; Console . Write ( arrangeBraces ( n , pos , k ) ) ; } }


"Balanced expressions such that given positions have opening brackets | Set 2 | C # implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; Driver code ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer"
Solution in C#:

using System ; class GFG { static readonly int N = 1000 ; static int find ( int index , int openbrk , int n , int [ , ] dp , int [ ] adj ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index , openbrk ] != - 1 ) { return dp [ index , openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index , openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index , openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index , openbrk ] ; } public static void Main ( ) { int [ , ] dp = new int [ N , N ] ; int n = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i , j ] = - 1 ; } } int [ ] adj = { 1 , 0 , 0 , 0 } ; Console . WriteLine ( find ( 0 , 0 , 2 * n , dp , adj ) ) ; } }


"Beatty sequence | C # implementation of the above approach ; Function to print the first N terms of the Beatty sequence ; Driver code"
Solution in C#:

using System ; class GFG { static void BeattySequence ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { double ans = Math . Floor ( i * Math . Sqrt ( 2 ) ) ; Console . Write ( ans + " , ▁ " ) ; } } public static void Main ( ) { int n = 5 ; BeattySequence ( n ) ; } }


"Bell Numbers ( Number of ways to Partition a Set ) | C # program to find n 'th Bell number ; Function to find n 'th  Bell Number ; Explicitly fill for j = 0 ; Fill for remaining values of j ; Driver Code"
Solution in C#:

using System ; class GFG { static int bellNumber ( int n ) { int [ , ] bell = new int [ n + 1 , n + 1 ] ; bell [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { bell [ i , 0 ] = bell [ i - 1 , i - 1 ] ; for ( int j = 1 ; j <= i ; j ++ ) bell [ i , j ] = bell [ i - 1 , j - 1 ] + bell [ i , j - 1 ] ; } return bell [ n , 0 ] ; } public static void Main ( ) { for ( int n = 0 ; n <= 5 ; n ++ ) Console . WriteLine ( " Bell ▁ Number ▁ " + n + " ▁ is ▁ " + bellNumber ( n ) ) ; } }