codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Driver code ; Given array
using System ; class GFG { public static bool isSorted ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } public static bool sortPoss ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int idx = - 1 ; int minVar = arr [ i ] ; int j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != - 1 ) { int t ; t = arr [ i ] ; arr [ i ] = arr [ idx ] ; arr [ idx ] = t ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 1 , 2 , 6 } ; int n = arr . Length ; if ( sortPoss ( arr , n ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Print all array elements appearing more than N / K times | C # program to implement the above approach ; Function to + find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to print all array elements whose frequency is greater than N / K ; Sort the array arr [ ] ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
using System ; class GFG { static int upperBound ( int [ ] arr , int N , int K ) { int l = 0 ; int r = N ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( arr [ mid ] <= K ) { l = mid + 1 ; } else { r = mid ; } } return l ; } static void NDivKWithFreq ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int i = 0 ; while ( i < N ) { int X = upperBound ( arr , N , arr [ i ] ) ; if ( ( X - i ) > N / 4 ) { Console . Write ( arr [ i ] + " ▁ " ) ; } i = X ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = arr . Length ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; } }
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | C # program for the above approach ; Function to find maximum possible sum after changing the array elements as per the given constraints ; Length of given array ; Stores the frequency of elements in given array ; Update frequency ; stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static long maxSum ( int [ ] a ) { int n = a . Length ; int [ ] count = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ Math . Min ( a [ i ] , n ) ] ++ ; int size = 0 ; long ans = 0 ; for ( int k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 1 } ; Console . Write ( maxSum ( arr ) ) ; } }
Find sum of product of every number and its frequency in given range | C # program to find sum of product of every number and square of its frequency in the given range ; Function to solve queries ; Calculating answer for every query ; The end points of the ith query ; Hashmap for storing frequency ; Incrementing the frequency ; Adding the contribution of ith number ; Print answer ; Driver code ; Calling function
using System ; using System . Collections . Generic ; class GFG { public static void answerQueries ( int [ ] arr , int n , int [ , ] queries ) { for ( int i = 0 ; i < queries . GetLength ( 0 ) ; i ++ ) { int ans = 0 ; int l = queries [ i , 0 ] - 1 ; int r = queries [ i , 1 ] - 1 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int j = l ; j < r + 1 ; j ++ ) { freq [ arr [ j ] ] = freq . GetValueOrDefault ( arr [ j ] , 0 ) + 1 ; } foreach ( int k in freq . Keys ) { ans += k * freq [ k ] ; } Console . WriteLine ( ans ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 1 } ; int n = arr . Length ; int [ , ] queries = { { 1 , 2 } , { 1 , 3 } } ; answerQueries ( arr , n , queries ) ; } }
Pandigital Product | C # code to check the number is Pandigital Product or not . ; calculate the multiplicand , multiplier , and product eligible for pandigital ; To check the string formed from multiplicand multiplier and product is pandigital ; Driver function
using System ; class GFG { public static bool PandigitalProduct_1_9 ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 && isPandigital ( " " + n + i + n / i ) ) return true ; return false ; } public static bool isPandigital ( String str ) { if ( str . Length != 9 ) return false ; char [ ] ch = str . ToCharArray ( ) ; Array . Sort ( ch ) ; return new String ( ch ) . Equals ( "123456789" ) ; } public static void Main ( ) { int n = 6952 ; if ( PandigitalProduct_1_9 ( n ) == true ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }
Median and Mode using Counting Sort | C # Program for Mode and Median using Counting Sort technique ; function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver Code
using System ; using System . Linq ; class GFG { static void printModeMedian ( int [ ] a , int n ) { int [ ] b = new int [ n ] ; int max = a . Max ( ) ; int t = max + 1 ; int [ ] count = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { count [ a [ i ] ] ++ ; } int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } for ( int i = 1 ; i < t ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { b [ count [ a [ i ] ] - 1 ] = a [ i ] ; count [ a [ i ] ] -- ; } float median ; if ( n % 2 != 0 ) { median = b [ n / 2 ] ; } else { median = ( float ) ( ( b [ ( n - 1 ) / 2 ] + b [ ( n / 2 ) ] ) / 2.0 ) ; } Console . WriteLine ( " median ▁ = ▁ " + median ) ; Console . WriteLine ( " mode ▁ = ▁ " + mode ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . Length ; printModeMedian ( a , n ) ; } }
Check if both halves of the string have at least one different character | C # implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function
using System ; class GeeksforGeeks { static int MAX = 26 ; static bool function ( String str ) { int l = str . Length ; int [ ] counter1 = new int [ MAX ] ; int [ ] counter2 = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { counter1 [ i ] = 0 ; counter2 [ i ] = 0 ; } for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( counter2 [ i ] != counter1 [ i ] ) return true ; } return false ; } public static void Main ( ) { String str = " abcasdsabcae " ; if ( function ( str ) ) Console . WriteLine ( " Yes , ▁ both ▁ halves ▁ " + " differ ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else Console . WriteLine ( " No , ▁ both ▁ halves ▁ " + " do ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Check if both halves of the string have at least one different character | C # implementation of the problem ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
using System ; class GFG { static int MAX = 26 ; static bool function ( String str ) { int l = str . Length ; int [ ] counter = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) counter [ i ] = 0 ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter [ str [ i ] - ' a ' ] -- ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter [ i ] != 0 ) return true ; return false ; } public static void Main ( ) { string str = " abcasdsabcae " ; if ( function ( str ) ) Console . Write ( " Yes , ▁ both ▁ halves " + " ▁ differ ▁ by ▁ at ▁ least ▁ one ▁ " + " character " ) ; else Console . Write ( " No , ▁ both ▁ halves " + " ▁ do ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Minimum difference between max and min of all K | C # program to find minimum difference between max and min of all subset of K size ; returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver code
using System ; class GFG { static int minDifferenceAmongMaxMin ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int res = 2147483647 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { int curSeqDiff = arr [ i + K - 1 ] - arr [ i ] ; res = Math . Min ( res , curSeqDiff ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 100 , 101 , 102 } ; int N = arr . Length ; int K = 3 ; Console . Write ( minDifferenceAmongMaxMin ( arr , N , K ) ) ; } }
Position of an element after stable sort | C # program to get index of array element in sorted array ; Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver code to test above methods
using System ; class ArrayIndex { static int getIndexInSortedArray ( int [ ] arr , int n , int idx ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ idx ] ) result ++ ; if ( arr [ i ] == arr [ idx ] && i < idx ) result ++ ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 } ; int n = arr . Length ; int idxOfEle = 5 ; Console . WriteLine ( getIndexInSortedArray ( arr , n , idxOfEle ) ) ; } }
Find permutation of numbers upto N with a specific sum in a specific range | C # program for the above approach ; Function to check if sum is possible with remaining numbers ; Stores the minimum sum possible with x numbers ; Stores the maximum sum possible with x numbers ; If S lies in the range [ minSum , maxSum ] ; Function to find the resultant permutation ; Stores the count of numbers in the given segment ; If the sum is not possible with numbers in the segment ; Output - 1 ; Stores the numbers present within the given segment ; Iterate over the numbers from 1 to N ; If ( S - i ) is a positive non - zero sum and if it is possible to obtain ( S - i ) remaining numbers ; Update sum S ; Update required numbers in the segement ; Push i in vector v ; If sum has been obtained ; Break from the loop ; If sum is not obtained ; Output - 1 ; Stores the numbers which are not present in given segment ; Loop to check the numbers not present in the segment ; Pointer to check if i is present in vector v or not ; If i is not present in v ; Push i in vector v1 ; Point to the first elements of v1 and v respectively ; Print the required permutation ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static bool possible ( int x , int S , int N ) { int minSum = ( x * ( x + 1 ) ) / 2 ; int maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) / 2 ; if ( S < minSum S > maxSum ) { return false ; } return true ; } public static void findPermutation ( int N , int L , int R , int S ) { int x = R - L + 1 ; if ( ! possible ( x , S , N ) ) { Console . WriteLine ( - 1 ) ; return ; } else { List < int > v = new List < int > ( ) ; for ( int i = N ; i >= 1 ; -- i ) { if ( ( S - i ) >= 0 && possible ( x - 1 , S - i , i - 1 ) ) { S = S - i ; x -- ; v . Add ( i ) ; } if ( S == 0 ) { break ; } } if ( S != 0 ) { Console . WriteLine ( - 1 ) ; return ; } List < int > v1 = new List < int > ( ) ; for ( int i = 1 ; i <= N ; ++ i ) { bool it = v . Contains ( i ) ; if ( ! it ) { v1 . Add ( i ) ; } } int j = 0 , f = 0 ; for ( int i = 1 ; i < L ; ++ i ) { Console . Write ( v1 [ j ] + " ▁ " ) ; j ++ ; } for ( int i = L ; i <= R ; ++ i ) { Console . Write ( v [ f ] + " ▁ " ) ; f ++ ; } for ( int i = R + 1 ; i <= N ; ++ i ) { Console . Write ( v1 [ j ] + " ▁ " ) ; j ++ ; } } return ; } public static void Main ( ) { int N = 6 , L = 3 , R = 5 , S = 8 ; findPermutation ( N , L , R , S ) ; } }
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | C # program for the above approach ; Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code
using System ; class GFG { static int countTriplets ( int N , int K ) { if ( K % 2 == 0 ) { int x = N / K ; int y = ( N + ( K / 2 ) ) / K ; return x * x * x + y * y * y ; } else { int x = N / K ; return x * x * x ; } } static void Main ( ) { int N = 2 , K = 2 ; Console . Write ( countTriplets ( N , K ) ) ; } }
Maximize count of 3 | C # program to implement the above approach ; Driver Code ; Function to count the maximum number of palindrome subsequences of length 3 considering the same index only once ; Index of the string S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value
using System ; class GFG { public static void Main ( String [ ] args ) { string S = " geeksforg " ; Console . WriteLine ( maxNumPalindrome ( S ) ) ; } static int maxNumPalindrome ( string S ) { int i = 0 ; int [ ] freq = new int [ 26 ] ; int freqPair = 0 ; int len = S . Length / 3 ; while ( i < S . Length ) { freq [ S [ i ] - ' a ' ] ++ ; i ++ ; } for ( i = 0 ; i < 26 ; i ++ ) { freqPair += ( freq [ i ] / 2 ) ; } return Math . Min ( freqPair , len ) ; } }
Minimum Bipartite Groups | Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Drivers Code ; Adjacency List ; Adding edges to List
using System ; using System . Collections . Generic ; class GFG { static int height ( int s , List < int > [ ] adj , int [ ] visited ) { visited [ s ] = 1 ; int h = 0 ; foreach ( int child in adj [ s ] ) { if ( visited [ child ] == 0 ) { h = Math . Max ( h , 1 + height ( child , adj , visited ) ) ; } } return h ; } static int minimumGroups ( List < int > [ ] adj , int N ) { int [ ] visited = new int [ N + 1 ] ; int groups = int . MinValue ; for ( int i = 1 ; i <= N ; i ++ ) { if ( visited [ i ] == 0 ) { int comHeight ; comHeight = height ( i , adj , visited ) ; groups = Math . Max ( groups , comHeight ) ; } } return groups ; } static void addEdge ( List < int > [ ] adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } public static void Main ( String [ ] args ) { int N = 5 ; List < int > [ ] adj = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj [ i ] = new List < int > ( ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 4 , 3 ) ; Console . Write ( minimumGroups ( adj , N ) ) ; } }
Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | C # implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int ans ; static void subsetSum ( List < int > c ) { int L = c . Count ; int mul = ( int ) Math . Pow ( 2 , L - 1 ) ; for ( int i = 0 ; i < c . Count ; i ++ ) ans += c [ i ] * mul ; } static void subsetGen ( int [ ] arr , int i , int n , List < int > c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . Add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . RemoveAt ( 0 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . Length ; List < int > c = new List < int > ( ) ; subsetGen ( arr , 0 , n , c ) ; Console . WriteLine ( ans ) ; } }
Find the maximum possible value of a [ i ] % a [ j ] over all pairs of i and j | C # implementation of the approach ; Function that returns the second largest element in the array if exists , else 0 ; There must be at least two elements ; To store the maximum and the second maximum element from the array ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; No second maximum found ; Driver code
using System ; class GFG { static int getMaxValue ( int [ ] arr , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { return 0 ; } first = second = int . MinValue ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) { second = arr [ i ] ; } } if ( second == int . MinValue ) { return 0 ; } else { return second ; } } public static void Main ( ) { int [ ] arr = { 4 , 5 , 1 , 8 } ; int n = arr . Length ; Console . Write ( getMaxValue ( arr , n ) ) ; } }
Maximize the value of the given expression | C # implementation of the approach ; Function to return the maximum result ; To store the count of negative integers ; Sum of all the three integers ; Product of all the three integers ; To store the smallest and the largest among all the three integers ; Calculate the count of negative integers ; Depending upon count of negatives ; When all three are positive integers ; For single negative integer ; For two negative integers ; For three negative integers ; Driver Code
using System ; class GFG { static int maximumResult ( int a , int b , int c ) { int countOfNegative = 0 ; int sum = a + b + c ; int product = a * b * c ; int largest = ( a > b ) ? ( ( a > c ) ? a : c ) : ( ( b > c ) ? b : c ) ; int smallest = ( a < b ) ? ( ( a < c ) ? a : c ) : ( ( b < c ) ? b : c ) ; if ( a < 0 ) countOfNegative ++ ; if ( b < 0 ) countOfNegative ++ ; if ( c < 0 ) countOfNegative ++ ; switch ( countOfNegative ) { case 0 : return ( sum - largest ) * largest ; case 1 : return ( product / smallest ) + smallest ; case 2 : return ( product / largest ) + largest ; case 3 : return ( sum - smallest ) * smallest ; } return - 1 ; } static void Main ( ) { int a = - 2 , b = - 1 , c = - 4 ; Console . WriteLine ( maximumResult ( a , b , c ) ) ; } }
Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | C # Implementation of above approach . ; Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code
using System ; using System . Collections . Generic ; using System . Collections ; using System . Linq ; class GFG { static int check ( int n , List < int > marks ) { int x = marks . Max ( ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks [ i ] + bonus >= 50 ) c += 1 ; } return c ; } public static void Main ( ) { int n = 5 ; List < int > marks = new List < int > ( new int [ ] { 0 , 21 , 83 , 45 , 64 } ) ; Console . WriteLine ( check ( n , marks ) ) ; } }
Sum of first N natural numbers which are not powers of K | C # program to find the sum of first n natural numbers which are not positive powers of k ; Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code
using System ; class GFG { static int find_sum ( int n , int k ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int power = k ; while ( power <= n ) { total_sum -= power ; power *= k ; } return total_sum ; } public static void Main ( ) { int n = 11 , k = 2 ; Console . WriteLine ( find_sum ( n , k ) ) ; } }
Minimum number of operations required to delete all elements of the array | C # implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program
using System ; public class Solution { public const int MAX = 10000 ; public static int [ ] hashTable = new int [ MAX ] ; public static int minOperations ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ arr [ i ] ] ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] != 0 ) { for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) { hashTable [ arr [ j ] ] = 0 ; } } res ++ ; } } return res ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 } ; int n = arr . Length ; Console . Write ( minOperations ( arr , n ) ) ; } }
Sorting array with reverse around middle | C # Program to answer queries on sum of sum of odd number digits of all the factors of a number ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; Driver code
using System ; class GFG { static bool ifPossible ( int [ ] arr , int n ) { int [ ] cp = new int [ n ] ; Array . Copy ( arr , cp , n ) ; Array . Sort ( cp ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == cp [ i ] ) && ! ( arr [ n - 1 - i ] == cp [ i ] ) ) return false ; } return true ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 } ; int n = arr . Length ; if ( ifPossible ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }