codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increment or decrement required to sort the array | Top | C # program of the above approach ; Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then [ ] arr is sorted ; If dp [ N , maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N , maxE ] ; Return the readonly result ; Driver Code ; Find the minimum and maximum element from the [ ] arr ; Function call
using System ; using System . Linq ; class GFG { static int [ , ] dp = new int [ 1000 , 1000 ] ; static int minimumIncDec ( int [ ] arr , int N , int maxE , int minE ) { if ( N == 0 ) { return 0 ; } if ( dp [ N , maxE ] != 0 ) return dp [ N , maxE ] ; int ans = int . MaxValue ; for ( int k = minE ; k <= maxE ; k ++ ) { int x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = Math . Min ( ans , x + Math . Abs ( arr [ N - 1 ] - k ) ) ; } dp [ N , maxE ] = ans ; return dp [ N , maxE ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; int minE = arr . Min ( ) ; int maxE = arr . Max ( ) ; Console . Write ( minimumIncDec ( arr , N , maxE , minE ) ) ; } }
Sort the major diagonal of the matrix | C # implementation to sort the major diagonal of the matrix ; Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
using System ; class GFG { static void sortDiagonal ( int [ , ] a , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i , i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j , j ] ) { sm = a [ j , j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( a [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static void swap ( int [ , ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i , i2 ] ; a [ i , i2 ] = a [ pos , pos2 ] ; a [ pos , pos2 ] = temp ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }
Minimum cost to make an Array a permutation of first N natural numbers | C # program to calculate minimum cost to make an Array a permutation of first N natural numbers ; Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int make_permutation ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += Math . Abs ( i + 1 - arr [ i ] ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 3 , 8 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( make_permutation ( arr , n ) ) ; } }
Find maximum sum from top to bottom row with no adjacent diagonal elements | C # implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver code ; Function to find maximum path
using System ; using System . Collections . Generic ; class GFG { static int maxSum ( int [ , ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { List < Tuple < int , int > > aux = new List < Tuple < int , int > > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . Add ( new Tuple < int , int > ( V [ i + 1 , j ] , j ) ) ; } aux . Sort ( ) ; aux . Reverse ( ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux [ k ] . Item2 - j == 0 || Math . Abs ( aux [ k ] . Item2 - j ) > 1 ) { V [ i , j ] += aux [ k ] . Item1 ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . Max ( ans , V [ 0 , i ] ) ; } return ans ; } static void Main ( ) { int [ , ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . GetLength ( 0 ) ; int m = V . GetLength ( 1 ) ; Console . WriteLine ( maxSum ( V , n , m ) ) ; } }
Minimum sum of product of elements of pairs of the given array | C # program to find the minimum product of sum of pair of element in array arr [ ] ; Function to find the minimum product ; Sort the array sort ( ) function ; Initialise product to 1 ; Find product of sum of all pairs ; Return the product ; Driver code ; Function call to find product
using System ; class GFG { static int minimumProduct ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int product = 1 ; for ( int i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 6 , 3 , 1 , 7 , 8 } ; int n = arr . Length ; Console . Write ( minimumProduct ( arr , n ) ) ; } }
Check if elements of array can be arranged in AP , GP or HP | C # program to check if a given array form AP , GP or HP ; Returns true if arr [ 0. . n - 1 ] can form AP ; Base Case ; Sort array ; After sorting , difference between consecutive elements must be same . ; Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form GP ; Base Case ; Sort array ; After sorting , common ratio between consecutive elements must be same . ; Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form HP ; Base Case ; Find reciprocal of [ ] arr ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . ; Driver 's Code ; Function to check AP ; Function to check GP ; Function to check HP
using System ; class GFG { static bool checkIsAP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != d ) { return false ; } } return true ; } static bool checkIsGP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } static bool checkIsHP ( double [ ] arr , int n ) { if ( n == 1 ) { return true ; } double [ ] rec = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } if ( checkIsAP ( rec , n ) ) return true ; else return false ; } public static void Main ( String [ ] args ) { double [ ] arr = { 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 } ; int n = arr . Length ; int flag = 0 ; if ( checkIsAP ( arr , n ) ) { Console . Write ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " + " STRNEWLINE " ) ; flag = 1 ; } if ( checkIsGP ( arr , n ) ) { Console . Write ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " + " STRNEWLINE " ) ; flag = 1 ; } if ( checkIsHP ( arr , n ) ) { Console . Write ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " + " STRNEWLINE " ) ; flag = 1 ; } else if ( flag == 0 ) { Console . Write ( " No " ) ; } } }
Sort the Array by reversing the numbers in it | C # implementation of the approach ; Function to return the reverse of n ; Function to sort the array according to the reverse of elements ; Vector to store the reverse with respective elements ; Inserting reverse with elements in the vector pair ; Sort the vector , this will sort the pair according to the reverse of elements ; Print the sorted vector content ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static void sortArr ( int [ ] arr , int n ) { List < Tuple < int , int > > vp = new List < Tuple < int , int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . Add ( new Tuple < int , int > ( reversDigits ( arr [ i ] ) , arr [ i ] ) ) ; } vp . Sort ( ) ; for ( int i = 0 ; i < vp . Count ; i ++ ) Console . Write ( vp [ i ] . Item2 + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 12 , 10 , 102 , 31 , 15 } ; int n = arr . Length ; sortArr ( arr , n ) ; } }
Sort the given Matrix | Memory Efficient Approach | C # implementation to sort the given matrix in strict order ; Function to sort the matrix ; Number of elements in matrix ; Loop to sort the matrix using Bubble Sort ; Condition to check if the Adjacent elements ; Swap if previous value is greater ; Loop to print the matrix ; Driver Code ; Function call to sort ; Function call to print matrix
using System ; class GFG { static void sortMat ( int [ , ] data , int row , int col ) { int size = row * col ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size - 1 ; j ++ ) { if ( data [ j / col , j % col ] > data [ ( j + 1 ) / col , ( j + 1 ) % col ] ) { int temp = data [ j / col , j % col ] ; data [ j / col , j % col ] = data [ ( j + 1 ) / col , ( j + 1 ) % col ] ; data [ ( j + 1 ) / col , ( j + 1 ) % col ] = temp ; } } } } static void printMat ( int [ , ] mat , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 5 , 4 , 7 } , { 1 , 3 , 8 } , { 2 , 9 , 6 } } ; int row = mat . GetLength ( 0 ) ; int col = mat . GetLength ( 1 ) ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ; } }
Partition the array into two odd length groups with minimized absolute difference between their median | C # program to minimise the median between partition array ; Function to find minimise the median between partition array ; Sort the given array [ ] arr ; Return the difference of two middle element of the [ ] arr ; Driver Code ; Size of [ ] arr ; Function that returns the minimum the absolute difference between median of partition array
using System ; class GFG { static int minimiseMedian ( int [ ] arr , int n ) { Array . Sort ( arr ) ; return Math . Abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 15 , 25 , 35 , 50 } ; int n = arr . Length ; Console . WriteLine ( minimiseMedian ( arr , n ) ) ; } }
Making three numbers equal with the given operations | C # implementation of the approach ; Function that returns true if a , b and c can be made equal with the given operations ; Sort the three numbers ; Find the sum of difference of the 3 rd and 2 nd element and the 3 rd and 1 st element ; Subtract the difference from k ; Check the required condition ; Driver code
using System ; class GFG { static bool canBeEqual ( int a , int b , int c , int k ) { int [ ] arr = new int [ 3 ] ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; Array . Sort ( arr ) ; int diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Arrange numbers to form a valid sequence | C # implementation of the approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
using System ; class GFG { static int [ ] orgazineInOrder ( int [ ] vec , int [ ] op , int n ) { int [ ] result = new int [ n ] ; Array . Sort ( vec ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } public static void Main ( ) { int [ ] vec = { 8 , 2 , 7 , 1 , 5 , 9 } ; int [ ] op = { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ; int [ ] result = orgazineInOrder ( vec , op , vec . Length ) ; for ( int i = 0 ; i < result . Length ; i ++ ) { Console . Write ( result [ i ] + " ▁ " ) ; } } }
Minimize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the above approach . ; function to find minimum sum ; variable to store final answer and initialising it with the values when 0 elements is removed from the left and K from the right . ; loop to simulate removal of elements ; removing i elements from the left and and K - i elements from the right and updating the answer correspondingly ; returning final answer ; Driver function ; input values ; calling the required function ;
using System ; class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . Min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n , k ) ) ; } }
Minimum elements to be removed from the ends to make the array sorted | C # implementation of the approach ; Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the readonly answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the readonly answer ; Driver code
using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . Max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }
Number of subsequences of maximum length K containing no repeated elements | C # implementation of the approach ; Returns number of subsequences of maximum length k and contains no repeated element ; Sort the array a [ ] ; Store the frequencies of all the distinct element in the vector arr ; count is the the number of such subsequences ; Create a 2 - d array dp [ n + 1 ] [ m + 1 ] to store the intermediate result ; Initialize the first row to 1 ; Update the dp [ ] [ ] array based on the recurrence relation ; Return the number of subsequences ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countSubSeq ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; List < int > arr = new List < int > ( ) ; int count , x ; for ( int i = 0 ; i < n ; ) { count = 1 ; x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . Add ( count ) ; } int m = arr . Count ; n = Math . Min ( m , k ) ; count = 1 ; int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i , j ] = 0 ; else { dp [ i , j ] = dp [ i , j + 1 ] + arr [ j ] * dp [ i - 1 , j + 1 ] ; } } count = count + dp [ i , 0 ] ; } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 3 , 3 , 5 } ; int n = a . Length ; int k = 3 ; Console . WriteLine ( countSubSeq ( a , n , k ) ) ; } }
Find the count of unvisited indices in an infinite array | C # implementation of the above approach ; Function to return the count of unvisited indices starting from the index 0 ; Largest index that cannot be visited ; Push the index to the queue ; To store the required count ; Current index that cannot be visited ; Increment the count for the current index ; ( curr - m ) and ( curr - n ) are also unreachable if they are valid indices ; Return the required count ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int countUnvisited ( int n , int m ) { int X = ( m * n ) - m - n ; Queue < int > queue = new Queue < int > ( ) ; queue . Enqueue ( X ) ; int count = 0 ; while ( queue . Count != 0 ) { int curr = queue . Dequeue ( ) ; count ++ ; if ( curr - m > 0 ) queue . Enqueue ( curr - m ) ; if ( curr - n > 0 ) queue . Enqueue ( curr - n ) ; } return count ; } public static void Main ( String [ ] args ) { int n = 2 , m = 5 ; Console . WriteLine ( countUnvisited ( n , m ) ) ; } }
Check whether the string S1 can be made equal to S2 with the given operation | C # implementation of the approach ; Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver Code
using System ; class GFG { static string partOdd ( string s ) { string st = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( i % 2 != 0 ) st += s [ i ] ; } return st ; } static string partEven ( string str ) { string s = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( i % 2 == 0 ) s += str [ i ] ; } return s ; } static bool canBeMadeEqual ( string s1 , string s2 ) { char [ ] even1 = partEven ( s1 ) . ToCharArray ( ) ; char [ ] even2 = partEven ( s2 ) . ToCharArray ( ) ; char [ ] odd1 = partOdd ( s1 ) . ToCharArray ( ) ; char [ ] odd2 = partOdd ( s2 ) . ToCharArray ( ) ; Array . Sort ( even1 ) ; Array . Sort ( even2 ) ; Array . Sort ( odd1 ) ; Array . Sort ( odd2 ) ; string even_s1 = new string ( even1 ) ; string even_s2 = new string ( even2 ) ; string odd_s1 = new string ( odd1 ) ; string odd_s2 = new string ( odd2 ) ; if ( even_s1 . Equals ( even_s2 ) && odd_s1 . Equals ( odd_s2 ) ) return true ; return false ; } public static void Main ( ) { string s1 = " cdab " ; string s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Sort the given stack elements based on their modulo with K | C # implementation of the above approach ; Function to sort the stack using another stack based on the values of elements modulo k ; Pop out the first element ; While temporary stack is not empty ; The top of the stack modulo k is greater than ( temp & k ) or if they are equal then compare the values ; Pop from temporary stack and push it to the input stack ; Push temp in temporary of stack ; Push all the elements in the original stack to get the ascending order ; Print the sorted elements ; Driver Code
using System ; using System . Collections ; class GFG { static void sortStack ( Stack input , int k ) { Stack tmpStack = new Stack ( ) ; while ( input . Count != 0 ) { int tmp = ( int ) input . Peek ( ) ; input . Pop ( ) ; while ( tmpStack . Count != 0 ) { int tmpStackMod = ( int ) tmpStack . Peek ( ) % k ; int tmpMod = tmp % k ; if ( ( tmpStackMod > tmpMod ) || ( tmpStackMod == tmpMod && ( int ) tmpStack . Peek ( ) > tmp ) ) { input . Push ( ( int ) tmpStack . Peek ( ) ) ; tmpStack . Pop ( ) ; } else break ; } tmpStack . Push ( tmp ) ; } while ( tmpStack . Count != 0 ) { input . Push ( ( int ) tmpStack . Peek ( ) ) ; tmpStack . Pop ( ) ; } while ( input . Count != 0 ) { Console . Write ( ( int ) input . Peek ( ) + " ▁ " ) ; input . Pop ( ) ; } } public static void Main ( string [ ] args ) { Stack input = new Stack ( ) ; input . Push ( 10 ) ; input . Push ( 3 ) ; input . Push ( 2 ) ; input . Push ( 6 ) ; input . Push ( 12 ) ; int k = 4 ; sortStack ( input , k ) ; } }
Longest sub | C # implementation of the approach ; Function to return the length of the largest subsequence with non - negative sum ; To store the current sum ; Sort the input array in non - increasing order ; Traverse through the array ; Add the current element to the sum ; Condition when c_sum falls below zero ; Complete array has a non - negative sum ; Driver code
using System ; class GFG { static int maxLen ( int [ ] arr , int n ) { int c_sum = 0 ; Array . Sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , - 6 } ; int n = arr . Length ; Console . WriteLine ( maxLen ( arr , n ) ) ; } }
Find Partition Line such that sum of values on left and right is equal | C # implementation of the approach ; Function that returns true if the required line exists ; To handle negative values from x [ ] ; Update arr [ ] such that arr [ i ] contains the sum of all v [ j ] such that x [ j ] = i for all valid values of j ; Update arr [ i ] such that arr [ i ] contains the sum of the subarray arr [ 0. . . i ] from the original array ; If all the points add to 0 then the line can be drawn anywhere ; If the line is drawn touching the leftmost possible points ; If the line is drawn just before the current point ; If the line is drawn touching the current point ; If the line is drawn just after the current point ; If the line is drawn touching the rightmost possible points ; Driver code
using System ; class GFG { static int MAX = 1000 ; static Boolean lineExists ( int [ ] x , int [ ] y , int [ ] v , int n ) { int size = ( 2 * MAX ) + 1 ; long [ ] arr = new long [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( int i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( int i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] x = { - 3 , 5 , 8 } ; int [ ] y = { 8 , 7 , 9 } ; int [ ] v = { 8 , 2 , 10 } ; int n = x . Length ; if ( lineExists ( x , y , v , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximum sum of minimums of pairs in an array | C # implementation of the approach ; Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code
using System ; class GFG { static int maxSum ( int [ ] a , int n ) { Array . Sort ( a ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 1 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }