codeparrot/xlcost-text-to-code huggingface dataset:
Selection Sort VS Bubble Sort | C # program for the above approach ; Driver Code
using System ; public class GFG { static void Bubble_Sort ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; } } } } static public void Main ( ) { int n = 5 ; int [ ] arr = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; Console . Write ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | C # implementation of the above approach ; Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Stores all the array elements in sorted order ; Traverse the array ; Insert current element into multiset ; Perform each operation ; Stores smallest element of ms ; Stores the largest element of ms ; Stores updated value of smallest element of ms by given operations ; Stores updated value of largest element of ms by given operations ; If the value of a + b less than the sum of smallest and largest array element ; Erase the smallest element ; Erase the largest element ; Insert the updated value of the smallest element ; Insert the updated value of the smallest element ; Stores sum of array elements ; Traverse the multiset ; Update ans ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int minimum_possible_sum ( int [ ] arr , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } List < int > ms = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ms . Add ( arr [ i ] ) ; } ms . Sort ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = ms [ 0 ] ; int largest_element = ms [ ms . Count - 1 ] ; int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { ms . RemoveAt ( 0 ) ; ms . RemoveAt ( ms . Count - 1 ) ; ms . Add ( a ) ; ms . Add ( b ) ; ms . Sort ( ) ; } } int ans = 0 ; foreach ( int x in ms ) { ans += x ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = arr . Length ; Console . Write ( minimum_possible_sum ( arr , n , K ) ) ; } }
Lexicographically smallest subsequence possible by removing a character from given string | C # program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given String S ; Function Call
using System ; class GFG { static void firstSubsequence ( String s ) { int isMax = - 1 ; for ( int i = 0 ; i < s . Length - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s = s . Substring ( 0 , isMax ) + s . Substring ( isMax + 1 ) ; s . rerase ( isMax , 1 ) ; } else { s . erase ( s . Length - 1 , 1 ) ; s = s . Substring ( 0 , s . Length - 1 ) ; } Console . Write ( s ) ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; firstSubsequence ( S ) ; } }
Maximize product of array by replacing array elements with its sum or product with element from another array | C # program for the above approach ; Function to find the largest product of array A [ ] ; Base Case ; Store all the elements of the array A [ ] ; Sort the Array B [ ] ; Traverse the array B [ ] ; Pop minimum element ; Check which operation is producing maximum element ; Insert resultant element into the priority queue ; Evaluate the product of the elements of A [ ] ; Return the maximum product ; Driver Code ; Given arrays ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static int largeProduct ( int [ ] A , int [ ] B , int N ) { if ( N == 0 ) { return 0 ; } List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( A [ i ] ) ; } Array . Sort ( B ) ; pq . Sort ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int min = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int maximized_element = Math . Max ( min * B [ i ] , min + B [ i ] ) ; pq . Add ( maximized_element ) ; pq . Sort ( ) ; } int max_product = 1 ; while ( pq . Count > 0 ) { max_product *= pq [ 0 ] ; pq . RemoveAt ( 0 ) ; } return max_product ; } static public void Main ( ) { int [ ] A = { 1 , 1 , 10 } ; int [ ] B = { 1 , 1 , 1 } ; int N = 3 ; Console . WriteLine ( largeProduct ( A , B , N ) ) ; } }
Print all root to leaf paths with there relative positions | C # program to print all root to leaf paths with there relative position ; tree structure ; Function create new node ; Store path information ; Prints given root to leaf path with underscores ; Find the minimum horizontal distance value in current root to leaf path ; Find minimum horizontal distance ; Print the root to leaf path with " _ " that indicate the related position ; Current tree node ; Print underscore ; Print current key ; A utility function print all path from root to leaf working of this function is similar to function of " Print _ vertical _ order " : Print paths of binary tree in vertical order www . geeksforgeeks . org / print - binary - tree - vertical - order - set - 2 / https : ; Base case ; Leaf node ; Add leaf node and then print path ; Store current path information ; Call left sub_tree ; Call left sub_tree ; Base case ; Driver code
using System ; using System . Collections . Generic ; public class Graph { static readonly int MAX_PATH_SIZE = 1000 ; public class Node { public char data ; public Node left , right ; } ; static Node newNode ( char data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public class PATH { public int Hd ; public char key ; public PATH ( int Hd , char key ) { this . Hd = Hd ; this . key = key ; } public PATH ( ) { } } ; static void printPath ( List < PATH > path , int size ) { int minimum_Hd = int . MaxValue ; PATH p ; for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; minimum_Hd = Math . Min ( minimum_Hd , p . Hd ) ; } for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; int noOfUnderScores = Math . Abs ( p . Hd - minimum_Hd ) ; for ( int i = 0 ; i < noOfUnderScores ; i ++ ) Console . Write ( " _ " ) ; Console . WriteLine ( p . key ) ; } Console . WriteLine ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = " ) ; } static void printAllPathsUtil ( Node root , List < PATH > AllPath , int HD , int order ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { AllPath [ order ] = new PATH ( HD , root . data ) ; printPath ( AllPath , order + 1 ) ; return ; } AllPath [ order ] = new PATH ( HD , root . data ) ; printAllPathsUtil ( root . left , AllPath , HD - 1 , order + 1 ) ; printAllPathsUtil ( root . right , AllPath , HD + 1 , order + 1 ) ; } static void printAllPaths ( Node root ) { if ( root == null ) return ; List < PATH > Allpaths = new List < PATH > ( ) ; for ( int i = 0 ; i < MAX_PATH_SIZE ; i ++ ) { Allpaths . Add ( new PATH ( ) ) ; } printAllPathsUtil ( root , Allpaths , 0 , 0 ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( ' A ' ) ; root . left = newNode ( ' B ' ) ; root . right = newNode ( ' C ' ) ; root . left . left = newNode ( ' D ' ) ; root . left . right = newNode ( ' E ' ) ; root . right . left = newNode ( ' F ' ) ; root . right . right = newNode ( ' G ' ) ; printAllPaths ( root ) ; } }
Minimum steps required to rearrange given array to a power sequence of 2 | C # Program to the above approach ; Function to calculate the minimum steps required to convert given array into a power sequence of 2 ; Sort the array in ascending order ; Calculate the absolute difference between arr [ i ] and 2 ^ i for each index ; Return the answer ; Driver Code
using System ; class GFG { static int minsteps ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += Math . Abs ( arr [ i ] - ( int ) ( Math . Pow ( 2 , i ) ) ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 8 , 2 , 10 , 6 } ; int n = arr . Length ; Console . WriteLine ( minsteps ( arr , n ) ) ; } }
Block swap algorithm for array rotation |  ; Wrapper over the recursive function leftRotateRec ( ) It left rotates [ ] arr by d . ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; Function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code
using System ; class GFG { public static void leftRotate ( int [ ] arr , int d , int n ) { leftRotateRec ( arr , 0 , d , n ) ; } public static void leftRotateRec ( int [ ] arr , int i , int d , int n ) { if ( d == 0 d == n ) return ; if ( n - d == d ) { swap ( arr , i , n - d + i , d ) ; return ; } if ( d < n - d ) { swap ( arr , i , n - d + i , d ) ; leftRotateRec ( arr , i , d , n - d ) ; } else { swap ( arr , i , d , n - d ) ; leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; } } public static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void swap ( int [ ] arr , int fi , int si , int d ) { int i , temp ; for ( i = 0 ; i < d ; i ++ ) { temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; } }
Cost required to empty a given array by repeated removal of maximum obtained by given operations | C # program for the above approach ; Function to find the total cost of removing all array elements ; Sort the array in descending order ; Stores the total cost ; Contribution of i - th greatest element to the cost ; Remove the element ; If negative ; Add to the readonly cost ; Return the cost ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static int findCost ( int [ ] a , int n ) { Array . Sort ( a ) ; Array . Reverse ( a ) ; int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int p = a [ j ] - j ; a [ j ] = 0 ; if ( p < 0 ) { p = 0 ; continue ; } count += p ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 7 , 4 , 2 , 5 , 3 } ; int N = arr . Length ; Console . Write ( findCost ( arr , N ) ) ; } }
Block swap algorithm for array rotation | C # code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B
static void leftRotate ( int [ ] arr , int d , int n ) { int i , j ; if ( d == 0 d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }
Count of index pairs with equal elements in an array | Set 2 | C # program for the above approach ; Function that count the pairs having same elements in the array [ ] arr ; Hash map to keep track of occurences of elements ; Traverse the array [ ] arr ; Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer ; Return the result ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . ContainsKey ( arr [ i ] ) ) { ans += count [ arr [ i ] ] ; count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }
Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | C # implementation to find the minimum cost of removing all elements from the array ; Reverse array in decreasing order ; Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function call
using System ; class GFG { static long [ ] reverse ( long [ ] a ) { int i , n = a . Length ; long t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static long removeElements ( long [ ] arr , int n ) { Array . Sort ( arr ) ; arr = reverse ( arr ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( long ) ( arr [ i ] * Math . Pow ( 2 , i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 4 ; long [ ] arr = { 3 , 1 , 2 , 3 } ; Console . Write ( removeElements ( arr , n ) ) ; } }
Lexicographically largest string possible in one swap | C # implementation to find the lexicographically largest string by atmost at most one swap ; Function to return the lexicographically largest string possible by swapping at most one character ; Stores last occurrence of every character ; Initialize with - 1 for every character ; Keep updating the last occurrence of each character ; If a previously unvisited character occurs ; Stores the sorted string ; Character to replace ; Find the last occurrence of this character ; Swap this with the last occurrence ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string findLargest ( char [ ] s ) { int len = s . Length ; int [ ] loccur = new int [ 26 ] ; Array . Fill ( loccur , - 1 ) ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } char [ ] sorted_s = ( new string ( s ) ) . ToCharArray ( ) ; Array . Sort ( sorted_s ) ; Array . Reverse ( sorted_s ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; char temp = s [ i ] ; s [ i ] = s [ last_occ ] ; s [ last_occ ] = temp ; break ; } } return ( new string ( s ) ) ; } static void Main ( ) { string str = " yrstvw " ; char [ ] s = str . ToCharArray ( ) ; Console . WriteLine ( findLargest ( s ) ) ; } }
Given a sorted and rotated array , find if there is a pair with a given sum | C # program to find a pair with a given sum in a sorted and rotated array ; This function returns true if arr [ 0. . n - 1 ] has a pair with sum equals to x . ; Find the pivot element ; l is now index of smallest element ; r is now index of largest element ; Keep moving either l or r till they meet ; If we find a pair with sum x , we return true ; If current pair sum is less , move to the higher sum ; Move to the lower sum side ; Driver Code
using System ; class PairInSortedRotated { static bool pairInSortedRotated ( int [ ] arr , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) return true ; if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return false ; } public static void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 8 , 9 , 10 } ; int sum = 16 ; int n = arr . Length ; if ( pairInSortedRotated ( arr , n , sum ) ) Console . WriteLine ( " Array ▁ has ▁ two ▁ elements " + " ▁ with ▁ sum ▁ 16" ) ; else Console . WriteLine ( " Array ▁ doesn ' t ▁ have ▁ two " + " ▁ elements ▁ with ▁ sum ▁ 16 ▁ " ) ; } }
Given a sorted and rotated array , find if there is a pair with a given sum | C # program to find number of pairs with a given sum in a sorted and rotated array . ; This function returns count of number of pairs with sum equals to x . ; Find the pivot element . Pivot element is largest element of array . ; l is index of smallest element . ; r is index of largest element . ; Variable to store count of number of pairs . ; Find sum of pair formed by arr [ l ] and arr [ r ] and update l , r and cnt accordingly . ; If we find a pair with sum x , then increment cnt , move l and r to next element . ; This condition is required to be checked , otherwise l and r will cross each other and loop will never terminate . ; If current pair sum is less , move to the higher sum side . ; If current pair sum is greater , move to the lower sum side . ; Driver Code
using System ; class GFG { static int pairsInSortedRotated ( int [ ] arr , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; int cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } static public void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 7 , 9 , 10 } ; int sum = 16 ; int n = arr . Length ; Console . WriteLine ( pairsInSortedRotated ( arr , n , sum ) ) ; } }
Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | C # program to find max value of i * arr [ i ] ; Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver Code
using System ; class Test { static int [ ] arr = new int [ ] { 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; static int maxSum ( ) { int arrSum = 0 ; int currVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } int maxVal = currVal ; for ( int j = 1 ; j < arr . Length ; j ++ ) { currVal = currVal + arrSum - arr . Length * arr [ arr . Length - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } public static void Main ( ) { Console . WriteLine ( " Max ▁ sum ▁ is ▁ " + maxSum ( ) ) ; } }
Print the nodes at odd levels of a tree | Recursive C # program to print odd level nodes ; If empty tree ; If current node is of odd level ; Recur for children with isOdd switched . ; Utility method to create a node ; Driver code
using System ; public class GfG { public class Node { public int data ; public Node left , right ; } public static void printOddNodes ( Node root , bool isOdd ) { if ( root == null ) { return ; } if ( isOdd == true ) { Console . Write ( root . data + " ▁ " ) ; } printOddNodes ( root . left , ! isOdd ) ; printOddNodes ( root . right , ! isOdd ) ; } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } 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 ) ; printOddNodes ( root , true ) ; } }
Maximum sum of i * arr [ i ] among all rotations of a given array | A Naive C # program to find maximum sum rotation ; Returns maximum value of i * arr [ i ] ; Initialize result ; Consider rotation beginning with i for all possible values of i . ; Initialize sum of current rotation ; Compute sum of all values . We don 't  acutally rotation the array, but compute  sum by finding ndexes when arr[i] is  first element ; Update result if required ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { int res = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . Max ( res , curr_sum ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }
Maximum sum of i * arr [ i ] among all rotations of a given array | An efficient C # program to compute maximum sum of i * arr [ i ] ; Compute sum of all array elements ; Compute sum of i * arr [ i ] for initial configuration . ; Initialize result ; Compute values for other iterations ; Compute next value using previous value in O ( 1 ) time ; Update current value ; Update result if required ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { int cum_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; int curr_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; int res = curr_val ; for ( int i = 1 ; i < n ; i ++ ) { int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . Max ( res , next_val ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 1 , 2 } ; int n = arr . Length ; Console . Write ( maxSum ( arr , n ) ) ; } }
Minimum time required to cover a Binary Array | C # implementation to find the Minimum time required to cover a Binary Array ; Function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; Check if count is odd ; Check ifcount is even ; Return the time ; Driver code ; Initialise N ; Array initialisation
using System ; class GFG { static int solve ( int [ ] arr , int n ) { int k = arr . Length ; int [ ] mp = new int [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = Math . Max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = Math . Max ( leftSegment , rightSegment ) ; int tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } public static void Main ( ) { int N = 5 ; int [ ] arr = { 1 , 4 } ; Console . Write ( solve ( arr , N ) ) ; } }
Find the Rotation Count in Rotated Sorted array | c # program to find number of rotations in a sorted and rotated array . ; Returns count of rotations for an array which is first sorted in ascending order , then rotated ; We basically find index of minimum element ; Driver program to test above functions
using System ; class LinearSearch { static int countRotations ( int [ ] arr , int n ) { int min = arr [ 0 ] , min_index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > arr [ i ] ) { min = arr [ i ] ; min_index = i ; } } return min_index ; } public static void Main ( ) { int [ ] arr = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = arr . Length ; Console . WriteLine ( countRotations ( arr , n ) ) ; } }