codeparrot/xlcost-text-to-code huggingface dataset:
Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | C # program to print all elements that appear more than once . ; Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Code
using System ; class GFG { static void printDuplicates ( int [ ] arr , int n ) { int i ; int fl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ arr [ i ] % n ] >= n ) { if ( arr [ arr [ i ] % n ] < 2 * n ) { Console . Write ( arr [ i ] % n + " ▁ " ) ; fl = 1 ; } } arr [ arr [ i ] % n ] += n ; } if ( ! ( fl > 0 ) ) Console . Write ( " - 1" ) ; } public static void Main ( ) { int [ ] arr = { 1 , 6 , 3 , 1 , 3 , 6 , 6 } ; int arr_size = arr . Length ; printDuplicates ( arr , arr_size ) ; } }
Find the smallest positive number missing from an unsorted array | Set 2 | C # program to find the smallest positive missing number ; Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code
using System ; class GFG { static int findMissingNo ( int [ ] arr , int n ) { int val ; int nextval ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= 0 arr [ i ] > n ) continue ; val = arr [ i ] ; while ( arr [ val - 1 ] != val ) { nextval = arr [ val - 1 ] ; arr [ val - 1 ] = val ; val = nextval ; if ( val <= 0 val > n ) break ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 } ; int arr_size = arr . Length ; int missing = findMissingNo ( arr , arr_size ) ; Console . Write ( " The ▁ smallest ▁ positive " + " ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }
Print all triplets with given sum | A simple C # program to find three elements whose sum is equal to given sum ; Prints all triplets in arr [ ] with given sum ; Driver code
using System ; class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { 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 ] == sum ) { Console . WriteLine ( arr [ i ] + " ▁ " + arr [ j ] + " ▁ " + arr [ k ] ) ; } } } } } static public void Main ( ) { int [ ] arr = { 0 , - 1 , 2 , - 3 , 1 } ; int n = arr . Length ; findTriplets ( arr , n , - 2 ) ; } }
Print all triplets with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; function to print triplets with given sum ; Find all pairs with sum equals to " sum - arr [ i ] " ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . Contains ( x ) ) Console . Write ( " { 0 } ▁ { 1 } ▁ { 2 } STRNEWLINE " , x , arr [ i ] , arr [ j ] ) ; else s . Add ( arr [ j ] ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . Length ; findTriplets ( arr , n , sum ) ; } }
Maximum product quadruple ( sub | A C # program to find a maximum product of a quadruple in array of integers ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; will contain max product ; Driver Code
using System ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 4 ) return - 1 ; int max_product = int . MinValue ; for ( int i = 0 ; i < n - 3 ; i ++ ) for ( int j = i + 1 ; j < n - 2 ; j ++ ) for ( int k = j + 1 ; k < n - 1 ; k ++ ) for ( int l = k + 1 ; l < n ; l ++ ) max_product = Math . Max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] * arr [ l ] ) ; return max_product ; } public static void Main ( ) { int [ ] arr = { 10 , 3 , 5 , 6 , 20 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . WriteLine ( " No ▁ Quadruple ▁ Exists " ) ; else Console . WriteLine ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
Maximum product quadruple ( sub | A C # program to find a maximum product of a quadruple in array of integers ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Sort the array in ascending order ; Return the maximum of x , y and z ; Driver Code
using System ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 4 ) return - 1 ; Array . Sort ( arr ) ; int x = arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] * arr [ n - 4 ] ; int y = arr [ 0 ] * arr [ 1 ] * arr [ 2 ] * arr [ 3 ] ; int z = arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] * arr [ n - 2 ] ; return Math . Max ( x , Math . Max ( y , z ) ) ; } public static void Main ( ) { int [ ] arr = { - 10 , - 3 , 5 , 6 , - 20 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . WriteLine ( " No ▁ Quadruple ▁ Exists " ) ; else Console . WriteLine ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
Minimum value of " max ▁ + ▁ min " in a subarray | C # program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { if ( n < 2 ) return - 1 ; int ans = arr [ 0 ] + arr [ 1 ] ; for ( int i = 1 ; i + 1 < n ; i ++ ) ans = Math . Min ( ans , ( arr [ i ] + arr [ i + 1 ] ) ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 12 , 2 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }
Stella Octangula Number | Program to check if a number is Stella Octangula Number ; Returns value of n * ( 2 * n * n - 1 ) ; Finds if a value of f ( n ) is equal to x where n is in interval [ low . . high ] ; Returns true if x is Stella Octangula Number . Else returns false . ; Find ' high ' for binary search by repeated doubling ; If condition is satisfied for a power of 2. ; Call binary search ; Driver code
using System ; class GFG { static int f ( int n ) { return n * ( 2 * n * n - 1 ) ; } static bool binarySearch ( int low , int high , int x ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( f ( mid ) < x ) low = mid + 1 ; else if ( f ( mid ) > x ) high = mid - 1 ; else return true ; } return false ; } static bool isStellaOctangula ( int x ) { if ( x == 0 ) return true ; int i = 1 ; while ( f ( i ) < x ) i = i * 2 ; if ( f ( i ) == x ) return true ; return binarySearch ( i / 2 , i , x ) ; } public static void Main ( ) { int n = 51 ; if ( isStellaOctangula ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sort the given string using character search | C # implementation to sort the given string without using any sorting technique ; function to sort the given string without using any sorting technique ; to store the final sorted string ; for each character ' i ' ; if character ' i ' is present at a particular index then add character ' i ' to ' new _ str ' ; required final sorted string ; Driver code
using System ; class GFG { static String sortString ( String str , int n ) { String new_str = " " ; for ( int i = ' a ' ; i <= ' z ' ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( str [ j ] == i ) new_str += str [ j ] ; return new_str ; } public static void Main ( ) { String str = " geeksforgeeks " ; int n = str . Length ; Console . Write ( sortString ( str , n ) ) ; } }
Sort the given string using character search | C # implementation to sort the given String without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to ' z ' ; to store the readonly sorted String ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver code
using System ; class GFG { static String sortString ( String str , int n ) { int i ; char [ ] arr = new char [ 26 ] ; String new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str [ i ] - ' a ' ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- > 0 ) { new_str += String . Join ( " " , ( char ) ( i + ' a ' ) ) ; } return new_str ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . Length ; Console . Write ( sortString ( str , n ) ) ; } }
Maximum occurring character in a linked list | C # program to count the maximum occurring character in linked list ; Link list node ; Storing element 's frequencies  in a hash table. ; calculating the first maximum element ; Push a node to linked list . Note that this function changes the head ; Driver Code
using System ; public class GFG { class Node { public char data ; public Node next ; } ; static Node head ; static char maxChar ( Node head ) { Node p = head ; int [ ] hash = new int [ 256 ] ; while ( p != null ) { hash [ p . data ] ++ ; p = p . next ; } p = head ; int max = - 1 ; char res = ' \x0000' ; while ( p != null ) { if ( max < hash [ p . data ] ) { res = p . data ; max = hash [ p . data ] ; } p = p . next ; } return res ; } static void push ( Node head_ref , char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } public static void Main ( String [ ] args ) { head = null ; char [ ] str = " skeegforskeeg " . ToCharArray ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { push ( head , str [ i ] ) ; } Console . WriteLine ( maxChar ( head ) ) ; } }
Maximum sum of elements from each row in the matrix | C # Program to find row - wise maximum element sum considering elements in increasing order . ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver Program
using System ; class GFG { static int N = 3 ; static int getGreatestSum ( int [ , ] a ) { int prev_max = 0 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max < a [ N - 1 , j ] ) prev_max = a [ N - 1 , j ] ; int sum = prev_max ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int curr_max = - 2147483648 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max > a [ i , j ] && a [ i , j ] > curr_max ) curr_max = a [ i , j ] ; if ( curr_max == - 2147483648 ) return - 1 ; prev_max = curr_max ; sum += prev_max ; } return sum ; } public static void Main ( ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . WriteLine ( getGreatestSum ( a ) ) ; int [ , ] b = { { 4 , 5 , 6 } , { 4 , 5 , 6 } , { 4 , 5 , 6 } } ; Console . WriteLine ( getGreatestSum ( b ) ) ; } }
Find the number of times every day occurs in a month | C # program to count occurrence of days in a month ; function to find occurrences ; stores days in a week ; Initialize all counts as 4. ; find index of the first day ; number of days whose occurrence will be 5 ; mark the occurrence to be 5 of n - 28 days ; print the days ; Driver function
using System ; public class GfG { public static void occurrenceDays ( int n , string firstday ) { String [ ] days = new String [ ] { " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " , " Saturday " , " Sunday " } ; int [ ] count = new int [ 7 ] ; for ( int i = 0 ; i < 7 ; i ++ ) count [ i ] = 4 ; int pos = 0 ; for ( int i = 0 ; i < 7 ; i ++ ) { if ( firstday == days [ i ] ) { pos = i ; break ; } } int inc = n - 28 ; for ( int i = pos ; i < pos + inc ; i ++ ) { if ( i > 6 ) count [ i % 7 ] = 5 ; else count [ i ] = 5 ; } for ( int i = 0 ; i < 7 ; i ++ ) { Console . WriteLine ( days [ i ] + " ▁ " + count [ i ] ) ; } } public static void Main ( ) { int n = 31 ; string firstday = " Tuesday " ; occurrenceDays ( n , firstday ) ; } }
Value of k | C # program to fin k - th element after append and insert middle operations ; Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code
using System ; class GFG { static void findElement ( int n , int k ) { int ans = n ; int left = 1 ; int right = ( int ) ( Math . Pow ( 2 , n ) - 1 ) ; while ( true ) { int mid = ( left + right ) / 2 ; if ( k == mid ) { Console . WriteLine ( ans ) ; break ; } ans -- ; if ( k < mid ) { right = mid - 1 ; } else { left = mid + 1 ; } } } public static void Main ( ) { int n = 4 , k = 8 ; findElement ( n , k ) ; } }
First common element in two linked lists | C # program to find first common element in two unsorted linked list ; Link list node ; A utility function to insert a node at the beginning of a linked list ; Returns the first repeating element in linked list ; Traverse through every node of first list ; If current node is present in second list ; If no common node ; Driver code
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int firstCommon ( Node head1 , Node head2 ) { for ( ; head1 != null ; head1 = head1 . next ) for ( Node p = head2 ; p != null ; p = p . next ) if ( p . data == head1 . data ) return head1 . data ; return 0 ; } public static void Main ( String [ ] args ) { Node head1 = null ; head1 = push ( head1 , 20 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 15 ) ; head1 = push ( head1 , 10 ) ; Node head2 = null ; head2 = push ( head2 , 10 ) ; head2 = push ( head2 , 2 ) ; head2 = push ( head2 , 15 ) ; head2 = push ( head2 , 8 ) ; Console . WriteLine ( firstCommon ( head1 , head2 ) ) ; } }
Print pair with maximum AND value in an array | C # Program to find pair with maximum AND value ; Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver code
using System ; class GFG { static int checkBit ( int pattern , int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int [ ] arr , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } if ( res == 0 ) Console . Write ( " Not ▁ Possible " ) ; else { Console . Write ( " Pair ▁ = ▁ " ) ; count = 0 ; for ( int i = 0 ; i < n && count < 2 ; i ++ ) { if ( ( arr [ i ] & res ) == res ) { count ++ ; Console . Write ( arr [ i ] + " ▁ " ) ; } } Console . WriteLine ( ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }
Probability of choosing a random pair with maximum sum in an array | C # program of choosing a random pair such that A [ i ] + A [ j ] is maximum . ; Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; frequency of first maximum ; frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver Code
using System ; public class GFG { static int countMaxSumPairs ( int [ ] a , int n ) { int first = int . MinValue , second = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > first ) { second = first ; first = a [ i ] ; } else if ( a [ i ] > second && a [ i ] != first ) second = a [ i ] ; } int cnt1 = 0 , cnt2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == first ) cnt1 ++ ; if ( a [ i ] == second ) cnt2 ++ ; } if ( cnt1 == 1 ) return cnt2 ; if ( cnt1 > 1 ) return cnt1 * ( cnt1 - 1 ) / 2 ; return 0 ; } static float findMaxSumProbability ( int [ ] a , int n ) { int total = n * ( n - 1 ) / 2 ; int max_sum_pairs = countMaxSumPairs ( a , n ) ; return ( float ) max_sum_pairs / ( float ) total ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 2 , 3 } ; int n = a . Length ; ; Console . WriteLine ( findMaxSumProbability ( a , n ) ) ; } }
The painter 's partition problem |  ; sum from 1 to i elements of arr
int [ ] sum = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ 1 , i ] = sum [ i ] ; and using as : best = Math . Min ( best , Math . Max ( dp [ i - 1 ] [ p ] , sum [ j ] - sum [ p ] ) ) ;
The painter 's partition problem |
for ( int i = k - 1 ; i <= n ; i ++ ) best = Math . Min ( best , Math . Max ( partition ( arr , i , k - 1 ) , sum ( arr , i , n - 1 ) ) ) ;
Find if given number is sum of first n natural numbers | C # program for above implementation ; Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Drivers code
using System ; class GFG { static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n ; if ( sum == s ) return n ; } return - 1 ; } public static void Main ( ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }
Find if given number is sum of first n natural numbers | C # program of the above approach ; Function to check if the s is the sum of first N natural number ; Solution of Quadratic Equation ; Condition to check if the solution is a integer ; Driver code ; Function call
using System ; class GFG { public static int isvalid ( int s ) { double k = ( - 1.0 + Math . Sqrt ( 1 + 8 * s ) ) / 2 ; if ( Math . Ceiling ( k ) == Math . Floor ( k ) ) return ( int ) k ; else return - 1 ; } public static void Main ( string [ ] args ) { int s = 15 ; Console . Write ( isvalid ( s ) ) ; } }
Save from Bishop in chessboard | Program to find the total safe positions to place your Bishop ; function to calc total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; Driver function
using System ; class GFG { static int calcSafe ( int pos ) { int j = pos % 10 ; int i = pos / 10 ; int dis_11 = Math . Min ( Math . Abs ( 1 - i ) , Math . Abs ( 1 - j ) ) ; int dis_18 = Math . Min ( Math . Abs ( 1 - i ) , Math . Abs ( 8 - j ) ) ; int dis_81 = Math . Min ( Math . Abs ( 8 - i ) , Math . Abs ( 1 - j ) ) ; int dis_88 = Math . Min ( Math . Abs ( 8 - i ) , Math . Abs ( 8 - j ) ) ; int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1 ; return ( 64 - sum ) ; } public static void Main ( ) { int pos = 34 ; Console . WriteLine ( " Safe ▁ Positions ▁ = ▁ " + calcSafe ( pos ) ) ; } }
Counting cross lines in an array | C # program to count cross line in array ; Merges two subarrays of [ ] arr . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; create temp arrays ; Copy data to temp arrays L [ ] and R [ ] ; i = 0 ; Initial index of first subarray j = 0 ; Initial index of second subarray k = l ; Initial index of merged subarray ; == == == == == == == == == == == == == == == == == == == == == = MAIN PORTION OF CODE == == == == == == == == == == == == == == == == == == == == = add all line which is cross by current element ; Copy the remaining elements of L [ ] , if there are any ; Copy the remaining elements of R [ ] , if there are any ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; function return count of cross line in an array ; Driver Code
using System ; class GFG { static int count_crossline ; static void merge ( int [ ] arr , int l , int m , int r ) { int i , j , k ; int n1 = m - l + 1 ; int n2 = r - m ; int [ ] L = new int [ n1 ] ; int [ ] R = new int [ n2 ] ; for ( i = 0 ; i < n1 ; i ++ ) { L [ i ] = arr [ l + i ] ; } for ( j = 0 ; j < n2 ; j ++ ) { R [ j ] = arr [ m + 1 + j ] ; } while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; count_crossline += ( n1 - i ) ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } static void mergeSort ( int [ ] arr , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static int countCrossLine ( int [ ] arr , int n ) { mergeSort ( arr , 0 , n - 1 ) ; return count_crossline ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( countCrossLine ( arr , n ) ) ; } }
Count number of elements between two given elements in array | C # Program to count number of elements between two given elements . ; Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements between the two elements . ; Driver Code
using System ; class GFG { static int getCount ( int [ ] arr , int n , int num1 , int num2 ) { int i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; int j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 } ; int n = arr . Length ; int num1 = 5 , num2 = 4 ; Console . WriteLine ( getCount ( arr , n , num1 , num2 ) ) ; } }
Best meeting point in 2D binary array | C # program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int [ , ] grid ) { if ( ROW == 0 COL == 0 ) return 0 ; List < int > vertical = new List < int > ( ) ; List < int > horizontal = new List < int > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i , j ] == 1 ) { vertical . Add ( i ) ; horizontal . Add ( j ) ; } } } vertical . Sort ( ) ; horizontal . Sort ( ) ; int size = vertical . Count / 2 ; int x = vertical [ size ] ; int y = horizontal [ size ] ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i , j ] == 1 ) distance += Math . Abs ( x - i ) + Math . Abs ( y - j ) ; return distance ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; Console . WriteLine ( minTotalDistance ( grid ) ) ; } }