codeparrot/xlcost-text-to-code huggingface dataset:
Program to print Step Pattern | C # Program to print Step Pattern ; function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Driver code ; Get the String and the number n ; calling the function
public class solution { using System ; public static void steps ( string str , int n ) { bool flag = false ; int x = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( x == 0 ) { flag = true ; } if ( x == n - 1 ) { flag = false ; } for ( int j = 0 ; j < x ; j ++ ) { Console . Write ( " * " ) ; } Console . Write ( str [ i ] + " STRNEWLINE " ) ; if ( flag == true ) { x ++ ; } else { x -- ; } } } public static void Main ( string [ ] args ) { int n = 4 ; string str = " GeeksForGeeks " ; Console . WriteLine ( " String : ▁ " + str ) ; Console . WriteLine ( " Max ▁ Length ▁ of ▁ Steps : ▁ " + n ) ; steps ( str , n ) ; } }
Frequency Measuring Techniques for Competitive Programming | C # program to count frequencies of array items ; Mark all array elements as not visited ; Traverse through array elements and count frequencies ; Skip this element if already processed ; Count frequency ; Driver Code
using System ; class GFG { static void countFreq ( int [ ] arr , int n ) { Boolean [ ] visited = new Boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == true ) continue ; int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { visited [ j ] = true ; count ++ ; } } Console . WriteLine ( arr [ i ] + " ▁ " + count ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 } ; int n = arr . Length ; countFreq ( arr , n ) ; } }
Check divisibility of binary string by 2 ^ k | C # implementation to check whether given binary number is evenly divisible by 2 ^ k or not ; function to check whether given binary number is evenly divisible by 2 ^ k or not ; count of number of 0 from last ; if count = k , number is evenly divisible , so returns true else false ; Driver program to test above ; first example ; Second example
using System ; class GFG { static bool isDivisible ( String str , int k ) { int n = str . Length ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ; return ( c == k ) ; } public static void Main ( ) { String str1 = "10101100" ; int k = 2 ; if ( isDivisible ( str1 , k ) == true ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No " ) ; String str2 = "111010100" ; k = 2 ; if ( isDivisible ( str2 , k ) == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if any anagram of a string is palindrome or not | C # program to Check if any anagram of a string is palindrome or not ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver program
using System ; public class GFG { static int NO_OF_CHARS = 256 ; static bool canFormPalindrome ( string str ) { int [ ] count = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ str [ i ] ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) != 0 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void Main ( ) { Console . WriteLine ( canFormPalindrome ( " geeksforgeeks " ) ? " Yes " : " No " ) ; Console . WriteLine ( canFormPalindrome ( " geeksogeeks " ) ? " Yes " : " No " ) ; } }
Program to check if input is an integer or a string | C # program to check if a given string is a valid integer ; Returns true if s is a number else false ; Driver code ; Saving the input in a string ; Function returns 1 if all elements are in range '0 ▁ - ▁ 9' ; Function returns 0 if the input is not an integer
using System ; public class GFG { static bool isNumber ( string s ) { for ( int i = 0 ; i < s . Length ; i ++ ) if ( char . IsDigit ( s [ i ] ) == false ) return false ; return true ; } static public void Main ( String [ ] args ) { string str = "6790" ; if ( isNumber ( str ) ) Console . WriteLine ( " Integer " ) ; else Console . WriteLine ( " String " ) ; } }
Print reverse of a string using recursion | C # program to reverse a string using recursion ; Function to print reverse of the passed string ; Driver Code
using System ; class GFG { static void reverse ( String str ) { if ( ( str == null ) || ( str . Length <= 1 ) ) Console . Write ( str ) ; else { Console . Write ( str [ str . Length - 1 ] ) ; reverse ( str . Substring ( 0 , ( str . Length - 1 ) ) ) ; } } public static void Main ( ) { String str = " Geeks ▁ for ▁ Geeks " ; reverse ( str ) ; } }
Probability of distributing given balls into two halves having equal count of distinct colors | C # program for the above approach ; Stores the count of distinct colors in box1 ; Stores the count of distinct colors in box2 ; Function to calculate the required probability ; Calculate factorial from [ 1 , 10 ] ; Assign all distinct balls to second box ; Total number of balls ; Calculate total number of balls ; If K is an odd number ; Total ways of distributing the balls in two equal halves ; Required number of ways ; Return the required probability ; Function to calculate total number of possible distributions which satisfies the given conditions ; If used balls is equal to K / 2 ; If box1 is equal to box2 ; Base condition ; Stores the number of ways of distributing remaining balls without including the current balls in box1 ; Increment box1 by one ; Iterate over the range [ 1 , balls [ i ] ] ; If all the balls goes to box1 , then decrease box2 by one ; Total number of ways of selecting j balls ; Increment res by total number of valid ways of distributing the remaining balls ; Decrement box1 by one ; Increment box2 by 1 ; Function to calculate factorial of N ; Base Case ; Iterate over the range [ 1 , N ] ; Function to calculate NcR ; Driver Code ; Print the result
using System ; public class GFG { static int box1 = 0 ; static int box2 = 0 ; static int [ ] fact = new int [ 11 ] ; public static double getProbability ( int [ ] balls ) { factorial ( 10 ) ; box2 = balls . Length ; int K = 0 ; for ( int i = 0 ; i < balls . Length ; i ++ ) K += balls [ i ] ; if ( K % 2 == 1 ) return 0 ; long all = comb ( K , K / 2 ) ; long validPermutationss = validPermutations ( ( K / 2 ) , balls , 0 , 0 ) ; return ( double ) validPermutationss / all ; } static long validPermutations ( int n , int [ ] balls , int usedBalls , int i ) { if ( usedBalls == n ) { return box1 == box2 ? 1 : 0 ; } if ( i >= balls . Length ) return 0 ; long res = validPermutations ( n , balls , usedBalls , i + 1 ) ; box1 ++ ; for ( int j = 1 ; j <= balls [ i ] ; j ++ ) { if ( j == balls [ i ] ) box2 -- ; long combinations = comb ( balls [ i ] , j ) ; res += combinations * validPermutations ( n , balls , usedBalls + j , i + 1 ) ; } box1 -- ; box2 ++ ; return res ; } static void factorial ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static long comb ( int n , int r ) { long res = fact [ n ] / fact [ r ] ; res /= fact [ n - r ] ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 } ; int N = 4 ; Console . WriteLine ( getProbability ( arr ) ) ; } }
Area of a n | C # Program to find the area of a regular polygon with given radius ; Function to find the area of a regular polygon ; Side and radius cannot be negative ; Area degree converted to radians ; Driver code
using System ; class GFG { static double polyarea ( double n , double r ) { if ( r < 0 && n < 0 ) return - 1 ; double A = ( ( r * r * n ) * Math . Sin ( ( 360 / n ) * 3.14159 / 180 ) ) / 2 ; return A ; } public static void Main ( ) { float r = 9 , n = 6 ; Console . WriteLine ( polyarea ( n , r ) ) ; } }
Check if a line at 45 degree can divide the plane into two equal weight parts |  ; Checking if a plane can be divide by a line at 45 degrees such that weight sum is equal ; Rotating each point by 45 degrees and calculating prefix sum . Also , finding maximum and minimum x coordinates ; storing weight sum upto x - y point ; Finding prefix sum ; Line passes through i , so it neither falls left nor right . ; Driven code
using System ; using System . Collections . Generic ; public class GFG { static void is_partition_possible ( int n , int [ ] x , int [ ] y , int [ ] w ) { Dictionary < int , int > weight_at_x = new Dictionary < int , int > ( ) ; int max_x = ( int ) - 2e3 , min_x = ( int ) 2e3 ; for ( int i = 0 ; i < n ; i ++ ) { int new_x = x [ i ] - y [ i ] ; max_x = Math . Max ( max_x , new_x ) ; min_x = Math . Min ( min_x , new_x ) ; if ( weight_at_x . ContainsKey ( new_x ) ) { weight_at_x [ new_x ] += w [ i ] ; } else { weight_at_x . Add ( new_x , w [ i ] ) ; } } List < int > sum_till = new List < int > ( ) ; sum_till . Add ( 0 ) ; for ( int s = min_x ; s <= max_x ; s ++ ) { if ( ! weight_at_x . ContainsKey ( s ) ) { sum_till . Add ( sum_till [ sum_till . Count - 1 ] ) ; } else { sum_till . Add ( sum_till [ sum_till . Count - 1 ] + weight_at_x [ s ] ) ; } } int total_sum = sum_till [ sum_till . Count - 1 ] ; int partition_possible = 0 ; for ( int i = 1 ; i < sum_till . Count ; i ++ ) { if ( sum_till [ i ] == total_sum - sum_till [ i ] ) partition_possible = 1 ; if ( sum_till [ i - 1 ] == total_sum - sum_till [ i ] ) partition_possible = 1 ; } Console . WriteLine ( partition_possible == 1 ? " YES " : " NO " ) ; } static public void Main ( ) { int n = 3 ; int [ ] x = { - 1 , - 2 , 1 } ; int [ ] y = { 1 , 1 , - 1 } ; int [ ] w = { 3 , 1 , 4 } ; is_partition_possible ( n , x , y , w ) ; } }
Slope of perpendicular to line | C # Program to find Slope of perpendicular to line ; Function to find the Slope of other line ; Driver Code
using System ; class GFG { static double findPCSlope ( double m ) { return - 1.0 / m ; } public static void Main ( ) { double m = 2.0 ; Console . Write ( findPCSlope ( m ) ) ; } }
Program to find area of a Circular Segment | C # Program to find area of segment of a circle ; Function to find area of segment ; Calculating area of sector ; Calculating area of triangle ; Driver Function
using System ; class GFG { static float pi = 3.14159f ; static float area_of_segment ( float radius , float angle ) { float area_of_sector = pi * ( radius * radius ) * ( angle / 360 ) ; float area_of_triangle = ( float ) 1 / 2 * ( radius * radius ) * ( float ) Math . Sin ( ( angle * pi ) / 180 ) ; return area_of_sector - area_of_triangle ; } public static void Main ( ) { float radius = 10.0f , angle = 90.0f ; Console . WriteLine ( " Area ▁ of ▁ minor ▁ segment ▁ = ▁ " + area_of_segment ( radius , angle ) ) ; Console . WriteLine ( " Area ▁ of ▁ major ▁ segment ▁ = ▁ " + area_of_segment ( radius , ( 360 - angle ) ) ) ; } }
Area of a Circular Sector | C # program to find Area of a Sector ; Calculating area of the sector ; Driver code
using System ; class GFG { static void SectorArea ( double radius , double angle ) { if ( angle >= 360 ) Console . WriteLine ( " Angle ▁ not ▁ possible " ) ; else { double sector = ( ( 22 * radius * radius ) / 7 ) * ( angle / 360 ) ; Console . WriteLine ( sector ) ; } } public static void Main ( ) { double radius = 9 ; double angle = 60 ; SectorArea ( radius , angle ) ; } }
Make two numbers equal by multiplying with their prime factors minimum number of times | C # program for the above approach ; Everything divides 0 ; Function to calculate total number of prime factor with their prime factor ; Iterate while the number is even ; Reduce to half ; Iterate up to sqrt ( N ) ; Iterate while N has factors of i ; Removing one factor of i ; Function to count the number of factors ; Find the GCD ; Find multiples left in X and Y ; Find prime factor of multiple left in X and Y ; Initialize ans ; Check if it possible to obtain X or not ; Check if it possible to obtain Y or not ; Return main ans ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static Dictionary < int , int > PrimeFactor ( int N ) { Dictionary < int , int > primef = new Dictionary < int , int > ( ) ; while ( N % 2 == 0 ) { if ( primef . ContainsKey ( 2 ) ) { primef [ 2 ] ++ ; } else { primef [ 2 ] = 1 ; } N = N / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( N ) ; i ++ ) { while ( N % i == 0 ) { if ( primef . ContainsKey ( i ) ) { primef [ i ] ++ ; } else { primef [ i ] = 1 ; } N = N / 2 ; } } if ( N > 2 ) { primef [ N ] = 1 ; } return primef ; } static int CountToMakeEqual ( int X , int Y ) { int gcdofXY = gcd ( X , Y ) ; int newX = Y / gcdofXY ; int newY = X / gcdofXY ; Dictionary < int , int > primeX = PrimeFactor ( newX ) ; Dictionary < int , int > primeY = PrimeFactor ( newY ) ; int ans = 0 ; foreach ( KeyValuePair < int , int > keys in primeX ) { if ( X % keys . Key != 0 ) { return - 1 ; } ans += primeX [ keys . Key ] ; } foreach ( KeyValuePair < int , int > keys in primeY ) { if ( Y % keys . Key != 0 ) { return - 1 ; } ans += primeY [ keys . Key ] ; } return ans ; } static void Main ( ) { int X = 36 ; int Y = 48 ; int ans = CountToMakeEqual ( X , Y ) ; Console . Write ( ans ) ; } }
Make given segments non | C # program for the above approach ; Stores the details of the Segment ; Function to check whether the graph is bipartite or not ; Mark source node as visited ; Push the source vertex in queue ; Get the front of the queue ; Assign the color to the popped node ; Traverse the adjacency list of the node u ; If any node is visited & a different colors has been assigned , then return false ; Set visited [ x ] ; Push the node x into the queue ; Update color of node ; If the graph is bipartite ; Function to add an edge between the nodes u and v ; Function to check if the assignment of direction can be possible to all the segments , such that they do not intersect after a long period of time ; Stores the adjacency list of the created graph ; Initialize ; Generate all possible pairs ; If segments do not overlap ; Otherwise , the segments overlap ; If both segments have same speed , then add an edge ; Keep the track of visited nodes ; Iterate for all possible nodes ; Check whether graph is bipartite or not ; If the graph is bipartite ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class Node { public int L , R , V ; } ; static Node newNode ( int L , int R , int V ) { Node temp = new Node ( ) ; temp . L = L ; temp . R = R ; temp . V = V ; return temp ; } static bool check ( List < int > [ ] Adj , int Src , int N , bool [ ] visited ) { int [ ] color = new int [ N ] ; visited [ Src ] = true ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( Src ) ; while ( q . Count > 0 ) { int u = q . Peek ( ) ; q . Dequeue ( ) ; int Col = color [ u ] ; foreach ( int x in Adj [ u ] ) { if ( visited [ x ] == true && color [ x ] == Col ) { return false ; } else if ( visited [ x ] == false ) { visited [ x ] = true ; q . Enqueue ( x ) ; color [ x ] = 1 - Col ; } } } return true ; } static void addEdge ( List < int > [ ] Adj , int u , int v ) { Adj [ u ] . Add ( v ) ; Adj [ v ] . Add ( u ) ; } static void isPossible ( Node [ ] Arr , int N ) { List < int > [ ] Adj = new List < int > [ N ] ; for ( int i = 0 ; i < N ; i ++ ) Adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] . R < Arr [ j ] . L Arr [ i ] . L > Arr [ j ] . R ) { continue ; } else { if ( Arr [ i ] . V == Arr [ j ] . V ) { addEdge ( Adj , i , j ) ; } } } } bool [ ] visited = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( visited [ i ] == false && Adj [ i ] . Count > 0 ) { if ( check ( Adj , i , N , visited ) == false ) { Console . Write ( " No " ) ; return ; } } } Console . Write ( " Yes " ) ; } public static void Main ( ) { Node [ ] arr = { newNode ( 5 , 7 , 2 ) , newNode ( 4 , 6 , 1 ) , newNode ( 1 , 5 , 2 ) , newNode ( 6 , 5 , 1 ) } ; int N = arr . Length ; isPossible ( arr , N ) ; } }
Generate all numbers up to N in Lexicographical Order | C # program for the above approach ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void lexNumbers ( int n ) { List < int > sol = new List < int > ( ) ; dfs ( 1 , n , sol ) ; Console . WriteLine ( " [ " + string . Join ( " , ▁ " , sol ) + " ] " ) ; } public static void dfs ( int temp , int n , List < int > sol ) { if ( temp > n ) return ; sol . Add ( temp ) ; dfs ( temp * 10 , n , sol ) ; if ( temp % 10 != 9 ) dfs ( temp + 1 , n , sol ) ; } public static void Main ( ) { int n = 15 ; lexNumbers ( n ) ; } }
Minimum number of swaps required to sort an array of first N number | C # program to find the minimum number of swaps required to sort the given array ; Function to find minimum swaps ; Initialise count variable ; If current element is not at the right position ; Swap current element with correct position of that element ; Increment for next index when current element is at correct position ; Driver code ; Function to find minimum swaps
using System ; class GfG { static int minimumSwaps ( int [ ] arr ) { int count = 0 ; int i = 0 ; while ( i < arr . Length ) { if ( arr [ i ] != i + 1 ) { while ( arr [ i ] != i + 1 ) { int temp = 0 ; temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; count ++ ; } } i ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 1 , 5 } ; Console . WriteLine ( minimumSwaps ( arr ) ) ; } }
Recursive Selection Sort | Recursive C # program to sort an array using selection sort ; Return minimum index ; Find minimum of remaining elements ; Return minimum of current and remaining . ; Recursive selection sort . n is size of a [ ] and index is index of starting element . ; Return when starting and size are same ; calling minimum index function for minimum index ; Swapping when index and minimum index are not same ; swap ; Recursively calling selection sort function ; Driver Code ; Calling function ; printing sorted array
using System ; class GFG { static int minIndex ( int [ ] a , int i , int j ) { if ( i == j ) return i ; int k = minIndex ( a , i + 1 , j ) ; return ( a [ i ] < a [ k ] ) ? i : k ; } static void recurSelectionSort ( int [ ] a , int n , int index ) { if ( index == n ) return ; int k = minIndex ( a , index , n - 1 ) ; if ( k != index ) { int temp = a [ k ] ; a [ k ] = a [ index ] ; a [ index ] = temp ; } recurSelectionSort ( a , n , index + 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 5 , 2 , 7 , 0 } ; recurSelectionSort ( arr , arr . Length , 0 ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Recursive Insertion Sort | Recursive C # program for insertion sort ; Recursive function to sort an array using insertion sort ; Base case ; Sort first n - 1 elements ; Insert last element at its correct position in sorted array . ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; Driver Code
using System ; class GFG { static void insertionSortRecursive ( int [ ] arr , int n ) { if ( n <= 1 ) return ; insertionSortRecursive ( arr , n - 1 ) ; int last = arr [ n - 1 ] ; int j = n - 2 ; while ( j >= 0 && arr [ j ] > last ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = last ; } static void Main ( ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 } ; insertionSortRecursive ( arr , arr . Length ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Recursive Bubble Sort | C # program for recursive implementation of Bubble sort ; A function to implement bubble sort ; Base case ; One pass of bubble sort . After this pass , the largest element is moved ( or bubbled ) to end . ; swap arr [ i ] , arr [ i + 1 ] ; Largest element is fixed , recur for remaining array ; Driver code
using System ; class GFG { static void bubbleSort ( int [ ] arr , int n ) { if ( n == 1 ) return ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } bubbleSort ( arr , n - 1 ) ; } static void Main ( ) { int [ ] arr = { 64 , 34 , 25 , 12 , 22 , 11 , 90 } ; bubbleSort ( arr , arr . Length ) ; Console . WriteLine ( " Sorted ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Maximum possible difference of sum of two subsets of an array | Set 2 | C # Program for the above approach ; Stores the positive elements ; Stores the negative elements ; Stores the count of 0 s ; Sum of all positive numbers ; Sum of all negative numbers ; Iterate over the array ; Stores the difference ; Sort the positive numbers in ascending order ; Sort the negative numbers in decreasing order ; Case 1 : Include both positive and negative numbers ; Put all numbers in subset A and one 0 in subset B ; Put all numbers in subset A except the smallest positive number which is put in B ; Put all numbers in subset B and one 0 in subset A ; Place the largest negative number in subset A and remaining in B ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxSumAfterPartition ( int [ ] arr , int n ) { List < int > pos = new List < int > ( ) ; List < int > neg = new List < int > ( ) ; int zero = 0 ; int pos_sum = 0 ; int neg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { pos . Add ( arr [ i ] ) ; pos_sum += arr [ i ] ; } else if ( arr [ i ] < 0 ) { neg . Add ( arr [ i ] ) ; neg_sum += arr [ i ] ; } else { zero ++ ; } } int ans = 0 ; pos . Sort ( ) ; neg . Sort ( ) ; neg . Reverse ( ) ; if ( pos . Count > 0 && neg . Count > 0 ) { ans = ( pos_sum - neg_sum ) ; } else if ( pos . Count > 0 ) { if ( zero > 0 ) { ans = ( pos_sum ) ; } else { ans = ( pos_sum - 2 * pos [ 0 ] ) ; } } else { if ( zero > 0 ) { ans = ( - 1 * neg_sum ) ; } else { ans = ( neg [ 0 ] - ( neg_sum - neg [ 0 ] ) ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , - 5 , - 7 } ; int n = arr . Length ; Console . Write ( maxSumAfterPartition ( arr , n ) ) ; } }
Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values | C # program for the above approach ; Function to find the bitwise OR of array elements ; Stores the resultant maximum value of Bitwise XOR ; Traverse the array arr [ ] ; Return the maximum value res ; Driver code
using System ; class GFG { static int MaxXOR ( int [ ] arr , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { res |= arr [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 7 } ; int N = arr . Length ; Console . Write ( MaxXOR ( arr , N ) ) ; } }
Count number of common elements between a sorted array and a reverse sorted array | C # program for the above approach ; Function to count the number of elements common in both the arrays ; Used to traverse array A [ ] and B [ ] from the front and the back ; Stores the count of numbers common in both array ; If A [ first ] is less than B [ second ] ; Increment the value of first ; IF B [ second ] is less than A [ first ] ; Decrement the value of second ; A [ first ] is equal to B [ second ] ; Increment the value of count ; Increment the value of first ; Decrement the value of second ; Return the value of count ; Driver code
using System ; class GFG { static int countEqual ( int [ ] A , int [ ] B , int N ) { int first = 0 ; int second = N - 1 ; int count = 0 ; while ( first < N && second >= 0 ) { if ( A [ first ] < B [ second ] ) { first ++ ; } else if ( B [ second ] < A [ first ] ) { second -- ; } else { count ++ ; first ++ ; second -- ; } } return count ; } static void Main ( ) { int [ ] A = { 2 , 4 , 5 , 8 , 12 , 13 , 17 , 18 , 20 , 22 , 309 , 999 } ; int [ ] B = { 109 , 99 , 68 , 54 , 22 , 19 , 17 , 13 , 11 , 5 , 3 , 1 } ; int N = A . Length ; Console . WriteLine ( countEqual ( A , B , N ) ) ; } }