codeparrot/xlcost-text-to-code huggingface dataset:
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | C # program to implement the above approach ; Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Perform K operations ; Stores smallest element in the array ; Stores index of the smallest array element ; Stores largest element in the array ; Stores index of the largest array element ; Traverse the array elements ; If current element exceeds largest_element ; Update the largest element ; Update index of the largest array element ; If current element is smaller than smallest_element ; Update the smallest element ; Update index of the smallest array element ; Stores the value of smallest element by given operations ; Stores the value of largest element by given operations ; If the value of a + b less than the sum of smallest and largest element of the array ; Update smallest element of the array ; Update largest element of the array ; Stores sum of elements of the array by given operations ; Traverse the array ; Update ans ; Driver Code
using System ; class GFG { static int minimum_possible_sum ( int [ ] arr , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = arr [ 0 ] ; int smallest_pos = 0 ; int largest_element = arr [ 0 ] ; int largest_pos = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] >= largest_element ) { largest_element = arr [ j ] ; largest_pos = j ; } if ( arr [ j ] < smallest_element ) { smallest_element = arr [ j ] ; smallest_pos = j ; } } int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { arr [ smallest_pos ] = a ; arr [ largest_pos ] = b ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = arr . Length ; Console . WriteLine ( minimum_possible_sum ( arr , n , K ) ) ; } }
Rearrange array elements excluded by given ranges to maximize sum of subarrays starting from the first index | C # program for the above approach ; Function that finds the maximum sum all subarrays from the starting index after rearranging the array ; Stores elements after rearranging ; Keeps track of visited elements ; Traverse the queries ; Mark elements that are not allowed to rearranged ; Stores the indices ; Get indices and elements that are allowed to rearranged ; Store the current index and element ; Sort vector v in descending order ; Insert elements in array ; Stores the resultant sum ; Stores the prefix sums ; Traverse the given array ; Return the maximum sum ; Driver Code ; Given array ; Given size ; Queries ; Number of queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int maxSum ( int n , int [ ] a , int [ , ] l , int q ) { List < int > v = new List < int > ( ) ; int [ ] d = new int [ n ] ; for ( int i = 0 ; i < q ; i ++ ) { for ( int x = l [ i , 0 ] ; x <= l [ i , 1 ] ; x ++ ) { if ( d [ x ] == 0 ) { d [ x ] = 1 ; } } } HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] == 0 ) { v . Add ( a [ i ] ) ; st . Add ( i ) ; } } v . Sort ( ) ; v . Reverse ( ) ; int c = 0 ; foreach ( int it in st ) { a [ it ] = v ; c ++ ; } int pref_sum = 0 ; int temp_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp_sum += a [ i ] ; pref_sum += temp_sum ; } return pref_sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 } ; int N = arr . Length ; int [ , ] q = { { 0 , 0 } , { 4 , 5 } } ; int queries = q . GetLength ( 0 ) ; Console . Write ( maxSum ( N , arr , q , queries ) ) ; } }
Count pairs with Bitwise XOR greater than both the elements of the pair | C # program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call
using System ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; Array . Sort ( A ) ; int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( ( 1 << j ) & A [ i ] ) == 0 ) { count += bits [ j ] ; } } ++ bits [ ( int ) ( ( int ) ( Math . Log ( A [ i ] ) / Math . Log ( 2 ) ) ) ] ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Maximum occurred integers after M circular operations in given range | C # program for the above approach ; Function to find the maximum occurred integer after completing all circular operations ; Stores the highest visit count for any element ; Stors the number of times an element is visited ; Iterate over the array ; Iterate over the range circularly form start to end ; Count number of times an element is visited ; Increment frequency of N ; Update maxVisited ; Increment frequency of start ; Update maxVisited ; Increment the start ; Increment the count for the last visited element ; Print most visited elements ; Driver Code ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void mostvisitedsector ( int N , ArrayList A ) { int maxVisited = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < A . Count - 1 ; i ++ ) { int start = ( int ) A [ i ] % N ; int end = ( int ) A [ i + 1 ] % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . ContainsKey ( N ) ) mp [ N ] = mp [ N ] + 1 ; else mp [ N ] = 1 ; if ( mp [ N ] > maxVisited ) { maxVisited = mp [ N ] ; } } else { if ( mp . ContainsKey ( start ) ) mp [ start ] = mp [ start ] + 1 ; else mp [ start ] = 1 ; if ( mp [ start ] > maxVisited ) { maxVisited = mp [ start ] ; } } start = ( start + 1 ) % N ; } } int last_element = ( int ) A [ A . Count - 1 ] ; if ( mp . ContainsKey ( last_element ) ) mp [ last_element ] = mp [ last_element ] + 1 ; else mp [ last_element ] = 1 ; if ( mp [ last_element ] > maxVisited ) { maxVisited = mp [ last_element ] ; } foreach ( var x in mp ) { if ( ( int ) x . Value == maxVisited ) { Console . Write ( x . Key + " ▁ " ) ; } } } public static void Main ( ) { int N = 4 ; ArrayList arr = new ArrayList ( ) { 1 , 2 , 1 , 2 } ; mostvisitedsector ( N , arr ) ; } }
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
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 ) ) ; } }
Rearrange two given arrays such that sum of same indexed elements lies within given range | C # program for the above approach ; Function to check if there exists any arrangements of the arrays such that sum of element lie in the range [ K / 2 , K ] ; Sort the array arr1 [ ] in increasing order ; Sort the array arr2 [ ] in decreasing order ; Traverse the array ; If condition is not satisfied break the loop ; Print the result ; Driver Code
using System ; using System . Collections ; class GFG { static void checkArrangement ( int [ ] A1 , int [ ] A2 , int n , int k ) { Array . Sort ( A1 ) ; Array . Sort ( A2 ) ; Array . Reverse ( A2 ) ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( A1 [ i ] + A2 [ i ] > k ) || ( A1 [ i ] + A2 [ i ] < k / 2 ) ) { flag = 1 ; break ; } } if ( flag == 1 ) Console . WriteLine ( " No " ) ; else Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int [ ] arr1 = { 1 , 3 , 4 , 5 } ; int [ ] arr2 = { 2 , 0 , 1 , 1 } ; int K = 6 ; int N = arr1 . Length ; checkArrangement ( arr1 , arr2 , N , K ) ; } }
Rearrange two given arrays to maximize sum of same indexed elements | Java program to implement the above approach ; Function to find the maximum possible sum by rearranging the given array elements ; Sort the array A [ ] in ascending order ; Sort the array B [ ] in descending order ; Stores maximum possible sum by rearranging array elements ; Traverse both the arrays ; Update maxSum ; Driver code
using System ; class GFG { static int MaxRearrngeSum ( int [ ] A , int [ ] B , int N ) { Array . Sort ( A ) ; Array . Sort ( B ) ; Array . Reverse ( B ) ; int maxSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { maxSum += Math . Abs ( A [ i ] - B [ i ] ) ; } return maxSum ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 2 , 4 , 5 } ; int [ ] B = { 5 , 5 , 5 , 6 , 6 } ; int N = A . Length ; Console . WriteLine ( MaxRearrngeSum ( A , B , 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
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 ) ) ; } }
Count pairs from two arrays with difference exceeding K | C # program to implement the above approach ; Function to count pairs that satisfy the given conditions ; Stores index of the left pointer . ; Stores index of the right pointer ; Stores count of total pairs that satisfy the conditions ; Sort arr [ ] array ; Sort brr [ ] array ; Traverse both the array and count then pairs ; If the value of ( brr [ j ] - arr [ i ] ) exceeds K ; Update cntPairs ; Update ; Update j ; Driver code
using System ; class GFG { static int count_pairs ( int [ ] arr , int [ ] brr , int N , int M , int K ) { int i = 0 ; int j = 0 ; int cntPairs = 0 ; Array . Sort ( arr ) ; Array . Sort ( brr ) ; while ( i < N && j < M ) { if ( brr [ j ] - arr [ i ] > K ) { cntPairs += ( M - j ) ; i ++ ; } else { j ++ ; } } return cntPairs ; } static void Main ( ) { int [ ] arr = { 5 , 9 , 1 , 8 } ; int [ ] brr = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = arr . Length ; int M = brr . Length ; Console . WriteLine ( count_pairs ( arr , brr , N , M , K ) ) ; } }
Merge two sorted arrays in O ( 1 ) extra space using Heap | C # program to implement the above approach ; Function to perform min heapify on array brr [ ] ; Stores index of left child of i . ; Stores index of right child of i . ; Stores index of the smallest element in ( arr [ i ] , arr [ left ] , arr [ right ] ) ; Check if arr [ left ] less than arr [ smallest ] ; Update smallest ; Check if arr [ right ] less than arr [ smallest ] ; Update smallest ; If i is not the index of smallest element ; Swap arr [ i ] and arr [ smallest ] ; Perform heapify on smallest ; Function to merge two sorted arrays ; Traverse the array [ ] arr ; Check if current element is less than brr [ 0 ] ; Swap arr [ i ] and brr [ 0 ] ; Perform heapify on brr [ ] ; Sort array brr [ ] ; Function to print array elements ; Traverse array [ ] arr ; Driver Code ; Function call to merge two array ; Print first array ; Print Second array .
using System ; class GFG { static void minHeapify ( int [ ] brr , int i , int M ) { int left = 2 * i + 1 ; int right = 2 * i + 2 ; int smallest = i ; if ( left < M && brr [ left ] < brr [ smallest ] ) { smallest = left ; } if ( right < M && brr [ right ] < brr [ smallest ] ) { smallest = right ; } if ( smallest != i ) { int temp = brr [ i ] ; brr [ i ] = brr [ smallest ] ; brr [ smallest ] = temp ; minHeapify ( brr , smallest , M ) ; } } static void merge ( int [ ] arr , int [ ] brr , int N , int M ) { for ( int i = 0 ; i < N ; ++ i ) { if ( arr [ i ] > brr [ 0 ] ) { int temp = arr [ i ] ; arr [ i ] = brr [ 0 ] ; brr [ 0 ] = temp ; minHeapify ( brr , 0 , M ) ; } } Array . Sort ( brr ) ; } static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 23 , 35 , 235 , 2335 } ; int [ ] brr = { 3 , 5 } ; int N = arr . Length ; int M = brr . Length ; merge ( arr , brr , N , M ) ; printArray ( arr , N ) ; printArray ( brr , M ) ; } }
Merge two sorted arrays in O ( 1 ) extra space using QuickSort partition | C # program to implement the above approach ; Function to perform the partition around the pivot element ; Stores index of each element of the array , [ ] arr ; Stores index of each element of the array , brr [ ] ; Traverse both the array ; If pivot is smaller than arr [ l ] ; If Pivot is greater than brr [ r ] ; If either arr [ l ] > Pivot or brr [ r ] < Pivot ; Function to merge the two sorted array ; Stores index of each element of the array [ ] arr ; Stores index of each element of the array brr [ ] ; Stores index of each element the readonly sorted array ; Stores the pivot element ; Traverse both the array ; If pivot element is not found or index < N ; If pivot element is not found or index < N ; Place the first N elements of the sorted array into [ ] arr and the last M elements of the sorted array into brr [ ] ; Sort both the arrays ; Print the first N elements in sorted order ; Print the last M elements in sorted order ; Driver Code
using System ; class GFG { static void partition ( int [ ] arr , int N , int [ ] brr , int M , int Pivot ) { int l = N - 1 ; int r = 0 ; while ( l >= 0 && r < M ) { if ( arr [ l ] < Pivot ) l -- ; else if ( brr [ r ] > Pivot ) r ++ ; else { int t = arr [ l ] ; arr [ l ] = brr [ r ] ; brr [ r ] = t ; l -- ; r ++ ; } } } static void Merge ( int [ ] arr , int N , int [ ] brr , int M ) { int l = 0 ; int r = 0 ; int index = - 1 ; int Pivot = 0 ; while ( index < N && l < N && r < M ) { if ( arr [ l ] < brr [ r ] ) { Pivot = arr [ l ++ ] ; } else { Pivot = brr [ r ++ ] ; } index ++ ; } while ( index < N && l < N ) { Pivot = arr [ l ++ ] ; index ++ ; } while ( index < N && r < M ) { Pivot = brr [ r ++ ] ; index ++ ; } partition ( arr , N , brr , M , Pivot ) ; Array . Sort ( arr ) ; Array . Sort ( brr ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; for ( int i = 0 ; i < M ; i ++ ) Console . Write ( brr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 9 } ; int [ ] brr = { 2 , 4 , 7 , 10 } ; int N = arr . Length ; int M = brr . Length ; Merge ( arr , N , brr , M ) ; } }
Count array elements with rank not exceeding K | C # program for the above approach ; Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Driver Code ; Given array ; Given K ; Function Call
using System ; class GFG { static int rankLessThanK ( int [ ] arr , int k , int n ) { int rank = 1 ; int position = 1 ; Array . Sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i == n - 1 arr [ i ] != arr [ i + 1 ] ) { rank = position ; if ( rank > k ) return position - 1 ; } position ++ ; } return n ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; int K = 4 ; Console . WriteLine ( rankLessThanK ( arr , K , N ) ) ; } }
Remove Sub | C # program for the above approach ; Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call
using System ; using System . Collections ; class GFG { static void eraseSubdirectory ( ArrayList dir ) { ArrayList res = new ArrayList ( ) ; dir . Sort ( ) ; res . Add ( dir [ 0 ] ) ; Console . Write ( " { " + dir [ 0 ] + " , ▁ " ) ; for ( int i = 1 ; i < dir . Count ; i ++ ) { string curr = ( string ) dir [ i ] ; string prev = ( string ) res [ ( res . Count - 1 ) ] ; int l = prev . Length ; if ( curr . Length > l && curr [ l ] == ' / ' && prev . Equals ( curr . Substring ( 0 , l ) ) ) continue ; res . Add ( curr ) ; Console . Write ( curr + " , ▁ " ) ; } Console . Write ( " } STRNEWLINE " ) ; } public static void Main ( ) { ArrayList dir = new ArrayList ( ) { " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " } ; eraseSubdirectory ( dir ) ; } }
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | C # program for the above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given [ ] arr ; Function Calling
using System ; class GFG { static void minCost ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( 0 + " STRNEWLINE " ) ; return ; } Array . Sort ( arr ) ; float raised = 1 / ( float ) ( n - 1 ) ; float temp = ( float ) Math . Pow ( arr [ n - 1 ] , raised ) ; int r = ( int ) ( temp ) + 1 ; int i , j , min_cost = int . MaxValue ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . Abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . Min ( min_cost , curr_cost ) ; common_ratio = j ; } } Console . Write ( min_cost + " ▁ " ) ; Console . Write ( common_ratio + " ▁ " ) ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; } }
Mth bit in Nth binary string from a sequence generated by the given operations | C # program for the above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the String Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code
using System ; class GFG { static int maxN = 10 ; static void calculateFib ( int [ ] fib , int n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( int x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } static int find_mth_bit ( int n , int m , int [ ] fib ) { if ( n <= 1 ) { return n ; } int len_left = fib [ n - 2 ] ; int len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } static void find_mth_bitUtil ( int n , int m ) { int [ ] fib = new int [ maxN ] ; calculateFib ( fib , maxN ) ; int ans = find_mth_bit ( n , m , fib ) ; Console . Write ( ans + " ▁ " ) ; } public static void Main ( ) { int n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ; } }
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | C # program to implement the above approach ; Function to count the minimum number of adjacent swaps ; Stores the size of the given matrix ; Stores the count of zero at the end of each row ; Traverse the given matrix ; Count of 0 s at the end of the ith row ; Stores the count of swaps ; Traverse the cntZero array ; If count of zero in the i - th row < ( N - i - 1 ) ; Stores the index of the row where count of zero > ( N - i - 1 ) ; If no row found that satisfy the condition ; Swap the adjacent row ; Driver Code
using System ; class GFG { static int minAdjSwaps ( int [ , ] mat ) { int N = mat . GetLength ( 0 ) ; int [ ] cntZero = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 && mat [ i , j ] == 0 ; j -- ) { cntZero [ i ] ++ ; } } int cntSwaps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntZero [ i ] < ( N - i - 1 ) ) { int First = i ; while ( First < N && cntZero [ First ] < ( N - i - 1 ) ) { First ++ ; } if ( First == N ) { return - 1 ; } while ( First > i ) { cntZero = swap ( cntZero , First , First - 1 ) ; First -- ; cntSwaps ++ ; } } } return cntSwaps ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 2 } , { 3 , 1 , 0 } , { 4 , 0 , 0 } } ; Console . Write ( minAdjSwaps ( mat ) ) ; } }
Largest area in a grid unbounded by towers | C # Program for the above approach ; Function to calculate the largest area unguarded by towers ; Sort the x - coordinates of the list ; Sort the y - coordinates of the list ; dx -- > maximum uncovered tiles in x coordinates ; dy -- > maximum uncovered tiles in y coordinates ; Calculate the maximum uncovered distances for both x and y coordinates ; Largest unguarded area is max ( dx ) - 1 * max ( dy ) - 1 ; Driver Code ; Length and width of the grid ; No of guard towers ; Array to store the x and y coordinates ; Function call
using System ; class GFG { static void maxArea ( int [ ] point_x , int [ ] point_y , int n , int length , int width ) { Array . Sort ( point_x ) ; Array . Sort ( point_y ) ; int dx = point_x [ 0 ] ; int dy = point_y [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dx = Math . Max ( dx , point_x [ i ] - point_x [ i - 1 ] ) ; dy = Math . Max ( dy , point_y [ i ] - point_y [ i - 1 ] ) ; } dx = Math . Max ( dx , ( length + 1 ) - point_x [ n - 1 ] ) ; dy = Math . Max ( dy , ( width + 1 ) - point_y [ n - 1 ] ) ; Console . WriteLine ( ( dx - 1 ) * ( dy - 1 ) ) ; } static public void Main ( ) { int length = 15 , width = 8 ; int n = 3 ; int [ ] point_x = { 3 , 11 , 8 } ; int [ ] point_y = { 8 , 2 , 6 } ; maxArea ( point_x , point_y , n , length , width ) ; } }