codeparrot/xlcost-text-to-code huggingface dataset:
Check if two arrays can be made equal by swapping pairs of one of the arrays | C # program for the above approach ; Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Driver Code ; Given arrays ; Size of the array
using System ; class GFG { static void checkArrays ( int [ ] arr1 , int [ ] arr2 , int N ) { int count = 0 ; bool flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = false ; break ; } } } } if ( ( flag && ( count == 0 ) ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static public void Main ( ) { int [ ] arr1 = { 0 , 0 , 1 , 1 } ; int [ ] arr2 = { 1 , 1 , 0 , 0 } ; int N = arr1 . Length ; checkArrays ( arr1 , arr2 , N ) ; } }
Generate an N | C # program to implement the above approach ; Function to construct an array with sum of each subarray divisible by K ; Traverse a loop from 1 to N ; Print i - th multiple of K ; Driver Code
using System ; public class GFG { static void construct_Array ( int N , int K ) { for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( K * i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 3 , K = 3 ; construct_Array ( N , K ) ; } }
Generate an N | C # Program to implement the above approach ; Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
using System ; class GFG { static void Print ( int N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { Console . WriteLine ( - 1 ) ; return ; } int CurEven = 2 , CurOdd = 1 ; int SumOdd = 0 , SumEven = 0 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { Console . Write ( CurEven + " ▁ " ) ; SumEven += CurEven ; CurEven += 2 ; } for ( int i = 0 ; i < N / 2 - 1 ; i ++ ) { Console . Write ( CurOdd + " ▁ " ) ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; Console . WriteLine ( CurOdd ) ; } public static void Main ( ) { int N = 12 ; Print ( N ) ; } }
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | C # program for the above approach ; Function to print the last remaining array element after after performing given operations ; If size of the array is 1 ; Check for the condition ; If condition is not satisfied ; Driver Code ; Function Call
using System ; public class GFG { static void canArrayBeReduced ( int [ ] arr , int N ) { if ( N == 1 ) { Console . Write ( arr [ 0 ] ) ; return ; } if ( arr [ 0 ] < arr [ N - 1 ] ) { Console . Write ( arr [ N - 1 ] ) ; } else Console . Write ( " Not ▁ Possible " ) ; } static public void Main ( ) { int [ ] arr = { 6 , 5 , 2 , 4 , 1 , 3 , 7 } ; int N = arr . Length ; canArrayBeReduced ( arr , N ) ; } }
Minimum prefix increments required to make all elements of an array multiples of another array | C # program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays
using System ; class GFG { static int MinimumMoves ( int [ ] A , int [ ] B , int N ) { int totalOperations = 0 ; int carry = 0 ; int K = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int nearestMultiple = ( int ) ( Math . Ceiling ( ( double ) ( A [ i ] + carry ) / ( double ) ( B [ i ] ) ) * B [ i ] ) ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } public static void Main ( string [ ] args ) { int [ ] A = { 3 , 4 , 5 , 2 , 5 , 5 , 9 } ; int [ ] B = { 1 , 1 , 9 , 6 , 3 , 8 , 7 } ; int N = A . Length ; Console . Write ( MinimumMoves ( A , B , N ) + " STRNEWLINE " ) ; } }
Minimize deviation of an array by given operations | C # implementation of the above approach ; Function to find the minimum deviation of the array A [ ] ; Store all array elements in sorted order ; Odd number are transformed using 2 nd operation ; ( Maximum - Minimum ) ; Check if the size of set is > 0 and the maximum element is divisible by 2 ; Maximum element of the set ; Erase the maximum element ; Using operation 1 ; ( Maximum - Minimum ) ; Print the Minimum Deviation Obtained ; Driver code ; Function Call to find Minimum Deviation of A [ ]
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void minimumDeviation ( int [ ] A , int N ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) s . Add ( A [ i ] ) ; else s . Add ( 2 * A [ i ] ) ; } List < int > S = s . ToList ( ) ; S . Sort ( ) ; int diff = S [ S . Count - 1 ] - S [ 0 ] ; while ( ( int ) S . Count != 0 && S [ S . Count - 1 ] % 2 == 0 ) { int maxEl = S [ S . Count - 1 ] ; S . RemoveAt ( S . Count - 1 ) ; S . Add ( maxEl / 2 ) ; S . Sort ( ) ; diff = Math . Min ( diff , S [ S . Count - 1 ] - S [ 0 ] ) ; } Console . Write ( diff ) ; } static void Main ( ) { int [ ] A = { 4 , 1 , 5 , 20 , 3 } ; int N = A . Length ; minimumDeviation ( A , N ) ; } }
Maximize given integer by swapping pairs of unequal bits | C # implementation of the above approach ; Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver Code ; Given "Input ; Function call to find the Maximum Possible Number
using System ; public class GFG { public static int findMaxNum ( int num ) { string binaryNumber = Convert . ToString ( num , 2 ) ; string maxBinaryNumber = " " ; int count0 = 0 , count1 = 0 ; int N = binaryNumber . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( binaryNumber [ i ] == '1' ) { count1 ++ ; } else { count0 ++ ; } } for ( int i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += '1' ; } for ( int i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += '0' ; } return Convert . ToInt32 ( maxBinaryNumber , 2 ) ; } static public void Main ( ) { int N = 11 ; Console . WriteLine ( findMaxNum ( N ) ) ; } }
Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | C # implementation of the above approach ; Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input String ; Function call
using System ; class GFG { static void minOperations ( String N ) { int even = 0 ; int odd = 0 ; foreach ( int it in N . ToCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { Console . Write ( " Not ▁ Possible " + " STRNEWLINE " ) ; } else { int ans = 0 ; foreach ( int it in N . ToCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 != 0 ) { ans ++ ; } Console . Write ( ans + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { String N = "12345" ; minOperations ( N ) ; } }
Find the player with least 0 s after emptying a Binary String by removing non | C # program to implement the above approach ; Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
using System ; class GFG { public static void FindwinnerOfGame ( string S ) { int cntZero = 0 ; int cntConOne = 0 ; int nimSum = 0 ; int N = S . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { Console . Write ( " Tie " ) ; } else if ( nimSum != 0 ) { Console . Write ( " player ▁ 1" ) ; } else { Console . Write ( " player ▁ 2" ) ; } } public static void Main ( string [ ] args ) { string S = "0110011" ; FindwinnerOfGame ( S ) ; } }
Place first N natural numbers at indices not equal to their values in an array | C # program for the above approach ; Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code
using System ; class GFG { static void generatepermutation ( int N ) { int [ ] answer = new int [ N ] ; answer [ 0 ] = N ; for ( int i = 1 ; i < N ; i ++ ) { answer [ i ] = i ; } foreach ( int i in answer ) Console . Write ( i + " ▁ " ) ; } static public void Main ( ) { int N = 4 ; generatepermutation ( N ) ; } }
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | C # program for the above approach ; Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Print the answer ; Driver Code ; Given N , K & M
using System ; class GFG { static void maxValueAtIndexK ( int N , int K , int M ) { int S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; int X = ( M + S1 + S2 ) / N ; Console . WriteLine ( X ) ; } static public void Main ( ) { int N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ; } }
Farthest cell from a given cell in a Matrix | C # program for the above approach ; Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Print the answer ; Driver Code
using System ; class GFG { static void farthestCellDistance ( int N , int M , int R , int C ) { int d1 = N + M - R - C ; int d2 = R + C - 2 ; int d3 = N - R + C - 1 ; int d4 = M - C + R - 1 ; int maxDistance = Math . Max ( d1 , Math . Max ( d2 , Math . Max ( d3 , d4 ) ) ) ; Console . WriteLine ( maxDistance ) ; } static public void Main ( ) { int N = 15 , M = 12 , R = 1 , C = 6 ; farthestCellDistance ( N , M , R , C ) ; } }
Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | C # program for the above approach ; Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Print the array ; Driver Code ; Given array ; Size of the array
using System ; class GFG { static void maxSum ( int [ ] arr , int N ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { int t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 7 , 1 , 8 } ; int N = arr . Length ; maxSum ( arr , N ) ; } }
Generate an N | C # program for the above approach ; Function to construct an array with given conditions ; Traverse the array arr [ ] ; Stores closest power of 2 less than or equal to arr [ i ] ; Stores R into brr [ i ] ; Print array elements ; Driver Code ; Given array ; Size of the array ; Function Call
using System ; class GFG { static void constructArray ( int [ ] arr , int N ) { int [ ] brr = new int [ N ] ; Array . Clear ( brr , 0 , brr . Length ) ; for ( int i = 0 ; i < N ; i ++ ) { int K = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; int R = ( int ) Math . Pow ( 2 , K ) ; brr [ i ] = R ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( brr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 11 , 5 , 7 , 3 , 2 } ; int N = arr . Length ; constructArray ( arr , N ) ; } }
Find the element at specified index in a Spiral Matrix | C # program for the above approach ; Function to the find element at ( i , j ) index ; Driver Code ; Function Call
using System ; class GFG { static int findInGrid ( int i , int j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } static public void Main ( ) { int i = 3 , j = 4 ; Console . WriteLine ( findInGrid ( i , j ) ) ; } }
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements | C # program for the above approach ; Function to find minimum count of operations required to convert all array elements to zero br replacing subsequence of equal elements to 0 ; Store distinct elements present in the array ; Traverse the array ; If arr [ i ] is already present in the Set or arr [ i ] is equal to 0 ; Otherwise , increment ans by 1 and insert current element ; Driver Code ; Given array ; Size of the given array
using System ; using System . Collections . Generic ; class GFG { static void minOpsToTurnArrToZero ( int [ ] arr , int N ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . Contains ( arr [ i ] ) arr [ i ] == 0 ) { continue ; } else { st . Add ( arr [ i ] ) ; } } Console . WriteLine ( st . Count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 3 } ; int N = arr . Length ; minOpsToTurnArrToZero ( arr , N ) ; } }
Split an array into equal length subsequences consisting of equal elements only | C # program for the above approach ; Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of each array element ; Store the GCD of frequencies of all array elements ; Traverse the map ; Update GCD ; If the GCD is greater than 1 , print YES otherwise print NO ; Driver Code ; Given array ; Store the size of the array
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void splitArray ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) == true ) mp [ arr [ i ] ] += 1 ; else mp [ arr [ i ] ] = 1 ; } int G = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { G = gcd ( G , i . Value ) ; } if ( G > 1 ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int n = arr . Length ; splitArray ( arr , n ) ; } }
Minimize count of given operations required to be performed to make all array elements equal to 1 | C # program to implement the above approach ; Function to check if all array elements are equal or not ; Traverse the array ; If all array elements are not equal ; Function to find minimum count of operation to make all the array elements equal to 1 ; Stores largest element of the array ; Check if a number is a power of 2 or not ; If Max is a power of 2 and all array elements are equal ; Driver Code
using System ; using System . Linq ; class GFG { static bool CheckAllEqual ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return false ; } } return true ; } static int minCntOperations ( int [ ] arr , int N ) { int Max = arr . Max ( ) ; bool isPower2 ; if ( ( int ) ( Math . Ceiling ( ( Math . Log ( N ) / Math . Log ( N ) ) ) ) == ( int ) ( Math . Floor ( ( ( Math . Log ( N ) / Math . Log ( 2 ) ) ) ) ) ) { isPower2 = true ; } else { isPower2 = false ; } if ( isPower2 && CheckAllEqual ( arr , N ) ) { return ( int ) ( Math . Log ( Max ) / Math . Log ( 2 ) ) ; } else { return ( int ) Math . Ceiling ( Math . Log ( Max ) / Math . Log ( 2 ) ) + 1 ; } } static public void Main ( ) { int [ ] arr = new int [ ] { 2 , 4 } ; int N = arr . Length ; Console . WriteLine ( minCntOperations ( arr , N ) ) ; } }
Largest number not exceeding N that does not contain any of the digits of S | C # program to implement the above approach ; Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static string greatestReducedNumber ( string num , string s ) { bool [ ] vis_s = new bool [ 10 ] ; for ( int i = 0 ; i < ( int ) s . Length ; i ++ ) { vis_s [ ( int ) ( s [ i ] ) - 48 ] = true ; } int n = num . Length ; int In = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis_s [ ( int ) num [ i ] - '0' ] ) { In = i ; break ; } } if ( In == - 1 ) { return num ; } for ( char dig = num [ In ] ; dig >= '0' ; dig -- ) { if ( vis_s [ ( int ) dig - '0' ] == false ) { num = num . Substring ( 0 , In ) + dig + num . Substring ( In + 1 , n - In - 1 ) ; break ; } } char LargestDig = '0' ; for ( char dig = '9' ; dig >= '0' ; dig -- ) { if ( vis_s [ dig - '0' ] == false ) { LargestDig = dig ; break ; } } for ( int i = In + 1 ; i < n ; i ++ ) { num = num . Substring ( 0 , i ) + LargestDig ; } int Count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( num [ i ] == '0' ) Count ++ ; else break ; } num = num . Substring ( Count , n ) ; if ( ( int ) num . Length == 0 ) return "0" ; return num ; } static void Main ( ) { string N = "12345" ; string S = "23" ; Console . Write ( greatestReducedNumber ( N , S ) ) ; } }
Generate an array of minimum sum whose XOR of same | C # program to implement the above approach ; Function to generate an array whose XOR with same - indexed elements of the given array is always a prime ; Traverse the array ; If current array element is 2 ; Print its XOR with 3 ; Otherwise ; Print its XOR with 2 ; Driver code ; Given array ; Size of the array ; Prints the required array
using System ; class GFG { private static void minXOR ( int [ ] Arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( Arr [ i ] == 2 ) { Console . Write ( ( Arr [ i ] ^ 3 ) + " ▁ " ) ; } else { Console . Write ( ( Arr [ i ] ^ 2 ) + " ▁ " ) ; } } } public static void Main ( ) { int [ ] Arr = { 5 , 4 , 7 , 6 } ; int N = Arr . Length ; minXOR ( Arr , N ) ; } }
Find the largest element in an array generated using the given conditions | C # program to implement the above approach ; Function to generate the required array and to find and return the maximum array element ; Stores the array ; Base case ; Iterate over the indices ; If current index is even ; Otherwise ; Function to find and return the maximum array element ; If n is 0 ; If n is 1 ; Generates the required array ; Return maximum element of Arr ; Driver Code
using System ; class GFG { static int [ ] findArray ( int n ) { int [ ] Arr = new int [ n + 1 ] ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { Arr [ i ] = Arr [ i / 2 ] ; } else { Arr [ i ] = Arr [ ( i - 1 ) / 2 ] + Arr [ ( i - 1 ) / 2 + 1 ] ; } } return Arr ; } static int maxElement ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; int [ ] Arr = findArray ( n ) ; int ans = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . Max ( ans , Arr [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 7 ; Console . WriteLine ( maxElement ( N ) ) ; } }
Construct a graph using N vertices whose shortest distance between K pair of vertices is 2 | C # program to implement the above approach ; Function to construct the simple and connected graph such that the distance between exactly K pairs of vertices is 2 ; Stores maximum possible count of edges in a graph ; Base Case ; Stores edges of a graph ; Connect all vertices of pairs ( i , j ) ; Print first ( ( N - 1 ) + Max - K ) elements of edges [ ] ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void constGraphWithCon ( int N , int K ) { int Max = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; if ( K > Max ) { Console . Write ( - 1 + " STRNEWLINE " ) ; return ; } List < pair > ans = new List < pair > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { ans . Add ( new pair ( i , j ) ) ; } } for ( int i = 0 ; i < ( N - 1 ) + Max - K ; i ++ ) { Console . Write ( ans [ i ] . first + " ▁ " + ans [ i ] . second + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int N = 5 , K = 3 ; constGraphWithCon ( N , K ) ; } }
Minimum steps required to visit all corners of an N * M grid | C # program to implement the above approach ; Function to find the minimum count of steps required to visit all the corners of the grid ; Stores corner of the grid ; Stores minimum count of steps required to visit the first corner of the grid ; Checking for leftmost upper corner ; Checking for leftmost down corner ; Checking for rightmost upper corner ; Checking for rightmost down corner ; Stores minimum count of steps required to visit remaining three corners of the grid ; Driver Code
using System ; class GFG { static int min_steps_required ( int n , int m , int r , int c ) { int i , j ; int corner_steps_req = int . MaxValue ; i = 1 ; j = 1 ; corner_steps_req = Math . Min ( corner_steps_req , Math . Abs ( r - i ) + Math . Abs ( j - c ) ) ; i = n ; j = 1 ; corner_steps_req = Math . Min ( corner_steps_req , Math . Abs ( r - i ) + Math . Abs ( j - c ) ) ; i = 1 ; j = m ; corner_steps_req = Math . Min ( corner_steps_req , Math . Abs ( r - i ) + Math . Abs ( j - c ) ) ; i = n ; j = m ; corner_steps_req = Math . Min ( corner_steps_req , Math . Abs ( r - i ) + Math . Abs ( j - c ) ) ; int minimum_steps = Math . Min ( 2 * ( n - 1 ) + m - 1 , 2 * ( m - 1 ) + n - 1 ) ; return minimum_steps + corner_steps_req ; } public static void Main ( String [ ] args ) { int n = 3 ; int m = 2 ; int r = 1 ; int c = 1 ; Console . Write ( min_steps_required ( n , m , r , c ) ) ; } }