codeparrot/xlcost-text-to-code huggingface dataset:
Make largest palindrome by changing at most K | C # program to get largest palindrome changing atmost K digits ; Returns maximum possible palindrome using k changes ; Initialize l and r by leftmost and rightmost ends ; First try to make String palindrome ; Replace left and right character by maximum of both ; If k is negative then we can 't make  String palindrome ; At mid character , if K > 0 then change it to 9 ; If character at lth ( same as rth ) is less than 9 ; If none of them is changed in the previous loop then subtract 2 from K and convert both to 9 ; If one of them is changed in the previous loop then subtract 1 from K ( 1 more is subtracted already ) and make them 9 ; Driver code to test above methods
using System ; public class GFG { static String maximumPalinUsingKChanges ( String str , int k ) { char [ ] palin = str . ToCharArray ( ) ; String ans = " " ; int l = 0 ; int r = str . Length - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { palin [ l ] = palin [ r ] = ( char ) Math . Max ( str [ l ] , str [ r ] ) ; k -- ; } l ++ ; r -- ; } if ( k < 0 ) { return " Not ▁ possible " ; } l = 0 ; r = str . Length - 1 ; while ( l <= r ) { if ( l == r ) { if ( k > 0 ) { palin [ l ] = '9' ; } } if ( palin [ l ] < '9' ) { if ( k >= 2 && palin [ l ] == str [ l ] && palin [ r ] == str [ r ] ) { k -= 2 ; palin [ l ] = palin [ r ] = '9' ; } else if ( k >= 1 && ( palin [ l ] != str [ l ] palin [ r ] != str [ r ] ) ) { k -- ; palin [ l ] = palin [ r ] = '9' ; } } l ++ ; r -- ; } for ( int i = 0 ; i < palin . Length ; i ++ ) ans += palin [ i ] ; return ans ; } public static void Main ( ) { String str = "43435" ; int k = 3 ; Console . Write ( maximumPalinUsingKChanges ( str , k ) ) ; } }
Minimize the count of adjacent pairs with different parity | C # implementation of above approach ; Recursive function to calculate minimum adjacent pairs with different parity ; If all the numbers are placed ; If replacement is not required ; If replacement is required ; Backtracking ; Backtracking ; Function to display the minimum number of adjacent elements with different parity ; Store no of even numbers not present in the array ; Store no of odd numbers not present in the array ; Erase exisiting numbers ; Store non - exisiting even and odd numbers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int min ; static void parity ( List < int > even , List < int > odd , List < int > v , int i ) { if ( i == v . Count even . Count == 0 && odd . Count == 0 ) { int count = 0 ; for ( int j = 0 ; j < v . Count - 1 ; j ++ ) { if ( v [ j ] % 2 != v [ j + 1 ] % 2 ) count ++ ; } if ( count < min ) min = count ; return ; } if ( v [ i ] != - 1 ) parity ( even , odd , v , i + 1 ) ; else { if ( even . Count != 0 ) { int x = even [ even . Count - 1 ] ; even . RemoveAt ( even . Count - 1 ) ; v [ i ] = x ; parity ( even , odd , v , i + 1 ) ; even . Add ( x ) ; } if ( odd . Count != 0 ) { int x = odd [ odd . Count - 1 ] ; odd . RemoveAt ( odd . Count - 1 ) ; v [ i ] = x ; parity ( even , odd , v , i + 1 ) ; odd . Add ( x ) ; } } } static void minDiffParity ( List < int > v , int n ) { List < int > even = new List < int > ( ) ; List < int > odd = new List < int > ( ) ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( m . ContainsKey ( i ) ) { m [ i ] = 1 ; } else { m . Add ( i , 1 ) ; } } for ( int i = 0 ; i < v . Count ; i ++ ) { if ( v [ i ] != - 1 ) m . Remove ( v [ i ] ) ; } foreach ( KeyValuePair < int , int > i in m ) { if ( i . Key % 2 == 0 ) { even . Add ( i . Key ) ; } else { odd . Add ( i . Key ) ; } } min = 1000 ; parity ( even , odd , v , 0 ) ; Console . WriteLine ( min ) ; } static void Main ( ) { int n = 8 ; List < int > v = new List < int > ( ) ; v . Add ( 2 ) ; v . Add ( 1 ) ; v . Add ( 4 ) ; v . Add ( - 1 ) ; v . Add ( - 1 ) ; v . Add ( 6 ) ; v . Add ( - 1 ) ; v . Add ( 8 ) ; minDiffParity ( v , n ) ; } }
Find triplet such that number of nodes connecting these triplets is maximum | C # implementation of the approach ; To store the required nodes ; Parent array to retrace the nodes ; Visited array to prevent DFS in direction on Diameter path ; DFS function to find the startnode ; DFS function to find the endnode of diameter and maintain the parent array ; DFS function to find the end node of the Longest Branch to Diameter ; Function to find the required nodes ; To find start node of diameter ; To find end node of diameter ; x is the end node of diameter ; Mark all the nodes on diameter using back tracking ; Find the end node of longest branch to diameter ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static List < List < int > > adjacent = new List < List < int > > ( ) ; static bool [ ] visited = new bool [ MAX ] ; static int startnode , endnode , thirdnode ; static int maxi = - 1 , N ; static int [ ] parent = new int [ MAX ] ; static bool [ ] vis = new bool [ MAX ] ; static void dfs ( int u , int count ) { visited [ u ] = true ; int temp = 0 ; for ( int i = 0 ; i < adjacent [ u ] . Count ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] ) { temp ++ ; dfs ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; startnode = u ; } } } static void dfs1 ( int u , int count ) { visited [ u ] = true ; int temp = 0 ; for ( int i = 0 ; i < adjacent [ u ] . Count ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] ) { temp ++ ; parent [ adjacent [ u ] [ i ] ] = u ; dfs1 ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; endnode = u ; } } } static void dfs2 ( int u , int count ) { visited [ u ] = true ; int temp = 0 ; for ( int i = 0 ; i < adjacent [ u ] . Count ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] && ! vis [ adjacent [ u ] [ i ] ] ) { temp ++ ; dfs2 ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; thirdnode = u ; } } } static void findNodes ( ) { dfs ( 1 , 0 ) ; for ( int i = 0 ; i <= N ; i ++ ) visited [ i ] = false ; maxi = - 1 ; dfs1 ( startnode , 0 ) ; for ( int i = 0 ; i <= N ; i ++ ) visited [ i ] = false ; int x = endnode ; vis [ startnode ] = true ; while ( x != startnode ) { vis [ x ] = true ; x = parent [ x ] ; } maxi = - 1 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] ) dfs2 ( i , 0 ) ; } } static void Main ( ) { for ( int i = 0 ; i < MAX ; i ++ ) adjacent . Add ( new List < int > ( ) ) ; N = 4 ; adjacent [ 1 ] . Add ( 2 ) ; adjacent [ 2 ] . Add ( 1 ) ; adjacent [ 1 ] . Add ( 3 ) ; adjacent [ 3 ] . Add ( 1 ) ; adjacent [ 1 ] . Add ( 4 ) ; adjacent [ 4 ] . Add ( 1 ) ; findNodes ( ) ; Console . WriteLine ( " ( " + startnode + " , ▁ " + endnode + " , ▁ " + thirdnode + " ) " ) ; } }
Percentage increase in volume of the sphere if radius is increased by a given percentage | C # program to find percentage increase in the volume of the sphere if radius is increased by a given percentage ; Driver code
using System ; class GFG { static void newvol ( double x ) { Console . WriteLine ( " percentage ▁ increase ▁ in ▁ the " + " ▁ volume ▁ of ▁ the ▁ sphere ▁ is ▁ " + ( Math . Pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . Pow ( x , 2 ) ) / 100 ) + " % " ) ; } public static void Main ( ) { double x = 10 ; newvol ( x ) ; } }
Length of the chord of the circle whose radius and the angle subtended at the center by the chord is given | C # program to find the length chord of the circle whose radius and the angle subtended at the centre is also given ; Function to find the length of the chord ; Driver code
using System ; class GFG { static void length_of_chord ( double r , double x ) { Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ chord " + " ▁ of ▁ the ▁ circle ▁ is ▁ " + 2 * r * Math . Sin ( x * ( 3.14 / 180 ) ) ) ; } public static void Main ( String [ ] args ) { double r = 4 , x = 63 ; length_of_chord ( r , x ) ; } }
Area of a square inscribed in a circle which is inscribed in an equilateral triangle | C # Program to find the area of the square inscribed within the circle which in turn is inscribed in an equilateral triangle ; Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code
using System ; class GFG { static float area ( float a ) { if ( a < 0 ) return - 1 ; float area = ( float ) Math . Sqrt ( a ) / 6 ; return area ; } public static void Main ( ) { float a = 10 ; Console . WriteLine ( area ( a ) ) ; } }
Length of longest rod that can fit into a cuboid | C # program to find the longest rod that can fit in a cuboid ; Function to find the length ; temporary variable to hold the intermediate result ; length of longest rod is calculated using square root function ; Driver Code ; calling longestRodInCuboid ( ) function to get the length of longest rod
using System ; class GFG { static double longestRodInCuboid ( int length , int breadth , int height ) { double result ; int temp ; temp = length * length + breadth * breadth + height * height ; result = Math . Sqrt ( temp ) ; return result ; } public static void Main ( ) { int length = 12 , breadth = 9 , height = 8 ; Console . WriteLine ( ( int ) longestRodInCuboid ( length , breadth , height ) ) ; } }
Check whether a given point lies on or inside the rectangle | Set 3 | C # program to Check whether a given point lies inside or on the rectangle or not ; function to Check whether a given point lies inside or on the rectangle or not ; Driver code
using System ; class GFG { static bool LiesInsieRectangle ( int a , int b , int x , int y ) { if ( x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0 ) return true ; return false ; } public static void Main ( ) { int a = 7 , b = 2 , x = 4 , y = 5 ; if ( LiesInsieRectangle ( a , b , x , y ) ) Console . Write ( " Given ▁ point ▁ lies ▁ " + " inside ▁ the ▁ rectangle " ) ; else Console . Write ( " Given ▁ point ▁ does ▁ not ▁ " + " lie ▁ on ▁ the ▁ rectangle " ) ; } }
Maximize volume of cuboid with given sum of sides | C # code to Maximize volume of cuboid with given sum of sides ; Return the maximum volume . ; for length ; for breadth ; for height ; calculating maximum volume . ; Driver function
using System ; class GFG { static int maxvolume ( int s ) { int maxvalue = 0 ; for ( int i = 1 ; i <= s - 2 ; i ++ ) { for ( int j = 1 ; j <= s - 1 ; j ++ ) { int k = s - i - j ; maxvalue = Math . Max ( maxvalue , i * j * k ) ; } } return maxvalue ; } public static void Main ( ) { int s = 8 ; Console . WriteLine ( maxvolume ( s ) ) ; } }
Maximize volume of cuboid with given sum of sides | C # code to Maximize volume of cuboid with given sum of sides ; Return the maximum volume . ; finding length ; finding breadth ; finding height ; Driven Program
using System ; class GFG { static int maxvolume ( int s ) { int length = s / 3 ; s -= length ; int breadth = s / 2 ; int height = s - breadth ; return length * breadth * height ; } public static void Main ( ) { int s = 8 ; Console . WriteLine ( maxvolume ( s ) ) ; } }
Area of a Hexagon | C # program to find area of a Hexagon ; Create a function for calculating the area of the hexagon . ; Driver Code ; Length of a side
using System ; class GFG { public static double hexagonArea ( double s ) { return ( ( 3 * Math . Sqrt ( 3 ) * ( s * s ) ) / 2 ) ; } public static void Main ( ) { double s = 4 ; Console . WriteLine ( " Area : ▁ " + hexagonArea ( s ) ) ; } }
Maximum number of squares that can fit in a right angle isosceles triangle | C # program for finding maximum squares that can fit in right angle isosceles triangle ; function for finding max squares ; return in O ( 1 ) with derived formula ; driver program
using System ; public class GFG { static int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; } public static void Main ( ) { int b = 10 , m = 2 ; Console . WriteLine ( maxSquare ( b , m ) ) ; } }
Check if right triangle possible from given area and hypotenuse | C # program to check existence of right triangle . ; Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1. ; Descriminant of the equation ; applying the linear equation formula to find both the roots ; Driver code
using System ; class GFG { static void findRightAngle ( double A , double H ) { double D = Math . Pow ( H , 4 ) - 16 * A * A ; if ( D >= 0 ) { double root1 = ( H * H + Math . Sqrt ( D ) ) / 2 ; double root2 = ( H * H - Math . Sqrt ( D ) ) / 2 ; double a = Math . Sqrt ( root1 ) ; double b = Math . Sqrt ( root2 ) ; if ( b >= a ) Console . WriteLine ( a + " ▁ " + b + " ▁ " + H ) ; else Console . WriteLine ( b + " ▁ " + a + " ▁ " + H ) ; } else Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { findRightAngle ( 6 , 5 ) ; } }
Maximum number of 2 x2 squares that can be fit inside a right isosceles triangle | C # program to count number of 2 x 2 squares in a right isosceles triangle ; removing the extra part we would always need ; Since each square has base of length of 2 ; Driver code
using System ; class GFG { public static int numberOfSquares ( int _base ) { _base = ( _base - 2 ) ; _base = _base / 2 ; return _base * ( _base + 1 ) / 2 ; } public static void Main ( ) { int _base = 8 ; Console . WriteLine ( numberOfSquares ( _base ) ) ; } }
Bitwise OR of bitwise AND of all possible non | C # program for the above approach ; Function to find the Bitwise OR of Bitwise AND of all possible subarrays after performing the every query ; Traversing each pair of the query ; Stores the Bitwise OR ; Updating the array ; Find the Bitwise OR of new updated array ; Print the ans ; Driver Code
using System ; class GFG { static void performQuery ( int [ ] arr , int [ , ] Q ) { for ( int i = 0 ; i < Q . Length ; i ++ ) { int or = 0 ; int x = Q [ i , 0 ] ; arr [ x - 1 ] = Q [ i , 1 ] ; for ( int j = 0 ; j < arr . Length ; j ++ ) { or = or | arr [ j ] ; } Console . Write ( or + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int [ , ] Q = { { 1 , 4 } , { 3 , 0 } } ; performQuery ( arr , Q ) ; } }
Smallest length of number divisible by K formed by using D only | C # program for the above approach ; Function to form the smallest number possible ; Array to mark the remainders counted already ; Iterate over the range ; If that remainder is already found , return - 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int smallest ( int k , int d ) { int cnt = 1 ; int m = d % k ; int [ ] v = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) v [ i ] = 0 ; v [ m ] = 1 ; while ( true ) { if ( m == 0 ) return cnt ; m = ( ( ( m * ( 10 % k ) ) % k ) + ( d % k ) ) % k ; if ( v [ m ] == 1 ) return - 1 ; v [ m ] = 1 ; cnt ++ ; } } public static void Main ( ) { int d = 1 ; int k = 41 ; Console . Write ( smallest ( k , d ) ) ; } }
Fibonacci Cube Graph | C # code to find vertices in a fibonacci cube graph of order n ; function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; Driver code
using System ; class GFG { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } static int findVertices ( int n ) { return fib ( n + 2 ) ; } static void Main ( ) { int n = 3 ; Console . Write ( findVertices ( n ) ) ; } }
Modify array such that the array does not contain any common divisors other than 1 | C # program for the above approach ; Function to check if it is possible to modify the array such that there is no common factor between array elements except 1 ; Stores GCD of the array ; Calculate GCD of the array ; If the current divisor is smaller than X ; Divide GCD by the current divisor ; If possible ; Print the modified array ; Otherwise ; Driver Code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; class GFG { static int GCD ( int a , int b ) { return b == 0 ? a : GCD ( b , a % b ) ; } static void checkCommonDivisor ( int [ ] arr , int N , int X ) { int G = 0 ; for ( int i = 0 ; i < N ; i ++ ) { G = GCD ( G , arr [ i ] ) ; } int copy_G = G ; for ( int divisor = 2 ; divisor <= X ; divisor ++ ) { while ( G % divisor == 0 ) { G = G / divisor ; } } if ( G <= X ) { Console . WriteLine ( " Yes " ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] / copy_G + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 6 , 15 , 6 } ; int X = 6 ; int N = arr . Length ; checkCommonDivisor ( arr , N , X ) ; } }
Program to print Spiral Pattern | Create row and col to traverse rows and columns ; Variable to determine the movement r = right , l = left , d = down , u = upper ; Array for matrix ; Assign the value ; switch - case to determine the next index ; If right , go right ; if left , go left ; if up , go up ; if down , go down ; Check if the matrix has reached array boundary ; Add the left size for the next boundary ; If 2 rotations has been made , decrease the size left by 1 ; switch - case to rotate the movement ; if right , rotate to down ; if down , rotate to left ; if left , rotate to up ; if up , rotate to right ; Print the matrix ; Driver Code ; Get the size of size ; Print the Spiral Pattern
int row = 0 , col = 0 ; int boundary = size - 1 ; int sizeLeft = size - 1 ; int flag = 1 ; char move = ' r ' ; int [ , ] matrix = new int [ size , size ] ; for ( int i = 1 ; i < size * size + 1 ; i ++ ) { matrix [ row , col ] = i ; switch ( move ) { case ' r ' : col += 1 ; break ; case ' l ' : col -= 1 ; break ; case ' u ' : row -= 1 ; break ; case ' d ' : row += 1 ; break ; } if ( i == boundary ) { boundary += sizeLeft ; if ( flag != 2 ) { flag = 2 ; } else { flag = 1 ; sizeLeft -= 1 ; } switch ( move ) { case ' r ' : move = ' d ' ; break ; case ' d ' : move = ' l ' ; break ; case ' l ' : move = ' u ' ; break ; case ' u ' : move = ' r ' ; break ; } } } for ( row = 0 ; row < size ; row ++ ) { for ( col = 0 ; col < size ; col ++ ) { int n = matrix [ row , col ] ; Console . Write ( ( n < 10 ) ? ( n + " ▁ " ) : ( n + " ▁ " ) ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int size = 5 ; printSpiral ( size ) ; } }
K 'th Largest element in BST using constant extra space | C # Program for finding K - th largest Node using O ( 1 ) extra memory and reverse Morris traversal . ; Node structure ; helper function to create a new Node ; count variable to keep count of visited Nodes ; if right child is NULL ; first increment count and check if count = k ; otherwise move to the left child ; find inorder successor of current Node ; set left child of successor to the current Node ; move current to its right ; restoring the tree back to original binary search tree removing threaded links ; move current to its left child ; Driver code ; Constructed binary tree is 4 / \ 2 7 / \ / \ 1 3 6 10
using System ; using System . Collections . Generic ; class GfG { public class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . right = null ; temp . left = null ; return temp ; } static Node KthLargestUsingMorrisTraversal ( Node root , int k ) { Node curr = root ; Node Klargest = null ; int count = 0 ; while ( curr != null ) { if ( curr . right == null ) { if ( ++ count == k ) Klargest = curr ; curr = curr . left ; } else { Node succ = curr . right ; while ( succ . left != null && succ . left != curr ) succ = succ . left ; if ( succ . left == null ) { succ . left = curr ; curr = curr . right ; } else { succ . left = null ; if ( ++ count == k ) Klargest = curr ; curr = curr . left ; } } } return Klargest ; } public static void Main ( String [ ] args ) { Node root = newNode ( 4 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 10 ) ; Console . Write ( " Finding ▁ K - th ▁ largest ▁ Node ▁ in ▁ BST ▁ : ▁ " + KthLargestUsingMorrisTraversal ( root , 2 ) . data ) ; } }
Sort the matrix row | C # implementation to sort the matrix row - wise and column - wise ; function to sort each row of the matrix ; sorting row number ' i ' ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of mat [ , ] ; get transpose of mat [ , ] ; again sort rows of mat [ , ] ; again get transpose of mat [ , ] ; function to print the matrix ; Driver code
using System ; class GFG { static void sortByRow ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - 1 ; j ++ ) { if ( mat [ i , j ] > mat [ i , j + 1 ] ) { var temp = mat [ i , j ] ; mat [ i , j ] = mat [ i , j + 1 ] ; mat [ i , j + 1 ] = temp ; } } } } static void transpose ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { var temp = mat [ i , j ] ; mat [ i , j ] = mat [ j , i ] ; mat [ j , i ] = temp ; } } static void sortMatRowAndColWise ( int [ , ] mat , int n ) { sortByRow ( mat , n ) ; transpose ( mat , n ) ; sortByRow ( mat , n ) ; transpose ( mat , n ) ; } static void printMat ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( ) { int [ , ] mat = { { 4 , 1 , 3 } , { 9 , 6 , 8 } , { 5 , 2 , 7 } } ; int n = 3 ; Console . Write ( " Original ▁ Matrix : STRNEWLINE " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; Console . Write ( " STRNEWLINE Matrix ▁ After ▁ Sorting : STRNEWLINE " ) ; printMat ( mat , n ) ; } }
Magic Square | Even Order | C # program to print Magic square of Doubly even order ; Function for calculating Magic square ; filling matrix with its count value starting from 1 ; ; change value of Array elements at fix location as per rule ( n * n + 1 ) - arr [ i ] [ j ] Top Left corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Top Right corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Bottom Left corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Bottom Right corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Centre of Matrix ( order ( n / 2 ) * ( n / 2 ) ) ; Printing the magic - square ; Driver Code ; Function call
using System ; class GFG { public static void doublyEven ( int n ) { int [ , ] arr = new int [ n , n ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { arr [ i , j ] = ( n * i ) + j + 1 ; } } for ( i = 0 ; i < n / 4 ; i ++ ) { for ( j = 0 ; j < n / 4 ; j ++ ) { arr [ i , j ] = ( n * n + 1 ) - arr [ i , j ] ; } } for ( i = 0 ; i < n / 4 ; i ++ ) { for ( j = 3 * ( n / 4 ) ; j < n ; j ++ ) { arr [ i , j ] = ( n * n + 1 ) - arr [ i , j ] ; } } for ( i = 3 * n / 4 ; i < n ; i ++ ) { for ( j = 0 ; j < n / 4 ; j ++ ) { arr [ i , j ] = ( n * n + 1 ) - arr [ i , j ] ; } } for ( i = 3 * n / 4 ; i < n ; i ++ ) { for ( j = 3 * n / 4 ; j < n ; j ++ ) { arr [ i , j ] = ( n * n + 1 ) - arr [ i , j ] ; } } for ( i = n / 4 ; i < 3 * n / 4 ; i ++ ) { for ( j = n / 4 ; j < 3 * n / 4 ; j ++ ) { arr [ i , j ] = ( n * n + 1 ) - arr [ i , j ] ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( string [ ] args ) { int n = 8 ; doublyEven ( n ) ; } }