codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Bitwise | C # program to find sum of Bitwise - OR of all submatrices ; Function to find prefix - count for each row from right to left ; Function to create a booleanean matrix set_bit which stores '1' at an index ( R , C ) if ith bit of arr [ R , C ] is set . ; array to store prefix count of zeros from right to left for booleanean array ; variable to store the count of submatrices with OR value 0 ; For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; stack to store elements and the count of the numbers they popped First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; variable to store the number of submatrices with all 0 s ; Function to find sum of Bitwise - OR of all submatrices ; matrix to store the status of ith bit of each element of matrix arr ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int n = 3 ; static void findPrefixCount ( int [ , ] p_arr , Boolean [ , ] set_bit ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( set_bit [ i , j ] ) continue ; if ( j != n - 1 ) p_arr [ i , j ] += p_arr [ i , j + 1 ] ; p_arr [ i , j ] += ( ! set_bit [ i , j ] ) ? 1 : 0 ; } } } public class pair { public int first , second ; public pair ( ) { } public pair ( int a , int b ) { first = a ; second = b ; } } static int matrixOrValueOne ( Boolean [ , ] set_bit ) { int [ , ] p_arr = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) p_arr [ i , j ] = 0 ; findPrefixCount ( p_arr , set_bit ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . Count != 0 && q . Peek ( ) . first > p_arr [ i , j ] ) { to_sum -= ( q . Peek ( ) . second + 1 ) * ( q . Peek ( ) . first - p_arr [ i , j ] ) ; c += q . Peek ( ) . second + 1 ; q . Pop ( ) ; } to_sum += p_arr [ i , j ] ; count_zero_submatrices += to_sum ; q . Push ( new pair ( p_arr [ i , j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } static int sumOrMatrix ( int [ , ] arr ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { Boolean [ , ] set_bit = new Boolean [ n , n ] ; for ( int R = 0 ; R < n ; R ++ ) for ( int C = 0 ; C < n ; C ++ ) set_bit [ R , C ] = ( ( arr [ R , C ] & ( 1 << i ) ) != 0 ) ; sum += ( mul * matrixOrValueOne ( set_bit ) ) ; mul *= 2 ; } return sum ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 9 , 7 , 4 } , { 8 , 9 , 2 } , { 11 , 11 , 5 } } ; Console . WriteLine ( sumOrMatrix ( arr ) ) ; } }
Count of sub | C # implementation of the approach ; Function to return the total number of required sub - sets ; Variable to store total elements which on dividing by 3 give remainder 0 , 1 and 2 respectively ; Create a dp table ; Process for n states and store the sum ( mod 3 ) for 0 , 1 and 2 ; Use of MOD for large numbers ; Final answer store at dp [ n - 1 , 0 ] ; Driver Program
using System ; class GFG { static int MOD = 1000000007 ; static int totalSubSets ( int n , int l , int r ) { int zero = ( int ) Math . Floor ( ( double ) r / 3 ) - ( int ) Math . Ceiling ( ( double ) l / 3 ) + 1 ; int one = ( int ) Math . Floor ( ( double ) ( r - 1 ) / 3 ) - ( int ) Math . Ceiling ( ( double ) ( l - 1 ) / 3 ) + 1 ; int two = ( int ) Math . Floor ( ( double ) ( r - 2 ) / 3 ) - ( int ) Math . Ceiling ( ( double ) ( l - 2 ) / 3 ) + 1 ; int [ , ] dp = new int [ n , 3 ] ; dp [ 0 , 0 ] = zero ; dp [ 0 , 1 ] = one ; dp [ 0 , 2 ] = two ; for ( int i = 1 ; i < n ; ++ i ) { dp [ i , 0 ] = ( ( dp [ i - 1 , 0 ] * zero ) + ( dp [ i - 1 , 1 ] * two ) + ( dp [ i - 1 , 2 ] * one ) ) % MOD ; dp [ i , 1 ] = ( ( dp [ i - 1 , 0 ] * one ) + ( dp [ i - 1 , 1 ] * zero ) + ( dp [ i - 1 , 2 ] * two ) ) % MOD ; dp [ i , 2 ] = ( ( dp [ i - 1 , 0 ] * two ) + ( dp [ i - 1 , 1 ] * one ) + ( dp [ i - 1 , 2 ] * zero ) ) % MOD ; } return dp [ n - 1 , 0 ] ; } public static void Main ( ) { int n = 5 ; int l = 10 ; int r = 100 ; Console . WriteLine ( totalSubSets ( n , l , r ) ) ; } }
Maximum sum of nodes in Binary tree such that no two are adjacent | C # program to find maximum sum in Binary Tree such that no two nodes are adjacent . ; A binary tree node structure ; maxSumHelper function ; This node is included ( Left and right children are not included ) ; This node is excluded ( Either left or right child is included ) ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code ; Pair class
using System ; public class FindSumOfNotAdjacentNodes { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; public static Pair maxSumHelper ( Node root ) { Pair sum ; if ( root == null ) { sum = new Pair ( 0 , 0 ) ; return sum ; } Pair sum1 = maxSumHelper ( root . left ) ; Pair sum2 = maxSumHelper ( root . right ) ; Pair sum3 = new Pair ( 0 , 0 ) ; sum3 . first = sum1 . second + sum2 . second + root . data ; sum3 . second = Math . Max ( sum1 . first , sum1 . second ) + Math . Max ( sum2 . first , sum2 . second ) ; return sum3 ; } public static int maxSum ( Node root ) { Pair res = maxSumHelper ( root ) ; return Math . Max ( res . first , res . second ) ; } public static void Main ( ) { Node root = new Node ( 10 ) ; root . left = new Node ( 1 ) ; root . left . left = new Node ( 2 ) ; root . left . left . left = new Node ( 1 ) ; root . left . right = new Node ( 3 ) ; root . left . right . left = new Node ( 4 ) ; root . left . right . right = new Node ( 5 ) ; Console . Write ( maxSum ( root ) ) ; } } public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } }
Check if a word exists in a grid or not | C # program to check if the word exists in the grid or not ; Function to check if a word exists in a grid starting from the first match in the grid level : index till which pattern is matched x , y : current position in 2D array ; Pattern matched ; Out of Boundary ; If grid matches with a letter while recursion ; Marking this cell as visited ; finding subpattern in 4 directions ; marking this cell as unvisited again ; else Not matching then false ; Function to check if the word exists in the grid or not ; if total characters in matrix is less then pattern lenghth ; Traverse in the grid ; If first letter matches , then recur and check ; Driver Code ; Function to check if word exists or not
using System ; class GFG { static readonly int r = 4 ; static readonly int c = 4 ; static bool findmatch ( char [ , ] mat , String pat , int x , int y , int nrow , int ncol , int level ) { int l = pat . Length ; if ( level == l ) return true ; if ( x < 0 y < 0 x >= nrow y >= ncol ) return false ; if ( mat [ x , y ] == pat [ level ] ) { char temp = mat [ x , y ] ; mat [ x , y ] = ' # ' ; bool res = findmatch ( mat , pat , x - 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x + 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y - 1 , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y + 1 , nrow , ncol , level + 1 ) ; mat [ x , y ] = temp ; return res ; } return false ; } static bool checkMatch ( char [ , ] mat , String pat , int nrow , int ncol ) { int l = pat . Length ; if ( l > nrow * ncol ) return false ; for ( int i = 0 ; i < nrow ; i ++ ) { for ( int j = 0 ; j < ncol ; j ++ ) { if ( mat [ i , j ] == pat [ 0 ] ) if ( findmatch ( mat , pat , i , j , nrow , ncol , 0 ) ) return true ; } } return false ; } public static void Main ( String [ ] args ) { char [ , ] grid = { { ' a ' , ' x ' , ' m ' , ' y ' } , { ' b ' , ' g ' , ' d ' , ' f ' } , { ' x ' , ' e ' , ' e ' , ' t ' } , { ' r ' , ' a ' , ' k ' , ' s ' } } ; if ( checkMatch ( grid , " geeks " , r , c ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Construct an array from its pair | C # program to construct an array from its pair - sum array ; Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver program to test above function
using System ; class PairSum { static void constructArr ( int [ ] arr , int [ ] pair , int n ) { arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ; for ( int i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; } public static void Main ( ) { int [ ] pair = { 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 } ; int n = 5 ; int [ ] arr = new int [ n ] ; constructArr ( arr , pair , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Size of the smallest subset with maximum Bitwise OR | C # Program for above approach ; Compute bitwise or of all elements in array of size sz ; Recursively calculating the size of minimum subset with maximum or ; If i is arr . length ; If curOr is equal to maxOr ; Return arr . length ; Try the current element in the subset ; Skip the current element ; Return minimum of take and notTake ; Driver Code ; Function Call
using System ; class Solution { private static int OR ( int [ ] arr ) { int mOR = 0 ; for ( int i = 0 ; i < arr . Length ; ++ i ) { mOR |= arr [ i ] ; } return mOR ; } private static int maxSubset ( int [ ] arr , int i , int curOr , int curSize , int maxOr ) { if ( i == arr . Length ) { if ( curOr == maxOr ) { return curSize ; } else { return arr . Length ; } } int take = maxSubset ( arr , i + 1 , curOr arr [ i ] , curSize + 1 , ) ; int notTake = maxSubset ( arr , i + 1 , curOr , curSize , maxOr ) ; return Math . Min ( take , notTake ) ; } static void Main ( ) { int [ ] data = { 5 , 1 , 3 , 4 , 2 } ; int maxOr = OR ( data ) ; int maxSubsetSize = maxSubset ( data , 0 , 0 , 0 , maxOr ) ; Console . WriteLine ( maxSubsetSize ) ; } }
Merge two sorted arrays with O ( 1 ) extra space | C # program program to merge two sorted arrays with O ( 1 ) extra space . ; Merge ar1 [ ] and ar2 [ ] with O ( 1 ) extra space ; Iterate through all elements of ar2 [ ] starting from the last element ; Find the smallest element greater than ar2 [ i ] . Move all elements one position ahead till the smallest greater element is not found ; If there was a greater element ; Driver method to test the above function
using System ; public class Test { static int [ ] arr1 = new int [ ] { 1 , 5 , 9 , 10 , 15 , 20 } ; static int [ ] arr2 = new int [ ] { 2 , 3 , 8 , 13 } ; static void merge ( int m , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { int j , last = arr1 [ m - 1 ] ; for ( j = m - 2 ; j >= 0 && arr1 [ j ] > arr2 [ i ] ; j -- ) arr1 [ j + 1 ] = arr1 [ j ] ; if ( j != m - 2 last > arr2 [ i ] ) { arr1 [ j + 1 ] = arr2 [ i ] ; arr2 [ i ] = last ; } } } public static void Main ( ) { merge ( arr1 . Length , arr2 . Length ) ; Console . Write ( " After ▁ Merging ▁ STRNEWLINE First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < arr1 . Length ; i ++ ) { Console . Write ( arr1 [ i ] + " ▁ " ) ; } Console . Write ( " STRNEWLINE Second ▁ Array : ▁ " ) ; for ( int i = 0 ; i < arr2 . Length ; i ++ ) { Console . Write ( arr2 [ i ] + " ▁ " ) ; } } }
Gould 's Sequence | C # program to generate Gould 's Sequence ; Function to generate gould 's Sequence ; loop to generate each row of pascal 's Triangle up to nth row ; Loop to generate each element of ith row ; if c is odd increment count ; print count of odd elements ; Driver code ; Get n ; Function call
using System ; class GFG { static void gouldSequence ( int n ) { for ( int row_num = 1 ; row_num <= n ; row_num ++ ) { int count = 1 ; int c = 1 ; for ( int i = 1 ; i <= row_num ; i ++ ) { c = c * ( row_num - i ) / i ; if ( c % 2 == 1 ) count ++ ; } Console . Write ( count + " ▁ " ) ; } } public static void Main ( ) { int n = 16 ; gouldSequence ( n ) ; } }
Product of maximum in first array and minimum in second | C # program to find the to calculate the product of max element of first array and min element of second array ; Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driver Code
using System ; class GfG { public static int minMaxProduct ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 ) { Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; return arr1 [ n1 - 1 ] * arr2 [ 0 ] ; } public static void Main ( ) { int [ ] arr1 = new int [ ] { 10 , 2 , 3 , 6 , 4 , 1 } ; int [ ] arr2 = new int [ ] { 5 , 1 , 4 , 2 , 6 , 9 } ; int n1 = 6 ; int n2 = 6 ; Console . WriteLine ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ; } }
Minimum odd cost path in a matrix | C # program to find minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code
using System ; public class GFG { public static int M = 100 ; public static int N = 100 ; static int find_min_odd_cost ( int [ , ] given , int m , int n ) { int [ , ] floor = new int [ M , N ] ; int min_odd_cost = 0 ; int i , j , temp ; for ( j = 0 ; j < n ; j ++ ) floor [ 0 , j ] = given [ 0 , j ] ; for ( i = 1 ; i < m ; i ++ ) for ( j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { floor [ i , j ] = given [ i , j ] ; floor [ i , j ] += Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j + 1 ] ) ; } else if ( j == n - 1 ) { floor [ i , j ] = given [ i , j ] ; floor [ i , j ] += Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j - 1 ] ) ; } else { temp = Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j - 1 ] ) ; temp = Math . Min ( temp , floor [ i - 1 , j + 1 ] ) ; floor [ i , j ] = given [ i , j ] + temp ; } } min_odd_cost = int . MaxValue ; for ( j = 0 ; j < n ; j ++ ) { if ( floor [ n - 1 , j ] % 2 == 1 ) { if ( min_odd_cost > floor [ n - 1 , j ] ) min_odd_cost = floor [ n - 1 , j ] ; } } if ( min_odd_cost == int . MinValue ) return - 1 ; return min_odd_cost ; } public static void Main ( ) { int m = 5 , n = 5 ; int [ , ] given = { { 1 , 2 , 3 , 4 , 6 } , { 1 , 2 , 3 , 4 , 5 } , { 1 , 2 , 3 , 4 , 5 } , { 1 , 2 , 3 , 4 , 5 } , { 100 , 2 , 3 , 4 , 5 } } ; Console . Write ( " Minimum ▁ odd ▁ cost ▁ is ▁ " + find_min_odd_cost ( given , m , n ) ) ; } }
Product of maximum in first array and minimum in second | C # program to find the to calculate the product of max element of first array and min element of second array ; Function to calculate the product ; Initialize max of first array ; initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driver Code
using System ; class GfG { public static int minMaxProduct ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 ) { int max = arr1 [ 0 ] ; int min = arr2 [ 0 ] ; int i ; for ( i = 1 ; i < n1 && i < n2 ; ++ i ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; if ( arr2 [ i ] < min ) min = arr2 [ i ] ; } while ( i < n1 ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; i ++ ; } while ( i < n2 ) { if ( arr2 [ i ] < min ) min = arr2 [ i ] ; i ++ ; } return max * min ; } public static void Main ( ) { int [ ] arr1 = new int [ ] { 10 , 2 , 3 , 6 , 4 , 1 } ; int [ ] arr2 = new int [ ] { 5 , 1 , 4 , 2 , 6 , 9 } ; int n1 = 6 ; int n2 = 6 ; Console . WriteLine ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ; } }
Burst Balloon to maximize coins | C # program to illustrate Burst balloon problem ; Add Bordering Balloons ; Declaring DP array ; For a sub - array from indices left , right . This innermost loop finds the last balloon burst ; Driver code ; Size of the array ; Calling function
using System ; class GFG { public static int getMax ( int [ ] A , int N ) { int [ ] B = new int [ N + 2 ] ; B [ 0 ] = B [ N + 1 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) B [ i ] = A [ i - 1 ] ; int [ , ] dp = new int [ ( N + 2 ) , ( N + 2 ) ] ; for ( int length = 1 ; length < N + 1 ; length ++ ) { for ( int left = 1 ; left < N - length + 2 ; left ++ ) { int right = left + length - 1 ; for ( int last = left ; last < right + 1 ; last ++ ) { dp [ left , right ] = Math . Max ( dp [ left , right ] , dp [ left , last - 1 ] + B [ left - 1 ] * B [ last ] * B [ right + 1 ] + dp [ last + 1 , right ] ) ; } } } return dp [ 1 , N ] ; } public static void Main ( ) { int [ ] A = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int N = A . Length ; Console . WriteLine ( getMax ( A , N ) ) ; } }
Search , insert and delete in an unsorted array | C # program to implement linear search in unsorted arrays ; Function to implement search operation ; Driver Code ; Using a last element as search element
using System ; class main { static int findElement ( int [ ] arr , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 12 , 34 , 10 , 6 , 40 } ; int n = arr . Length ; int key = 40 ; int position = findElement ( arr , n , key ) ; if ( position == - 1 ) Console . WriteLine ( " Element ▁ not ▁ found " ) ; else Console . WriteLine ( " Element ▁ Found ▁ at ▁ Position : ▁ " + ( position + 1 ) ) ; } }
Search , insert and delete in an unsorted array | C # program to implement insert operation in an unsorted array . ; Function to insert a given key in the array . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key
using System ; class main { static int insertSorted ( int [ ] arr , int n , int key , int capacity ) { if ( n >= capacity ) return n ; arr [ n ] = key ; return ( n + 1 ) ; } public static void Main ( ) { int [ ] arr = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = 20 ; int n = 6 ; int i , key = 26 ; Console . Write ( " Before ▁ Insertion : ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; n = insertSorted ( arr , n , key , capacity ) ; Console . Write ( " After ▁ Insertion : ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Search , insert and delete in an unsorted array | C # program to implement delete operation in an unsorted array ; Function to search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code
using System ; class main { static int findElement ( int [ ] arr , int n , int key ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } static int deleteElement ( int [ ] arr , int n , int key ) { int pos = findElement ( arr , n , key ) ; if ( pos == - 1 ) { Console . WriteLine ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void Main ( ) { int i ; int [ ] arr = { 10 , 50 , 30 , 40 , 20 } ; int n = arr . Length ; int key = 30 ; Console . Write ( " Array ▁ before ▁ deletion ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; n = deleteElement ( arr , n , key ) ; Console . Write ( " Array ▁ after ▁ deletion ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Search , insert and delete in a sorted array | C # program to implement binary search in a sorted array ; function to implement binary search ; low + ( high - low ) / 2 ; ; Driver Code
using System ; public class GFG { public static int binarySearch ( int [ ] arr , int low , int high , int key ) { if ( high < low ) { return - 1 ; } int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) { return mid ; } if ( key > arr [ mid ] ) { return binarySearch ( arr , ( mid + 1 ) , high , key ) ; } return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 5 , 6 , 7 , 8 , 9 , 10 } ; int n , key ; n = arr . Length ; key = 10 ; Console . WriteLine ( " Index : ▁ " + binarySearch ( arr , 0 , n - 1 , key ) ) ; } }
Search , insert and delete in a sorted array | C # program to insert an element in a sorted array ; Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver program to test above function ; Inserting key
using System ; public class GFG { public static int insertSorted ( int [ ] arr , int n , int key , int capacity ) { if ( n >= capacity ) { return n ; } int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) { arr [ i + 1 ] = arr [ i ] ; } arr [ i + 1 ] = key ; return ( n + 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = arr . Length ; int n = 6 ; int key = 26 ; Console . Write ( " STRNEWLINE Before ▁ Insertion : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } n = insertSorted ( arr , n , key , capacity ) ; Console . Write ( " STRNEWLINE After ▁ Insertion : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Search , insert and delete in a sorted array | C # program to delete an element from a sorted array ; binary search ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code
using System ; public class GFG { static int binarySearch ( int [ ] arr , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } static int deleteElement ( int [ ] arr , int n , int key ) { int pos = binarySearch ( arr , 0 , n - 1 , key ) ; if ( pos == - 1 ) { Console . WriteLine ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void Main ( ) { int i ; int [ ] arr = { 10 , 20 , 30 , 40 , 50 } ; int n = arr . Length ; int key = 30 ; Console . Write ( " Array ▁ before ▁ deletion : STRNEWLINE " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; n = deleteElement ( arr , n , key ) ; Console . Write ( " STRNEWLINE STRNEWLINE Array ▁ after ▁ deletion : STRNEWLINE " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }