codeparrot/xlcost-text-to-code huggingface dataset:
Print alternate elements of an array | C # program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; If currIndex stores even index or odd position ; Driver Code
using System ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex ++ ) { if ( currIndex % 2 == 0 ) { Console . Write ( arr [ currIndex ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; printAlter ( arr , N ) ; } }
Minimize deletions in a Binary String to remove all subsequences of the form "0101" | C # Program to implement the above approach ; Function to find minimum characters to be removed such that no subsequence of the form "0101" exists in the string ; Stores the partial sums ; Calculate partial sums ; Setting endpoints and deleting characters indices . ; Return count of deleted characters ; Driver Code
using System ; class GFG { static int findmin ( string s ) { int n = s . Length ; int i , j , maximum = 0 ; int [ ] incr = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { incr [ i + 1 ] = incr [ i ] ; if ( s [ i ] == '0' ) { incr [ i + 1 ] ++ ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { maximum = Math . Max ( maximum , incr [ i ] + j - i + 1 - ( incr [ j + 1 ] - incr [ i ] ) + incr [ n ] - incr [ j + 1 ] ) ; } } return n - maximum ; } public static void Main ( ) { string S = "0110100110" ; int minimum = findmin ( S ) ; Console . WriteLine ( minimum ) ; } }
Length of longest subarray with product equal to a power of 2 | C # program for the above approach ; Function to check whether a number is power of 2 or not ; Function to find maximum length subarray having product of element as a perfect power of 2 ; Stores current subarray length ; Stores maximum subarray length ; Traverse the given array ; If arr [ i ] is power of 2 ; Increment max_length ; Update max_len_subarray ; Otherwise ; Print the maximum length ; Driver Code ; Given arr [ ] ; Function Call
using System ; class GFG { public static bool isPower ( int x ) { if ( x != 0 && ( x & ( x - 1 ) ) == 0 ) return true ; return false ; } public static void maximumlength ( int [ ] arr , int N ) { int max_length = 0 ; int max_len_subarray = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( isPower ( arr [ i ] ) ) { max_length ++ ; max_len_subarray = Math . Max ( max_length , max_len_subarray ) ; } else { max_length = 0 ; } } Console . WriteLine ( max_len_subarray ) ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 4 , 6 , 8 , 8 , 2 } ; int N = arr . Length ; maximumlength ( arr , N ) ; } }
Length of smallest subarray to be removed such that the remaining array is sorted | C # program for the above approach ; Find the length of the shortest subarray ; To store the result ; Calculate the possible length of the sorted subarray from left ; Array is sorted ; Calculate the possible length of the sorted subarray from left ; Update the result ; Calculate the possible length in the middle we can delete and update the result ; Update the result ; Return the result ; Driver Code ; Function call
using System ; class GFG { static int findLengthOfShortestSubarray ( int [ ] arr , int N ) { int minlength = int . MaxValue ; int left = 0 ; int right = N - 1 ; while ( left < right && arr [ left + 1 ] >= arr [ left ] ) { left ++ ; } if ( left == N - 1 ) return 0 ; while ( right > left && arr [ right - 1 ] <= arr [ right ] ) { right -- ; } minlength = Math . Min ( N - left - 1 , right ) ; int j = right ; for ( int i = 0 ; i < left + 1 ; i ++ ) { if ( arr [ i ] <= arr [ j ] ) { minlength = Math . Min ( minlength , j - i - 1 ) ; } else if ( j < N - 1 ) { j ++ ; } else { break ; } } return minlength ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 } ; int N = arr . Length ; Console . Write ( findLengthOfShortestSubarray ( arr , N ) ) ; } }
Smallest number to be subtracted to convert given number to a palindrome | C # program for the above approach ; Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call
using System ; class GFG { static void minSub ( int N ) { int count = 0 ; while ( N >= 0 ) { int num = N ; int rev = 0 ; while ( num != 0 ) { int digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = num / 10 ; } if ( N == rev ) { break ; } count ++ ; N -- ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int N = 3456 ; minSub ( N ) ; } }
Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | C # program for the above approach ; Function to count all the subarrays of size at least 3 forming GP ; If array size is less than 3 ; Stores the count of subarray ; Stores the count of subarray for each iteration ; Traverse the array ; Check if L [ i ] forms GP ; Otherwise , update count to 0 ; Update the final count ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call
using System ; class GFG { static int numberOfGP ( int [ ] L , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int i = 2 ; i < N ; ++ i ) { if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 16 , 24 } ; int N = arr . Length ; Console . Write ( numberOfGP ( arr , N ) ) ; } }
Count even length subarrays having bitwise XOR equal to 0 | C # program to implement the above approach ; Function to count the number of even - length subarrays having Bitwise XOR equal to 0 ; Stores the count of required subarrays ; Stores prefix - XOR of arr [ i , i + 1 , ... N - 1 ] ; Traverse the array ; Calculate the prefix - XOR of current subarray ; Check if XOR of the current subarray is 0 and length is even ; Driver Code
using System ; class GFG { static int cntSubarr ( int [ ] arr , int N ) { int res = 0 ; int prefixXor = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixXor = arr [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { prefixXor ^= arr [ j ] ; if ( prefixXor == 0 && ( j - i + 1 ) % 2 == 0 ) { res ++ ; } } } return res ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . Length ; Console . WriteLine ( cntSubarr ( arr , N ) ) ; } }
Count even length subarrays having bitwise XOR equal to 0 | C # program to implement the above approach ; Function to get the count of even length subarrays having bitwise xor 0 ; Stores prefix - xor of the given array ; Stores prefix - xor at even index of the array . ; Stores prefix - xor at odd index of the array . ; Stores count of subarrays that satisfy the condition ; length from 0 index to odd index is even ; Traverse the array . ; Take prefix - xor ; If index is odd ; Calculate pairs ; Increment prefix - xor at odd index ; Calculate pairs ; Increment prefix - xor at odd index ; Driver Code
using System ; class GFG { static readonly int M = 1000000 ; static int cntSubXor ( int [ ] arr , int N ) { int prefixXor = 0 ; int [ ] Even = new int [ M ] ; int [ ] Odd = new int [ M ] ; int cntSub = 0 ; Odd [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { prefixXor ^= arr [ i ] ; if ( i % 2 == 1 ) { cntSub += Odd [ prefixXor ] ; Odd [ prefixXor ] ++ ; } else { cntSub += Even [ prefixXor ] ; Even [ prefixXor ] ++ ; } } return cntSub ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . Length ; Console . Write ( cntSubXor ( arr , N ) ) ; } }
Maximize sum of array elements removed by performing the given operations | C # program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { List < List < int > > A = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . Add ( new List < int > ( ) ) ; A [ i ] . Add ( arr [ i ] ) ; A [ i ] . Add ( min [ i ] ) ; } A = A . OrderBy ( lst => lst [ 0 ] ) . ToList ( ) ; A . Reverse ( ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . Max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) ; K += k ; } } static public void Main ( ) { int [ ] arr = { 3 , 5 , 2 , 1 } ; int [ ] min = { 3 , 2 , 1 , 3 } ; int N = arr . Length ; int K = 3 ; S = 0 ; findMaxSum ( arr , N , min , K ) ; Console . WriteLine ( S ) ; } }
Count smaller primes on the right of each array element | C # program for the above approach ; Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
using System ; class GFG { static bool is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countSmallerPrimes ( int [ ] ar , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } Console . Write ( count + " ▁ " ) ; } } public static void Main ( ) { int [ ] ar = { 43 , 3 , 5 , 7 , 2 , 41 } ; int N = ar . Length ; countSmallerPrimes ( ar , N ) ; } }
Check if a string is concatenation of another given string | C # program to implement the above approach ; Function to check if a String is concatenation of another String ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the Strings ; If str1 is not concatenation of str2 ; Driver Code
using System ; class GFG { static bool checkConcat ( String str1 , String str2 ) { int N = str1 . Length ; int M = str2 . Length ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] != str2 [ i % M ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str1 = " abcabcabc " ; String str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Minimize length of string by replacing K pairs of distinct adjacent characters | C # program to implement the above approach ; Function to minimize the length of the String by replacing distinct pairs of adjacent characters ; Stores the length of the given String ; Stores the index of the given String ; Traverse the given String ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
using System ; class GFG { static int MinLen ( String str , int K ) { int N = str . Length ; int i = 0 ; while ( i < N - 1 ) { if ( str [ i ] != str [ i + 1 ] ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . Max ( 1 , N - K ) ; } public static void Main ( String [ ] args ) { String str = " aabc " ; int K = 1 ; Console . Write ( MinLen ( str , K ) ) ; } }
Length of smallest sequence having sum X and product Y | C # program for the above approach ; Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
using System ; class GFG { static double temp ( int n , int x ) { return Math . Pow ( x * 1.0 / n , n ) ; } static bool check ( int n , int y , int x ) { double v = temp ( n , x ) ; return ( v >= y ) ; } static void find ( int x , int y ) { int high = ( int ) Math . Floor ( x / Math . Exp ( 1.0 ) ) ; int low = 1 ; if ( x == y ) Console . Write ( 1 + " STRNEWLINE " ) ; else if ( ! check ( high , y , x ) ) Console . Write ( - 1 + " STRNEWLINE " ) ; else { while ( high - low > 1 ) { int mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } Console . Write ( high + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int x = 9 , y = 8 ; find ( x , y ) ; } }
Print the middle nodes of each level of a Binary Tree | C # program for the above approach ; Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Driver Code ; Given Tree ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static Dictionary < int , ArrayList > M ; class node { public int data ; public node left ; public node right ; public node ( int data , node left , node right ) { this . data = data ; this . left = left ; this . right = right ; } } ; static node newnode ( int d ) { node temp = new node ( d , null , null ) ; return temp ; } static void dfs ( node root , int l ) { if ( root == null ) return ; if ( ! M . ContainsKey ( l ) ) { ArrayList temp = new ArrayList ( ) ; temp . Add ( root . data ) ; M [ l ] = temp ; } else M [ l ] . Add ( root . data ) ; dfs ( root . left , l + 1 ) ; dfs ( root . right , l + 1 ) ; } static void printMidNodes ( node root ) { M = new Dictionary < int , ArrayList > ( ) ; dfs ( root , 0 ) ; foreach ( KeyValuePair < int , ArrayList > it in M ) { int size = it . Value . Count ; if ( size % 2 == 1 ) { Console . Write ( it . Value [ ( size - 1 ) / 2 ] + " STRNEWLINE " ) ; } else { Console . Write ( it . Value [ ( size - 1 ) / 2 ] + " ▁ " + it . Value [ ( ( size - 1 ) / 2 ) + 1 ] + " STRNEWLINE " ) ; } } } public static void Main ( string [ ] args ) { node root = newnode ( 1 ) ; root . left = newnode ( 2 ) ; root . right = newnode ( 3 ) ; root . left . left = newnode ( 4 ) ; root . left . right = newnode ( 5 ) ; root . left . right . left = newnode ( 11 ) ; root . left . right . right = newnode ( 6 ) ; root . left . right . right . left = newnode ( 7 ) ; root . left . right . right . right = newnode ( 9 ) ; root . right . left = newnode ( 10 ) ; root . right . right = newnode ( 8 ) ; printMidNodes ( root ) ; } }
Smallest array that can be obtained by replacing adjacent pairs with their products | C # implementation of the above approach ; Function to minimize the size of array by performing given operations ; If all array elements are not same ; If all array elements are same ; Driver Code ; Function call
using System ; class GFG { static int minLength ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return 1 ; } } return N ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 3 , 1 } ; int N = arr . Length ; Console . Write ( minLength ( arr , N ) ) ; } }
Maximum XOR of Two Numbers in an Array | Set 2 | C # program for the above approach ; Structure of Tree ; Function to insert binary representation of element x in the Tree ; Store the head ; Find the i - th bit ; If curr . left is null ; Update curr to curr . left ; If curr . right is null ; Update curr to curr . right ; Function that finds the maximum Bitwise XOR value for all such pairs ; Head Node of Tree ; Insert each element in tree ; Stores the maximum XOR value ; Traverse the given array ; Stores the XOR with current value arr [ i ] ; Finding ith bit ; Check if the bit is 0 ; If right node exists ; Update the currentXOR ; Check if left node exists ; Update the currentXOR ; Update M to M / 2 for next set bit ; Update the maximum XOR ; Return the maximum XOR found ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { public class node { public node left ; public node right ; } ; static void insert ( int x , node head ) { node curr = head ; for ( int i = 30 ; i >= 0 ; i -- ) { int val = ( x >> i ) & 1 ; if ( val == 0 ) { if ( curr . left == null ) curr . left = new node ( ) ; curr = curr . left ; } else { if ( curr . right == null ) curr . right = new node ( ) ; curr = curr . right ; } } } static int findMaximumXOR ( int [ ] arr , int n ) { node head = new node ( ) ; for ( int i = 0 ; i < n ; i ++ ) { insert ( arr [ i ] , head ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; int M = ( int ) Math . Pow ( 2 , 30 ) ; node curr = head ; for ( int j = 30 ; j >= 0 ; j -- ) { int val = ( arr [ i ] >> j ) & 1 ; if ( val == 0 ) { if ( curr . right != null ) { curr_xor += M ; curr = curr . right ; } else { curr = curr . left ; } } else { if ( curr . left != null ) { curr_xor += M ; curr = curr . left ; } else { curr = curr . right ; } } M /= 2 ; } ans = Math . Max ( ans , curr_xor ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( findMaximumXOR ( arr , N ) ) ; } }
Subarrays whose sum is a perfect square | C # program to implement the above approach ; Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver code
using System ; class GFG { static void PrintIndexes ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int currSubSum = 0 ; for ( int j = i ; j < N ; j ++ ) { currSubSum += arr [ j ] ; int sq = ( int ) Math . Sqrt ( currSubSum ) ; if ( sq * sq == currSubSum ) { Console . Write ( " ( " + i + " , " + j + " ) " + " ▁ " ) ; } } } } public static void Main ( String [ ] args ) { int [ ] arr = { 65 , 79 , 81 } ; PrintIndexes ( arr , arr . Length ) ; } }
Count positions in Binary Matrix having equal count of set bits in corresponding row and column | C # Program to implement above approach ; Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
using System ; class GFG { static int countPosition ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int [ ] row = new int [ n ] ; int [ ] col = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i , j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 } , { 1 , 1 } } ; Console . WriteLine ( countPosition ( mat ) ) ; } }
Maximum value at each level in an N | C # program for the above approach ; Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Driver Code ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void maxAtLevel ( int N , int M , int [ ] Value , int [ , ] Edges ) { List < int > [ ] adj = new List < int > [ N ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; } Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 0 ) ; while ( q . Count != 0 ) { int count = q . Count ; int maxVal = 0 ; while ( count -- > 0 ) { int temp = q . Peek ( ) ; q . Dequeue ( ) ; maxVal = Math . Max ( maxVal , Value [ temp ] ) ; for ( int i = 0 ; i < adj [ temp ] . Count ; i ++ ) { q . Enqueue ( adj [ temp ] [ i ] ) ; } } Console . Write ( maxVal + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 10 ; int [ , ] Edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; int [ ] Value = { 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAtLevel ( N , N - 1 , Value , Edges ) ; } }
Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | C # program to implement the above approach ; Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
using System ; class GFG { static void ctTriplets ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int [ ] temp = new int [ N ] ; int index = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } for ( int i = 0 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } int ct = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && i + 1 < N ) { if ( temp [ i ] < temp [ i + 1 ] && temp [ i ] < temp [ i - 1 ] ) { ct ++ ; } } } Console . WriteLine ( " Count ▁ of ▁ triplets : " + ct ) ; Console . Write ( " Array : " ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( temp [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; ctTriplets ( arr , N ) ; } }
Rearrange array to make it non | C # program for above approach ; Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Print the answer
using System ; class GFG { static bool check ( int [ ] a , int n ) { int [ ] b = new int [ n ] ; int minElement = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = Math . Min ( minElement , a [ i ] ) ; } Array . Sort ( b ) ; int k = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } static public void Main ( ) { int [ ] a = { 4 , 3 , 6 , 6 , 2 , 9 } ; int n = a . Length ; if ( check ( a , n ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }