codeparrot/xlcost-text-to-code huggingface dataset:
Longest Palindrome in a String formed by concatenating its prefix and suffix | C # program to find the longest palindrome in a String formed by concatenating its prefix and suffix ; Function to check whether the String is a palindrome ; Reverse the String to compare with the original String ; Check if both are same ; Function to find the longest palindrome in a String formed by concatenating its prefix and suffix ; Length of the String ; Finding the length upto which the suffix and prefix forms a palindrome together ; Check whether the String has prefix and suffix subStrings which are palindromes . ; Removing the suffix and prefix subStrings which already forms a palindrome and storing them in separate Strings ; Check all prefix subStrings in the remaining String str ; Check if the prefix subString is a palindrome ; If the prefix subString is a palindrome then check if it is of maximum length so far ; Check all the suffix subStrings in the remaining String str ; Check if the suffix subString is a palindrome ; If the suffix subString is a palindrome then check if it is of maximum length so far ; Combining all the thee parts of the answer ; Driver code
using System ; class GFG { static bool isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . Equals ( p ) ) ; } static String PrefixSuffixPalindrome ( String str ) { int n = str . Length , len = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != str [ n - i - 1 ] ) { len = i ; break ; } } String prefix = " " , suffix = " " ; String midPal = " " ; prefix = str . Substring ( 0 , len ) ; suffix = str . Substring ( n - len ) ; str = str . Substring ( len , ( n - 2 * len ) + len ) ; for ( int i = 1 ; i <= str . Length ; i ++ ) { String y = str . Substring ( 0 , i ) ; if ( isPalindrome ( y ) ) { if ( midPal . Length < y . Length ) { midPal = y ; } } } for ( int i = 1 ; i <= str . Length ; i ++ ) { String y = str . Substring ( str . Length - i ) ; if ( isPalindrome ( y ) ) { if ( midPal . Length < y . Length ) { midPal = y ; } } } String answer = prefix + midPal + suffix ; return answer ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String str = " abcdfdcecba " ; Console . Write ( PrefixSuffixPalindrome ( str ) ) ; } }
Minimize the maximum difference between adjacent elements in an array | C # implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Traversing over subsets in iterative manner ; Number of elements to be taken in the subset ON bits of i represent elements not to be removed ; If the removed set is of size k ; Creating the new array after removing elements ; Maximum difference of adjacent elements of remaining array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int minimumAdjacentDifference ( int [ ] a , int n , int k ) { int minDiff = int . MaxValue ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = countSetBits ( i ) ; if ( cnt == n - k ) { List < int > temp = new List < int > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . Add ( a [ j ] ) ; } int maxDiff = int . MinValue ; for ( int j = 0 ; j < temp . Count - 1 ; j ++ ) { maxDiff = Math . Max ( maxDiff , temp [ j + 1 ] - temp [ j ] ) ; } minDiff = Math . Min ( minDiff , maxDiff ) ; } } return minDiff ; } static int countSetBits ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; Console . WriteLine ( minimumAdjacentDifference ( a , n , k ) ) ; } }
Minimize the maximum difference between adjacent elements in an array | C # implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Iterating over all subarrays of size n - k ; Maximum difference after removing elements ; Minimum Adjacent Difference ; Driver Code
using System ; class GFG { static int minimumAdjacentDifference ( int [ ] a , int n , int k ) { int minDiff = int . MaxValue ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = int . MinValue ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = Math . Max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . Min ( minDiff , maxDiff ) ; } return minDiff ; } public static void Main ( string [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; Console . WriteLine ( minimumAdjacentDifference ( a , n , k ) ) ; } }
Minimum elements inserted in a sorted array to form an Arithmetic progression | C # implementation to find the minimum elements required to be inserted into an array to form an arithmetic progression ; Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling
using System ; class GFG { static int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } static int findMinimumElements ( int [ ] a , int n ) { int [ ] b = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } static public void Main ( ) { int [ ] arr1 = new int [ ] { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = arr1 . Length ; Console . WriteLine ( findMinimumElements ( arr1 , n1 ) ) ; } }
Check if a Sequence is a concatenation of two permutations | C # program to check if a given sequence is a concatenation of two permutations or not ; Function to Check if a given sequence is a concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Driver code
using System ; class GFG { static bool checkPermutation ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int lsum = prefix [ i ] ; int rsum = sum - prefix [ i ] ; int l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = arr . Length ; if ( checkPermutation ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find a N | C # program to find N digit number such that it is not divisible by any of its digits ; Function that print the answer ; if n == 1 then it is not possible ; loop to n - 1 times ; print 4 as last digit of the number ; Driver code ; Function call
using System ; class GFG { static void findTheNumber ( int n ) { if ( n == 1 ) { Console . Write ( " Impossible " + " STRNEWLINE " ) ; return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { Console . Write ( "5" ) ; } Console . Write ( "4" ) ; } public static void Main ( String [ ] args ) { int n = 12 ; findTheNumber ( n ) ; } }
Queries to check whether bitwise AND of a subarray is even or odd | C # implementation of the approach ; Function to precompute the count of even and odd numbers ; If the current element is odd then put 1 at odd [ i ] ; If the current element is even then put 1 at even [ i ] ; Taking the prefix sums of these two arrays so we can get the count of even and odd numbers in a range [ L , R ] in O ( 1 ) ; Function that returns true if the bitwise AND of the subarray a [ L ... R ] is odd ; cnt will store the count of odd numbers in the range [ L , R ] ; Check if all the numbers in the range are odd or not ; Function to perform the queries ; Perform queries ; Driver code ; Queries
using System ; class GFG { static readonly int MAXN = 1000005 ; static int [ ] even = new int [ MAXN ] ; static int [ ] odd = new int [ MAXN ] ; static void precompute ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } static bool isOdd ( int L , int R ) { int cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } static void performQueries ( int [ ] a , int n , int [ , ] q , int m ) { precompute ( a , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i , 0 ] , R = q [ i , 1 ] ; if ( isOdd ( L , R ) ) Console . WriteLine ( " Odd " ) ; else Console . WriteLine ( " Even " ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 1 , 5 , 7 , 6 , 8 , 9 } ; int n = a . Length ; int [ , ] q = { { 0 , 2 } , { 1 , 2 } , { 2 , 3 } , { 3 , 6 } } ; int m = q . GetLength ( 0 ) ; performQueries ( a , n , q , m ) ; } }
Find distinct integers for a triplet with given product | C # implementation of the approach ; Function to find the required triplets ; To store the factors ; Find factors in Math . Sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Print the valid triplet ; Triplet found ; Triplet not found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int x ) { List < int > fact = new List < int > ( ) ; HashSet < int > factors = new HashSet < int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . Add ( i ) ; if ( x / i != i ) fact . Add ( x / i ) ; factors . Add ( i ) ; factors . Add ( x / i ) ; } } bool found = false ; int k = fact . Count ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact [ i ] ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { Console . Write ( a + " ▁ " + b + " ▁ " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int x = 105 ; findTriplets ( x ) ; } }
Count of pairs satisfying the given condition | C # implementation of the approach ; Function to return the number of pairs satisfying the equation ; Converting integer b to String by using to_String function ; Loop to check if all the digits of b are 9 or not ; If '9' doesn 't appear  then break the loop ; If all the digits of b contain 9 then multiply a with String length else multiply a with String length - 1 ; Return the number of pairs ; Driver code
using System ; class GFG { static int countPair ( int a , int b ) { String s = String . Join ( " " , b ) ; int i ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != '9' ) break ; } int result ; if ( i == s . Length ) result = a * s . Length ; else result = a * ( s . Length - 1 ) ; return result ; } public static void Main ( String [ ] args ) { int a = 5 , b = 101 ; Console . Write ( countPair ( a , b ) ) ; } }
Partition the digits of an integer such that it satisfies a given condition | C # implementation of the approach ; Function to generate sequence from the given String ; Initialize vector to store sequence ; First add all the digits of group A from left to right ; Then add all the digits of group B from left to right ; Return the sequence ; Function that returns true if the sequence is non - decreasing ; Initialize result ; Function to partition the digits of an integer such that it satisfies the given conditions ; Convert the integer to String ; Length of the String ; Array to store the digits ; Storing the digits of X in array ; Initialize the result ; Loop through the digits ; Put into group A if digit less than D ; Put into group B if digit greater than D ; Put into group C if digit equal to D ; Loop through the digits to decide for group C digits ; Set flag equal to true if group B digit present ; If flag is true put in group A or else put in B ; Generate the sequence from partition ; Check if the sequence is non decreasing ; Return - 1 if no such partition is possible ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < int > makeSeq ( String s , int [ ] a ) { List < int > seq = new List < int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( s [ i ] == ' A ' ) seq . Add ( a [ i ] ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( s [ i ] == ' B ' ) seq . Add ( a [ i ] ) ; return seq ; } static bool checkSeq ( List < int > v ) { bool check = true ; for ( int i = 1 ; i < v . Count ; i ++ ) if ( v [ i ] < v [ i - 1 ] ) check = false ; return check ; } static String digitPartition ( int X ) { String num = String . Join ( " " , X ) ; int l = num . Length ; int [ ] a = new int [ l ] ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = ( num [ i ] - '0' ) ; for ( int D = 0 ; D < 10 ; D ++ ) { String res = " " ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] < D ) res += ' A ' ; else if ( a [ i ] > D ) res += ' B ' ; else res += ' C ' ; } bool flag = false ; for ( int i = 0 ; i < l ; i ++ ) { if ( res [ i ] == ' B ' ) flag = true ; if ( res [ i ] == ' C ' ) res = res . Substring ( 0 , i ) + ( flag ? ' A ' : ' B ' ) + res . Substring ( i + 1 ) ; } List < int > seq = makeSeq ( res , a ) ; if ( checkSeq ( seq ) ) return res ; } return " - 1" ; } public static void Main ( String [ ] args ) { int X = 777147777 ; Console . Write ( digitPartition ( X ) ) ; } }
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
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 ) ) ; } }
Check if all the elements can be made of same parity by inverting adjacent elements | C # implementation of the approach ; Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
using System ; class GFG { static bool flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = a . Length ; if ( flipsPossible ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Queries On Array with disappearing and reappearing elements | C # implementation of the approach ; Function to perform the queries ; Size of the array with 1 - based indexing ; Number of queries ; Iterating through the queries ; If m is more than the size of the array ; Count of turns ; Find the remainder ; If the remainder is 0 and turn is odd then the array is empty ; If the remainder is 0 and turn is even then array is full and is in its initial state ; If the remainder is not 0 and the turn is even ; Current size of the array ; Current size of the array ; Print the result ; Driver code ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
using System ; using System . Collections . Generic ; class GFG { static void PerformQueries ( int [ ] a , int [ , ] vec ) { List < int > ans = new List < int > ( ) ; int n = ( int ) a . Length - 1 ; int q = ( int ) vec . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; ++ i ) { long t = vec [ i , 0 ] ; int m = vec [ i , 1 ] ; if ( m > n ) { ans . Add ( - 1 ) ; continue ; } int turn = ( int ) ( t / n ) ; int rem = ( int ) ( t % n ) ; if ( rem == 0 && turn % 2 == 1 ) { ans . Add ( - 1 ) ; continue ; } if ( rem == 0 && turn % 2 == 0 ) { ans . Add ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { int cursize = n - rem ; if ( cursize < m ) { ans . Add ( - 1 ) ; continue ; } ans . Add ( a [ m + rem ] ) ; } else { int cursize = rem ; if ( cursize < m ) { ans . Add ( - 1 ) ; continue ; } ans . Add ( a [ m ] ) ; } } foreach ( int i in ans ) Console . Write ( i + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { - 1 , 1 , 2 , 3 , 4 , 5 } ; int [ , ] vec = { { 1 , 4 } , { 6 , 1 } , { 3 , 5 } } ; PerformQueries ( a , vec ) ; } }
Number of pairs in an array having sum equal to product | C # implementation of the approach ; Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code
using System ; class GFG { static int sumEqualProduct ( int [ ] a , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . Length ; Console . Write ( sumEqualProduct ( a , n ) ) ; } }
Minimize the sum of digits of A and B such that A + B = N | C # implementation of the approach ; Function to return the minimum possible sum of digits of A and B such that A + B = n ; Find the sum of digits of n ; If num is a power of 10 ; Driver code
using System ; class GFG { static int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; } public static void Main ( String [ ] args ) { int n = 1884 ; Console . Write ( minSum ( n ) ) ; } }
Multiplication on Array : Range update query in O ( 1 ) | C # implementation of the approach ; Creates a mul [ ] array for A [ ] and returns it after filling initial values . ; Does range update ; Prints updated Array ; Driver code ; ; Array to be updated ; Create and fill mul and div Array
using System ; class GFG { static void initialize ( int [ ] mul , int [ ] div , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } static void update ( int l , int r , int x , int [ ] mul , int [ ] div ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } static void printArray ( int [ ] ar , int [ ] mul , int [ ] div , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; Console . Write ( ar [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] ar = { 10 , 5 , 20 , 40 } ; int n = ar . Length ; int [ ] mul = new int [ n + 1 ] ; int [ ] div = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; } }
Optimal Strategy for a Game | Special Gold Coin | C # implementation of the approach ; Function to return the winner of the game ; To store the count of silver coins ; Update the position of the gold coin ; First player will win the game ; Driver code
using System ; class GFG { static String getWinner ( String str , int len ) { int total = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' S ' ) { total ++ ; } } if ( ( total % 2 ) == 1 ) return " First " ; return " Second " ; } public static void Main ( string [ ] args ) { string str = " GSSS " ; int len = str . Length ; Console . WriteLine ( getWinner ( str , len ) ) ; } }
Find the coordinates of a triangle whose Area = ( S / 2 ) | C # implementation of the above approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code
using System ; class GFG { static readonly long MAX = 1000000000 ; static void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; Console . WriteLine ( " ( " + X1 + " , ▁ " + Y1 + " ) " ) ; Console . WriteLine ( " ( " + X2 + " , ▁ " + Y2 + " ) " ) ; Console . WriteLine ( " ( " + X3 + " , ▁ " + Y3 + " ) " ) ; } public static void Main ( String [ ] args ) { long S = 4 ; findTriangle ( S ) ; } }
Rearrange array elements such that Bitwise AND of first N | C # implementation of the approach ; Utility function to print the elements of an array ; Function to find the required arrangement ; There has to be atleast 2 elements ; Minimum element from the array ; Swap any occurrence of the minimum element with the last element ; Find the bitwise AND of the first ( n - 1 ) elements ; If the bitwise AND is equal to the last element then print the arrangement ; Driver code
using System ; using System . Linq ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void findArrangement ( int [ ] arr , int n ) { if ( n < 2 ) { Console . Write ( " - 1" ) ; return ; } int minVal = arr . Min ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minVal ) { swap ( arr , i , n - 1 ) ; break ; } } int andVal = arr [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { andVal &= arr [ i ] ; } if ( andVal == arr [ n - 1 ] ) printArr ( arr , n ) ; else Console . Write ( " - 1" ) ; } 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 [ ] arr = { 1 , 5 , 3 , 3 } ; int n = arr . Length ; findArrangement ( arr , n ) ; } }
Maximum prime moves to convert X to Y | C # implementation of the approach ; Function to return the maximum operations required to convert X to Y ; X cannot be converted to Y ; If the difference is 1 ; If the difference is even ; Add 3 to X and the new difference will be even ; Driver code
using System ; class GFG { static int maxOperations ( int X , int Y ) { if ( X > Y ) return - 1 ; int diff = Y - X ; if ( diff == 1 ) return - 1 ; if ( diff % 2 == 0 ) return ( diff / 2 ) ; return ( 1 + ( ( diff - 3 ) / 2 ) ) ; } public static void Main ( String [ ] args ) { int X = 5 , Y = 16 ; Console . WriteLine ( maxOperations ( X , Y ) ) ; } }