codeparrot/xlcost-text-to-code huggingface dataset:
Find the first , second and third minimum elements in an array | C # program to find the first , second and third minimum element in an array ; Check if current element is less than firstmin , then update first , second and third ; Check if current element is less than secmin then update second and third ; Check if current element is less than then update third ; Driver code
using System ; class GFG { static void Print3Smallest ( int [ ] array , int n ) { int firstmin = int . MaxValue ; int secmin = int . MaxValue ; int thirdmin = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } Console . WriteLine ( " First ▁ min ▁ = ▁ " + firstmin ) ; Console . WriteLine ( " Second ▁ min ▁ = ▁ " + secmin ) ; Console . WriteLine ( " Third ▁ min ▁ = ▁ " + thirdmin ) ; } static void Main ( ) { int [ ] array = new int [ ] { 4 , 9 , 1 , 32 , 12 } ; int n = array . Length ; Print3Smallest ( array , n ) ; } }
Program to find the minimum ( or maximum ) element of an array | C # program to find minimum ( or maximum ) element in an array . ; If there is single element , return it . Else return minimum of first element and minimum of remaining array . ; If there is single element , return it . Else return maximum of first element and maximum of remaining array . ; Driver code
using System ; class GFG { static int getMin ( int [ ] arr , int i , int n ) { return ( n == 1 ) ? arr [ i ] : Math . Min ( arr [ i ] , getMin ( arr , i + 1 , n - 1 ) ) ; } static int getMax ( int [ ] arr , int i , int n ) { return ( n == 1 ) ? arr [ i ] : Math . Max ( arr [ i ] , getMax ( arr , i + 1 , n - 1 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ element ▁ of ▁ array : ▁ " + getMin ( arr , 0 , n ) ) ; Console . WriteLine ( " Maximum ▁ element ▁ of ▁ array : ▁ " + getMax ( arr , 0 , n ) ) ; } }
Program to find the minimum ( or maximum ) element of an array | C # program to find minimum ( or maximum ) element in an array . ; C # program to find minimum ( or maximum ) element in an array . ; Driver code
using System ; using System . Linq ; class GFG { static int getMin ( int [ ] arr , int n ) { return arr . Min ( ) ; } static int getMax ( int [ ] arr , int n ) { return arr . Max ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ element ▁ of ▁ array : ▁ " + getMin ( arr , n ) ) ; Console . WriteLine ( " Maximum ▁ element ▁ of ▁ array : ▁ " + getMax ( arr , n ) ) ; } }
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | C # program to print frequencies of all array elements in O ( n ) extra space and O ( n ) time ; Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Hashmap ; Traverse all array elements ; Update the frequency of array [ i ] ; Increase the index ; Driver code
using System ; class GFG { public static void findCounts ( int [ ] arr , int n ) { int [ ] hash = new int [ n ] ; int i = 0 ; while ( i < n ) { hash [ arr [ i ] - 1 ] ++ ; i ++ ; } Console . WriteLine ( " STRNEWLINE Below ▁ are ▁ counts ▁ " + " of ▁ all ▁ elements " ) ; for ( i = 0 ; i < n ; i ++ ) { Console . WriteLine ( ( i + 1 ) + " ▁ - > ▁ " + hash [ i ] ) ; } } static public void Main ( ) { int [ ] arr = new int [ ] { 2 , 3 , 3 , 2 , 5 } ; findCounts ( arr , arr . Length ) ; int [ ] arr1 = new int [ ] { 1 } ; findCounts ( arr1 , arr1 . Length ) ; int [ ] arr3 = new int [ ] { 4 , 4 , 4 , 4 } ; findCounts ( arr3 , arr3 . Length ) ; int [ ] arr2 = new int [ ] { 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 } ; findCounts ( arr2 , arr2 . Length ) ; int [ ] arr4 = new int [ ] { 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 } ; findCounts ( arr4 , arr4 . Length ) ; int [ ] arr5 = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 } ; findCounts ( arr5 , arr5 . Length ) ; int [ ] arr6 = new int [ ] { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ; findCounts ( arr6 , arr6 . Length ) ; } }
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | C # program to print frequencies of all array elements in O ( 1 ) extra space and O ( n ) time ; Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Traverse all array elements ; If this element is already processed , then nothing to do ; Find index corresponding to this element . For example , index for 5 is 4 ; If the elementIndex has an element that is not processed yet , then first store that element to arr [ i ] so that we don 't loose anything. ; After storing arr [ elementIndex ] , change it to store initial count of ' arr [ i ] ' ; If this is NOT first occurrence of arr [ i ] , then decrement its count . ; And initialize arr [ i ] as 0 means the element ' i + 1' is not seen so far ; Driver Code
using System ; class GFG { void findCounts ( int [ ] arr , int n ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] <= 0 ) { i ++ ; continue ; } int elementIndex = arr [ i ] - 1 ; if ( arr [ elementIndex ] > 0 ) { arr [ i ] = arr [ elementIndex ] ; arr [ elementIndex ] = - 1 ; } else { arr [ elementIndex ] -- ; arr [ i ] = 0 ; i ++ ; } } Console . Write ( " STRNEWLINE Below ▁ are ▁ counts ▁ of ▁ " + " all ▁ elements " + " STRNEWLINE " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( j + 1 + " - > " + Math . Abs ( arr [ j ] ) + " STRNEWLINE " ) ; } public static void Main ( ) { GFG count = new GFG ( ) ; int [ ] arr = { 2 , 3 , 3 , 2 , 5 } ; count . findCounts ( arr , arr . Length ) ; int [ ] arr1 = { 1 } ; count . findCounts ( arr1 , arr1 . Length ) ; int [ ] arr3 = { 4 , 4 , 4 , 4 } ; count . findCounts ( arr3 , arr3 . Length ) ; int [ ] arr2 = { 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 } ; count . findCounts ( arr2 , arr2 . Length ) ; int [ ] arr4 = { 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 } ; count . findCounts ( arr4 , arr4 . Length ) ; int [ ] arr5 = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 } ; count . findCounts ( arr5 , arr5 . Length ) ; int [ ] arr6 = { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ; count . findCounts ( arr6 , arr6 . Length ) ; } }
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time |  ; Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Subtract 1 from every element so that the elements become in range from 0 to n - 1 ; Use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; To print counts , simply print the number of times n was added at index corresponding to every element ; Driver program to test above functions
using System ; internal class CountFrequency { internal virtual void printfrequency ( int [ ] arr , int n ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ j ] = arr [ j ] - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] = arr [ arr [ i ] % n ] + n ; } for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + 1 + " - > " + arr [ i ] / n ) ; } } public static void Main ( string [ ] args ) { CountFrequency count = new CountFrequency ( ) ; int [ ] arr = new int [ ] { 2 , 3 , 3 , 2 , 5 } ; int n = arr . Length ; count . printfrequency ( arr , n ) ; } }
Delete an element from array ( Using two traversals and one traversal ) | C # program to remove a given element from an array ; This function removes an element x from arr [ ] and returns new size after removal ( size is reduced only when x is present in arr [ ] ; Search x in array ; If x found in array ; reduce size of array and move all elements on space ahead ; Driver program to test above function ; Delete x from arr [ ]
using System ; class GFG { static int deleteElement ( int [ ] arr , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) break ; if ( i < n ) { n = n - 1 ; for ( int j = i ; j < n ; j ++ ) arr [ j ] = arr [ j + 1 ] ; } return n ; } public static void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 8 , 9 , 10 } ; int n = arr . Length ; int x = 6 ; n = deleteElement ( arr , n , x ) ; Console . WriteLine ( " Modified ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Delete an element from array ( Using two traversals and one traversal ) | C # program to remove a given element from an array ; This function removes an element x from arr [ ] and returns new size after removal . Returned size is n - 1 when element is present . Otherwise 0 is returned to indicate failure . ; If x is last element , nothing to do ; Start from rightmost element and keep moving elements one position ahead . ; If element was not found ; Else move the next element in place of x ; Driver Code ; Delete x from arr [ ]
using System ; class GFG { static int deleteElement ( int [ ] arr , int n , int x ) { if ( arr [ n - 1 ] == x ) return ( n - 1 ) ; int prev = arr [ n - 1 ] , i ; for ( i = n - 2 ; i >= 0 && arr [ i ] != x ; i -- ) { int curr = arr [ i ] ; arr [ i ] = prev ; prev = curr ; } if ( i < 0 ) return 0 ; arr [ i ] = prev ; return ( n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 8 , 9 , 10 } ; int n = arr . Length ; int x = 6 ; n = deleteElement ( arr , n , x ) ; Console . WriteLine ( " Modified ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Count Inversions of size three in a given array | A O ( n ^ 2 ) Java program to count inversions of size 3 ; returns count of inversion of size 3 ; initialize result ; count all smaller elements on right of arr [ i ] ; count all greater elements on left of arr [ i ] ; update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; driver program to test above function
using System ; public class Inversion { static int getInvCount ( int [ ] arr , int n ) { int invcount = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ] ) great ++ ; invcount += great * small ; } return invcount ; } public static void Main ( ) { int [ ] arr = new int [ ] { 8 , 4 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Inversion ▁ count ▁ : ▁ " + getInvCount ( arr , n ) ) ; } }
Trapping Rain Water | C # implementation of the approach ; Function to return the maximum water that can be stored ; To store the maximum water that can be stored ; For every element of the array except first and last element ; Find maximum element on its left ; Find maximum element on its right ; Update maximum water value ; Driver code
using System ; class GFG { public static int maxWater ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int left = arr [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { left = Math . Max ( left , arr [ j ] ) ; } int right = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { right = Math . Max ( right , arr [ j ] ) ; } res += Math . Min ( left , right ) - arr [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . Length ; Console . Write ( maxWater ( arr , n ) ) ; } }
Trapping Rain Water | C # program to find maximum amount of water that can be trapped within given set of bars . ; Method for maximum amount of water ; left [ i ] contains height of tallest bar to the left of i 'th bar including itself ; Right [ i ] contains height of tallest bar to the right of ith bar including itself ; Initialize result ; Fill left array ; Fill right array ; Calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; Driver method to test the above function
using System ; class Test { static int [ ] arr = new int [ ] { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; static int findWater ( int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; int water = 0 ; left [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) left [ i ] = Math . Max ( left [ i - 1 ] , arr [ i ] ) ; right [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) right [ i ] = Math . Max ( right [ i + 1 ] , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) water += Math . Min ( left [ i ] , right [ i ] ) - arr [ i ] ; return water ; } public static void Main ( ) { Console . WriteLine ( " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " + findWater ( arr . Length ) ) ; } }
Trapping Rain Water | C # Code For Trapping Rain Water ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; Driver program
using System ; class GFG { static int findWater ( int [ ] arr , int n ) { int result = 0 ; int left_max = 0 , right_max = 0 ; int lo = 0 , hi = n - 1 ; while ( lo <= hi ) { if ( arr [ lo ] < arr [ hi ] ) { if ( arr [ lo ] > left_max ) left_max = arr [ lo ] ; else result += left_max - arr [ lo ] ; lo ++ ; } else { if ( arr [ hi ] > right_max ) right_max = arr [ hi ] ; else result += right_max - arr [ hi ] ; hi -- ; } } return result ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int result = Trap . findWater ( arr , arr . length ) ; System . out . print ( " ▁ Total ▁ trapping ▁ water : ▁ " + result ) ; } }
Trapping Rain Water | C # implementation of the approach ; Function to return the maximum water that can be stored ; Let the first element be stored as previous , we shall loop from index 1 ; To store previous wall 's index ; To store the water until a larger wall is found , if there are no larger walls then delete temp value from water ; If the current wall is taller than the previous wall then make current wall as the previous wall and its index as previous wall 's index  for the subsequent loops ; Because larger or same height wall is found ; Since current wall is shorter than the previous , we subtract previous wall ' s ▁ height ▁ from ▁ the ▁ current ▁ wall ' s height and add it to the water ; Store the same value in temp as well If we dont find any larger wall then we will subtract temp from water ; If the last wall was larger than or equal to the previous wall then prev_index would be equal to size of the array ( last element ) If we didn 't find a wall greater than or equal  to the previous wall from the left then  prev_index must be less than the index  of the last element ; Temp would 've stored the water collected  from previous largest wall till the end  of array if no larger wall was found then  it has excess water and remove that  from water variable ; We start from the end of the array , so previous should be assigned to the last element ; Loop from the end of array up to the previous index which would contain the largest wall from the left ; Right end wall will be definitely smaller than the ' previous ▁ index ' wall ; Return the maximum water ; Driver code
using System ; class GFG { static int maxWater ( int [ ] arr , int n ) { int size = n - 1 ; int prev = arr [ 0 ] ; int prev_index = 0 ; int water = 0 ; int temp = 0 ; for ( int i = 1 ; i <= size ; i ++ ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; prev_index = i ; temp = 0 ; } else { water += prev - arr [ i ] ; temp += prev - arr [ i ] ; } } if ( prev_index < size ) { water -= temp ; prev = arr [ size ] ; for ( int i = size ; i >= prev_index ; i -- ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; } else { water += prev - arr [ i ] ; } } } return water ; } static void Main ( ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxWater ( arr , n ) ) ; } }
Trapping Rain Water |  ; C # implementation of the approach ; Function to return the maximum water that can be stored ; Stores the indices of the bars ; size of the array ; Stores the final result ; Loop through the each bar ; Remove bars from the stack until the condition holds ; store the height of the top and pop it . ; If the stack does not have any bars or the the popped bar has no left boundary ; Get the distance between the left and right boundary of popped bar ; Calculate the min . height ; If the stack is either empty or height of the current bar is less than or equal to the top bar of stack ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static int maxWater ( int [ ] height ) { Stack stack = new Stack ( ) ; int n = height . Length ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ( stack . Count != 0 ) && ( height [ ( int ) stack . Peek ( ) ] < height [ i ] ) ) { int pop_height = height [ ( int ) stack . Peek ( ) ] ; stack . Pop ( ) ; if ( stack . Count == 0 ) break ; int distance = i - ( int ) stack . Peek ( ) - 1 ; int min_height = Math . Min ( height [ ( int ) stack . Peek ( ) ] , height [ i ] ) - pop_height ; ans += distance * min_height ; } stack . Push ( i ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; Console . Write ( maxWater ( arr ) ) ; } }
Trapping Rain Water | C # implementation of the approach ; indices to traverse the array ; To store Left max and right max for two pointers left and right ; To store the total amount of rain water trapped ; We need check for minimum of left and right max for each element ; Add the difference between current value and right max at index r ; Update right max ; Update right pointer ; Add the difference between current value and left max at index l ; Update left max ; Update left pointer ; Driver code
using System ; class GFG { static int maxWater ( int [ ] arr , int n ) { int left = 0 ; int right = n - 1 ; int l_max = 0 ; int r_max = 0 ; int result = 0 ; while ( left <= right ) { if ( r_max <= l_max ) { result += Math . Max ( 0 , r_max - arr [ right ] ) ; r_max = Math . Max ( r_max , arr [ right ] ) ; right -= 1 ; } else { result += Math . Max ( 0 , l_max - arr [ left ] ) ; l_max = Math . Max ( l_max , arr [ left ] ) ; left += 1 ; } } return result ; } static void Main ( ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxWater ( arr , n ) ) ; } }
Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | C # code for median with case of returning double value when even number of elements are present in both array combinely ; Function to find max ; Function to find minimum ; Function to find median of two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . and if j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . Searching on right ; if i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have any elements in the  first half from a[] so we  returning the last element in  b[] from the first half. ; and this condition happens when we don 't have any elements in the  first half from b[] so we  returning the last element in  a[] from the first half. ; calculating the median . If number of elements is odd there is one middle element . ; Elements from a [ ] in the second half is an empty set . ; Elements from b [ ] in the second half is an empty set . ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
using System ; class GFG { static int maximum ( int a , int b ) { return a > b ? a : b ; } static int minimum ( int a , int b ) { return a < b ? a : b ; } static double findMedianSortedArrays ( ref int [ ] a , int n , ref int [ ] b , int m ) { int min_index = 0 , max_index = n , i = 0 , j = 0 , median = 0 ; while ( min_index <= max_index ) { i = ( min_index + max_index ) / 2 ; j = ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) median = b [ j - 1 ] ; else if ( j == 0 ) median = a [ i - 1 ] ; else median = maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; break ; } } if ( ( n + m ) % 2 == 1 ) return ( double ) median ; if ( i == n ) return ( median + b [ j ] ) / 2.0 ; if ( j == m ) return ( median + a [ i ] ) / 2.0 ; return ( median + minimum ( a [ i ] , b [ j ] ) ) / 2.0 ; } static void Main ( ) { int [ ] a = new int [ ] { 900 } ; int [ ] b = new int [ ] { 10 , 13 , 14 } ; int n = a . Length ; int m = b . Length ; if ( n < m ) Console . Write ( " The ▁ median ▁ is ▁ : ▁ " + findMedianSortedArrays ( ref a , n , ref b , m ) ) ; else Console . Write ( " The ▁ median ▁ is ▁ : ▁ " + findMedianSortedArrays ( ref b , m , ref a , n ) ) ; } }
Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | C # code for finding median of the given two sorted arrays that exists in the merged array ( ( n + m ) / 2 - 1 position ) ; Function to find median of given two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . If j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; if i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have any  elements in the first  half from a[] so we  returning the last  element in b[] from  the first half. ; and this condition happens when we don 't have any  elements in the first half  from b[] so we returning the  last element in a[] from the  first half. ; Function to find maximum ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
using System ; class GFG { static int findMedianSortedArrays ( ref int [ ] a , int n , ref int [ ] b , int m ) { int min_index = 0 , max_index = n , i , j ; while ( min_index <= max_index ) { i = ( min_index + max_index ) / 2 ; j = ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) return b [ j - 1 ] ; if ( j == 0 ) return a [ i - 1 ] ; else return maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; } } Console . Write ( " ERROR ! ! ! ▁ " + " returning STRNEWLINE " ) ; return 0 ; } static int maximum ( int a , int b ) { return a > b ? a : b ; } static void Main ( ) { int [ ] a = new int [ ] { 900 } ; int [ ] b = new int [ ] { 10 , 13 , 14 } ; int n = a . Length ; int m = b . Length ; if ( n < m ) Console . Write ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( ref a , n , ref b , m ) ) ; else Console . Write ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( ref b , m , ref a , n ) ) ; } }