codeparrot/xlcost-text-to-code huggingface dataset:
Replace every element with the smallest of the others | C # code for the above approach . ; There should be atleast two elements ; If current element is smaller than firstSmallest then update both firstSmallest and secondSmallest ; If arr [ i ] is in between firstSmallest and secondSmallest then update secondSmallest ; Replace every element by smallest of all other elements ; Print the modified array . ; Driver code
using System ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { if ( n < 2 ) { Console . Write ( " Invalid ▁ Input " ) ; return ; } int firstSmallest = Int32 . MaxValue ; int secondSmallest = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < firstSmallest ) { secondSmallest = firstSmallest ; firstSmallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest && arr [ i ] != firstSmallest ) secondSmallest = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != firstSmallest ) arr [ i ] = firstSmallest ; else arr [ i ] = secondSmallest ; } for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " , ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 3 } ; int n = arr . Length ; ReplaceElements ( arr , n ) ; } }
Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | C # implementation to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table ' um ' ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver program to test above
using System ; using System . Collections . Generic ; class GFG { static int countSubarrWithEqualZeroAndOne ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; if ( mp . ContainsKey ( curr_sum ) ) { var v = mp [ curr_sum ] ; mp . Remove ( curr_sum ) ; mp . Add ( curr_sum , ++ v ) ; } else mp . Add ( curr_sum , 1 ) ; } int count = 0 ; foreach ( KeyValuePair < int , int > itr in mp ) { if ( itr . Value > 1 ) count += ( ( itr . Value * ( itr . Value - 1 ) ) / 2 ) ; } if ( mp . ContainsKey ( 0 ) ) count += mp [ 0 ] ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Count ▁ = ▁ " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ; } }
Longest subarray having count of 1 s one more than count of 0 s | C # implementation to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider '0' as ' - 1' ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int lenOfLongSubarr ( int [ ] arr , int n ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . ContainsKey ( sum ) ) um . Add ( sum , i ) ; if ( um . ContainsKey ( sum - 1 ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ = ▁ " + lenOfLongSubarr ( arr , n ) ) ; } }
Maximum sum subarray having sum less than or equal to given sum using Set | Java program to find maximum sum subarray less than K ; Function to maximum required sum < K ; Hash to lookup for value ( cum_sum - K ) ; Getting cummulative sum from [ 0 to i ] ; Lookup for upperbound of ( cSum - K ) in hash ; Check if upper_bound of ( cSum - K ) exists then update max sum ; Insert cummulative value in hash ; Return maximum sum lesser than K ; x is the target value or key ; Driver code ; Initialise the array ; Initialise the value of K ; Size of array
using System ; using System . Collections . Generic ; class GFG { static int maxSubarraySum ( int [ ] arr , int N , int K ) { HashSet < int > cum_set = new HashSet < int > ( ) ; cum_set . Add ( 0 ) ; int max_sum = Int32 . MinValue , cSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; List < int > al = new List < int > ( ) ; int end = 0 ; foreach ( int it in cum_set ) { end = it ; al . Add ( it ) ; } al . Sort ( ) ; int sit = lower_bound ( al , cSum - K ) ; if ( sit != end ) max_sum = Math . Max ( max_sum , cSum - sit ) ; cum_set . Add ( cSum ) ; } return max_sum ; } static int lower_bound ( List < int > al , int x ) { int l = - 1 , r = al . Count ; while ( l + 1 < r ) { int m = ( l + r ) >> 1 ; if ( al [ m ] >= x ) r = m ; else l = m ; } return r ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , - 2 , 6 , 3 , - 5 } ; int K = 15 ; int N = arr . Length ; Console . Write ( maxSubarraySum ( arr , N , K ) ) ; } }
Print all triplets in sorted array that form AP | C # program to print all triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printAllAPTriplets ( int [ ] arr , int n ) { List < int > s = new List < int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; bool exists = s . Exists ( element => element == ( arr [ i ] - diff ) ) ; if ( exists ) Console . WriteLine ( arr [ i ] - diff + " ▁ " + arr [ i ] + " ▁ " + arr [ j ] ) ; } s . Add ( arr [ i ] ) ; } } static void Main ( ) { int [ ] arr = new int [ ] { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . Length ; printAllAPTriplets ( arr , n ) ; } }
Print all triplets in sorted array that form AP | C # implementation to print all the triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
using System ; class GFG { static void findAllTriplets ( int [ ] arr , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { for ( int j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { Console . WriteLine ( arr [ j ] + " ▁ " + arr [ i ] + " ▁ " + arr [ k ] ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } public static void Main ( ) { int [ ] arr = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . Length ; findAllTriplets ( arr , n ) ; } }
Check if there is a root to leaf path with given sequence | C # program to see if there is a root to leaf path with given sequence . ; A binary tree node has data , pointer to left child and a pointer to right child ; function to check given sequence of root to leaf path exist in tree or not . index represents current element in sequence of rooth to leaf path ; If root is NULL , then there must not be any element in array . ; If this node is a leaf and matches with last entry of array . ; If current node is equal to arr [ index ] this means that till this level path has been matched and remaining path can be either in left subtree or right subtree . ; Driver code ; arr [ ] is sequence of root to leaf path
using System ; public class CheckForPath { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; public static bool existPath ( Node root , int [ ] arr , int index ) { if ( root == null ) { return arr . Length == 0 ; } if ( ( root . left == null && root . right == null ) && ( root . data == arr [ index ] && root . data == arr [ arr . Length - 1 ] ) ) { return true ; } return ( index < arr . Length && ( root . data == arr [ index ] && ( existPath ( root . left , arr , index + 1 ) || existPath ( root . right , arr , index + 1 ) ) ) ) ; } public static void Main ( ) { int [ ] arr = { 5 , 8 , 6 , 7 } ; Node root = new Node ( 5 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 8 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 4 ) ; root . left . left . left = new Node ( 1 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; if ( existPath ( root , arr , 0 ) ) { Console . Write ( " Path ▁ Exists " ) ; } else { Console . Write ( " Path ▁ does ▁ not ▁ Exist " ) ; } } }
All unique triplets that sum up to a given value | C # program to find all unique triplets without using any extra space . ; Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int [ ] a , int n , int sum ) { int i ; Array . Sort ( a ) ; bool flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { int start = i + 1 ; int end = n - 1 ; int target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { Console . Write ( " [ " + a [ i ] + " , " + a [ start ] + " , " + a [ end ] + " ] ▁ " ) ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { Console . WriteLine ( " No ▁ Such ▁ Triplets ▁ Exist " ) ; } } static void Main ( ) { int [ ] a = { 12 , 3 , 6 , 1 , 6 , 9 } ; int n = a . Length ; int sum = 24 ; findTriplets ( a , n , sum ) ; } }
Find the position of the given row in a 2 | C # implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code
using System ; class GFG { static int m = 6 ; static int n = 4 ; static int linearCheck ( int [ , ] ar , int [ ] arr ) { for ( int i = 0 ; i < m ; i ++ ) { bool matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i , j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } static public void Main ( ) { int [ , ] mat = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int [ ] row = { 10 , 9 , 22 , 23 } ; Console . Write ( linearCheck ( mat , row ) ) ; } }
Count number of triplets with product equal to given number | C # program to count triplets with given product m ; Method to count such triplets ; Consider all triplets and count if their product is equal to m ; Driver method
using System ; public class GFG { static int countTriplets ( int [ ] arr , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) count ++ ; return count ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 6 , 2 , 3 , 8 } ; int m = 24 ; Console . WriteLine ( countTriplets ( arr , arr . Length , m ) ) ; } }
Count number of triplets with product equal to given number | C # implementation of the above approach ; Method to count such triplets ; Store all the elements in a set ; Consider all pairs and check for a third number so their product is equal to m ; Check if current pair divides m or not If yes , then search for ( m / arr [ i ] * arr [ j ] ) ; occ . containsKey ( check ) ; Check if the third number is present in the map and it is not equal to any other two elements and also check if this triplet is not counted already using their indexes ; Return number of triplets ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] arr , int n , int m ) { Dictionary < int , int > occ = new Dictionary < int , int > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) occ . Add ( arr [ i ] , i ) ; int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ i ] * arr [ j ] <= m ) && ( arr [ i ] * arr [ j ] != 0 ) && ( m % ( arr [ i ] * arr [ j ] ) == 0 ) ) { int check = m / ( arr [ i ] * arr [ j ] ) ; if ( check != arr [ i ] && check != arr [ j ] && occ . ContainsKey ( check ) && occ [ check ] > i && occ [ check ] > j ) count ++ ; } } } return count ; } static void Main ( ) { int [ ] arr = { 1 , 4 , 6 , 2 , 3 , 8 } ; int m = 24 ; Console . WriteLine ( countTriplets ( arr , arr . Length , m ) ) ; } }
Count of index pairs with equal elements in an array | C # program to count of pairs with equal elements in an array . ; Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driver code
using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }
Queries to answer the number of ones and zero to the left of given index | C # implementation of the approach ; pair class ; Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
using System ; class GFG { public class pair { public int first , second ; public pair ( int a , int b ) { first = a ; second = b ; } } static void preCalculate ( int [ ] binary , int n , pair [ ] left ) { int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] . first = count1 ; left [ i ] . second = count0 ; if ( binary [ i ] != 0 ) count1 ++ ; else count0 ++ ; } } public static void Main ( String [ ] args ) { int [ ] binary = { 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = binary . Length ; pair [ ] left = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) left [ i ] = new pair ( 0 , 0 ) ; preCalculate ( binary , n , left ) ; int [ ] queries = { 0 , 1 , 2 , 4 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( left [ queries [ i ] ] . first + " ▁ ones ▁ " + left [ queries [ i ] ] . second + " ▁ zeros STRNEWLINE " ) ; } }
Count of index pairs with equal elements in an array | C # program to count of index pairs with equal elements in an array . ; Return the number of pairs with equal values . ; Finding frequency of each number . ; Calculating count of pairs with equal values ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int countPairs ( int [ ] arr , int n ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . ContainsKey ( arr [ i ] ) ) { int a = hm [ arr [ i ] ] ; hm . Remove ( arr [ i ] ) ; hm . Add ( arr [ i ] , a + 1 ) ; } else hm . Add ( arr [ i ] , 1 ) ; } int ans = 0 ; foreach ( var it in hm ) { int count = it . Value ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 1 } ; Console . WriteLine ( countPairs ( arr , arr . Length ) ) ; } }
Partition the array into three equal sum segments | C # implementation of the approach ; First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code
using System ; class GFG { public static int pos1 = - 1 ; public static int pos2 = - 1 ; public static bool equiSumUtil ( int [ ] arr ) { int n = arr . Length ; int [ ] pre = new int [ n ] ; int sum = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } int [ ] suf = new int [ n ] ; sum = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } int total_sum = sum ; int j = n - 1 ; i = 0 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != - 1 && pos2 != - 1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } public static void equiSum ( int [ ] arr ) { bool ans = equiSumUtil ( arr ) ; if ( ans ) { Console . Write ( " First ▁ Segment ▁ : ▁ " ) ; for ( int i = 0 ; i <= pos1 ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " Second ▁ Segment ▁ : ▁ " ) ; for ( int i = pos1 + 1 ; i < pos2 ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " Third ▁ Segment ▁ : ▁ " ) ; for ( int i = pos2 ; i < arr . Length ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } else { Console . WriteLine ( " Array ▁ cannot ▁ be ▁ " + " divided ▁ into ▁ three ▁ equal ▁ sum ▁ segments " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 } ; equiSum ( arr ) ; } }
Print cousins of a given node in Binary Tree | C # program to print cousins of a node ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; It returns level of the node if it is present in tree , otherwise returns 0. ; base cases ; If node is present in left subtree ; If node is not present in left subtree ; Print nodes at a given level such that sibling of node is not printed if it exists ; Base cases ; If current node is parent of a node with given level ; Recur for left and right subtrees ; This function prints cousins of a given node ; Get level of given node ; Print nodes of given level . ; Driver code
using System ; public class GfG { class Node { public int data ; public Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int getLevel ( Node root , Node node , int level ) { if ( root == null ) return 0 ; if ( root == node ) return level ; int downlevel = getLevel ( root . left , node , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return getLevel ( root . right , node , level + 1 ) ; } static void printGivenLevel ( Node root , Node node , int level ) { if ( root == null level < 2 ) return ; if ( level == 2 ) { if ( root . left == node root . right == node ) return ; if ( root . left != null ) Console . Write ( root . left . data + " ▁ " ) ; if ( root . right != null ) Console . Write ( root . right . data + " ▁ " ) ; } else if ( level > 2 ) { printGivenLevel ( root . left , node , level - 1 ) ; printGivenLevel ( root . right , node , level - 1 ) ; } } static void printCousins ( Node root , Node node ) { int level = getLevel ( root , node , 1 ) ; printGivenLevel ( root , node , level ) ; } 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 . right = newNode ( 15 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; printCousins ( root , root . left . right ) ; } }
Leftmost and rightmost indices of the maximum and the minimum element of an array | C # implementation of above idea ; Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
using System ; public class GFG { public static int getRightMin ( int [ ] arr , int n ) { int min = arr [ 0 ] ; int rightMin = 0 ; int i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } public static int getLeftMax ( int [ ] arr , int n ) { int max = arr [ n - 1 ] ; int leftMax = n - 1 ; int i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i /= 2 ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } public static void Main ( ) { int [ ] arr = { 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ left ▁ : ▁ " + 0 ) ; Console . WriteLine ( " Minimum ▁ right ▁ : ▁ " + getRightMin ( arr , n ) ) ; Console . WriteLine ( " Maximum ▁ left ▁ : ▁ " + getLeftMax ( arr , n ) ) ; Console . WriteLine ( " Maximum ▁ right ▁ : ▁ " + ( n - 1 ) ) ; } }
Elements to be added so that all elements of a range are present in array | C # program for above implementation ; Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Drivers code
using System ; public class GFG { static int countNum ( int [ ] arr , int n ) { int count = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . Length ; Console . WriteLine ( countNum ( arr , n ) ) ; } }