codeparrot/xlcost-text-to-code huggingface dataset:
Maximum of minimum difference of all pairs from subsequences of given size | C # program to implement the above approach ; Function to check a subsequence can be formed with min difference mid ; If a subsequence of size B with min diff = mid is possible return true else false ; Function to find the maximum of all minimum difference of pairs possible among the subsequence ; Sort the Array ; Stores the boundaries of the search space ; Store the answer ; Binary Search ; If subsequence can be formed with min diff mid and size B ; Right half ; Left half ; Driver Code
using System ; class GFG { static bool can_place ( int [ ] A , int n , int B , int mid ) { int count = 1 ; int last_position = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] - last_position >= mid ) { last_position = A [ i ] ; count ++ ; if ( count == B ) { return true ; } } } return false ; } static int find_min_difference ( int [ ] A , int n , int B ) { Array . Sort ( A ) ; int s = 0 ; int e = A [ n - 1 ] - A [ 0 ] ; int ans = 0 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( can_place ( A , n , B , mid ) ) { ans = mid ; s = mid + 1 ; } else { e = mid - 1 ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 2 , 3 , 5 } ; int n = A . Length ; int B = 3 ; int min_difference = find_min_difference ( A , n , B ) ; Console . Write ( min_difference ) ; } }
Count of triplets in an Array such that A [ i ] * A [ j ] = A [ k ] and i < j < k | C # program to implement the above approach ; Returns total number of valid triplets possible ; Stores the count ; Map to store frequency of array elements ; Increment the frequency of A [ j + 1 ] as it can be a valid A [ k ] ; If target exists in the map ; Return the readonly count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int j = N - 2 ; j >= 1 ; j -- ) { if ( map . ContainsKey ( A [ j + 1 ] ) ) map [ A [ j + 1 ] ] = map [ A [ j + 1 ] ] + 1 ; else map . Add ( A [ j + 1 ] , 1 ) ; for ( int i = 0 ; i < j ; i ++ ) { int target = A [ i ] * A [ j ] ; if ( map . ContainsKey ( target ) ) ans += map [ target ] ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 2 , 3 , 4 , 6 , 12 } ; Console . Write ( countTriplets ( A , N ) ) ; } }
Missing vertex among N axis | C # Program to implement the above approach ; Driver Code ; Number of rectangles ; Stores the coordinates ; Insert the coordinates
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void MissingPoint ( List < pair > V , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < V . Count ; i ++ ) { if ( mp . ContainsKey ( V [ i ] . first ) ) mp [ V [ i ] . first ] = mp [ V [ i ] . first ] + 1 ; else mp . Add ( V [ i ] . first , 1 ) ; } int x = 0 , y = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value % 2 == 1 ) { x = it . Key ; break ; } } mp . Clear ( ) ; for ( int i = 0 ; i < V . Count ; i ++ ) { if ( mp . ContainsKey ( V [ i ] . second ) ) mp [ V [ i ] . second ] = mp [ V [ i ] . second ] + 1 ; else mp . Add ( V [ i ] . second , 1 ) ; } foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value % 2 == 1 ) { y = it . Key ; break ; } } Console . Write ( x + " ▁ " + y ) ; } public static void Main ( String [ ] args ) { int N = 2 ; List < pair > V = new List < pair > ( ) ; V . Add ( new pair ( 1 , 1 ) ) ; V . Add ( new pair ( 1 , 2 ) ) ; V . Add ( new pair ( 4 , 6 ) ) ; V . Add ( new pair ( 2 , 1 ) ) ; V . Add ( new pair ( 9 , 6 ) ) ; V . Add ( new pair ( 9 , 3 ) ) ; V . Add ( new pair ( 4 , 3 ) ) ; MissingPoint ( V , N ) ; } }
Longest alternating subsequence with maximum sum | Set 2 | C # Program to implement the above approach ; Function to check the sign of the element ; Function to calculate and return the maximum sum of longest alternating subsequence ; Iterate through the array ; Stores the first element of a sequence of same sign ; Traverse until an element with opposite sign is encountered ; Update the maximum ; Update the maximum sum ; Update i ; Return the maximum sum ; Driver Code
using System ; class GFG { static int sign ( int x ) { if ( x > 0 ) return 1 ; else return - 1 ; } static int findMaxSum ( int [ ] arr , int size ) { int max_sum = 0 , pres , i , j ; for ( i = 0 ; i < size ; i ++ ) { pres = arr [ i ] ; j = i ; while ( j < size && sign ( arr [ i ] ) == sign ( arr [ j ] ) ) { pres = Math . Max ( pres , arr [ j ] ) ; j ++ ; } max_sum = max_sum + pres ; i = j - 1 ; } return max_sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 } ; int size = arr . Length ; Console . WriteLine ( findMaxSum ( arr , size ) ) ; } }
Check if an array can be split into subsets of K consecutive elements | C # program to implement the above approach ; Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool groupInKConsecutive ( int [ ] arr , int K ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; foreach ( int h in arr ) { if ( count . ContainsKey ( h ) ) count [ h ] ++ ; else count [ h ] = 1 ; } foreach ( int c in count . Keys . ToList ( ) ) { int cur = c ; int n = count ; if ( n > 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( ! count . ContainsKey ( cur + i ) ) { return false ; } count [ cur + i ] -= n ; if ( count [ cur + i ] < 0 ) return false ; } } } return true ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 } ; int k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }
Count of substrings of a given Binary string with all characters same | C # program for the above approach ; Function to count number of sub - Strings of a given binary String that contains only 1 ; Iterate untill L and R cross each other ; Check if reached the end of String ; Check if encountered '1' then extend window ; Check if encountered '0' then add number of Strings of current window and change the values for both l and r ; Return the answer ; Function to flip the bits of String ; Function to count number of sub - Strings of a given binary String that contains only 0 s & 1 s ; count of subString which contains only 1 s ; Flip the character of String s 0 to 1 and 1 to 0 to count the subString with consecutive 0 s ; count of subString which contains only 0 s ; Driver Code ; Given String str ; Function call
using System ; class GFG { static int countSubAllOnes ( String s ) { int l = 0 , r = 0 , ans = 0 ; while ( l <= r ) { if ( r == s . Length ) { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; break ; } if ( s [ r ] == '1' ) r ++ ; else { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; l = r + 1 ; r ++ ; } } return ans ; } static String flip ( char [ ] s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) s [ i ] = '0' ; else s [ i ] = '1' ; } return String . Join ( " " , s ) ; } static int countSubAllZerosOnes ( String s ) { int only_1s = countSubAllOnes ( s ) ; s = flip ( s . ToCharArray ( ) ) ; int only_0s = countSubAllOnes ( s ) ; return only_0s + only_1s ; } public static void Main ( String [ ] args ) { String s = "011" ; Console . Write ( countSubAllZerosOnes ( s ) + " STRNEWLINE " ) ; } }
Count of primes in a given range that can be expressed as sum of perfect squares | C # program to implement the above approach ; Function to check if a prime number satisfies the condition to be expressed as sum of two perfect squares ; Function to check if a number is prime or not ; Corner cases ; Function to return the count of primes in the range which can be expressed as the sum of two squares ; If i is a prime ; If i can be expressed as the sum of two squares ; Return the count ; Driver code
using System ; class GFG { static bool sumSquare ( int p ) { return ( p - 1 ) % 4 == 0 ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countOfPrimes ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { if ( sumSquare ( i ) ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int L = 5 , R = 41 ; Console . WriteLine ( countOfPrimes ( L , R ) ) ; } }
Count of array elements which are greater than all elements on its left | C # program to implement the above approach ; Function to return the count of array elements with all elements to its left smaller than it ; Stores the count ; Stores the maximum ; Iterate over the array ; If an element greater than maximum is obtained ; Increase count ; Update maximum ; Driver Code
using System ; class GFG { static int count_elements ( int [ ] arr , int n ) { int count = 1 ; int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { count += 1 ; max = arr [ i ] ; } } return count ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 6 , 3 } ; int n = arr . Length ; Console . Write ( count_elements ( arr , n ) ) ; } }
Count of bitonic substrings from the given string | C # program for the above approach ; Function to find all the bitonic sub Strings ; Pick starting point ; Iterate till length of the String ; Pick ending point for String ; SubString from i to j is obtained ; SubStrings of length 1 ; Increase count ; For increasing sequence ; Check for strictly increasing ; Increase count ; Check for decreasing sequence ; Increase count ; Print the result ; Driver Code ; Given String ; Function call
using System ; class GFG { static void subString ( char [ ] str , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = ( char ) 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( k < n && temp < str [ k ] ) { temp = str [ k ] ; k ++ ; f = ( char ) 2 ; } if ( k > j ) { c ++ ; f = ( char ) 2 ; } while ( k < n && temp > str [ k ] && f != 2 ) { k ++ ; f = ( char ) 0 ; } if ( k > j && f != 2 ) { c ++ ; f = ( char ) 0 ; } } } Console . Write ( c + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { char [ ] str = " bade " . ToCharArray ( ) ; subString ( str , str . Length ) ; } }
Check if ceil of number divided by power of two exist in sorted array | C # implementation to check if a number divided by power of two exist in the sorted array ; Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
using System ; class GFG { static int findNumberDivByPowerofTwo ( int [ ] ar , int k , int n ) { int found = - 1 , m = k ; while ( m > 0 ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != - 1 ) { break ; } m = m / 2 ; } return found ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 7 , 8 , 10 } ; int k = 4 , n = 5 ; Console . WriteLine ( findNumberDivByPowerofTwo ( arr , k , n ) ) ; } }
Largest subset having with sum less than equal to sum of respective indices | C # program to implement the above approach ; Function to find the length of the longest subset ; Stores the sum of differences between elements and their respective index ; Stores the size of the subset ; Iterate over the array ; If an element which is smaller than or equal to its index is encountered ; Increase count and sum ; Store the difference with index of the remaining elements ; Sort the differences in increasing order ; Include the differences while sum remains positive or ; Return the size ; Driver code ; Function calling
using System ; using System . Collections . Generic ; class GFG { public static int findSubset ( int [ ] a , int n ) { int sum = 0 ; int cnt = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( a [ i - 1 ] - i <= 0 ) { sum += a [ i - 1 ] - i ; cnt += 1 ; } else { v . Add ( a [ i - 1 ] - i ) ; } } v . Sort ( ) ; int ptr = 0 ; while ( ptr < v . Count && sum + v [ ptr ] <= 0 ) { cnt += 1 ; ptr += 1 ; sum += v [ ptr ] ; } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 6 , 7 , 8 , 2 } ; int n = arr . Length ; Console . WriteLine ( findSubset ( arr , n ) ) ; } }
Queries for count of array elements with values in given range with updates | C # code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void setElement ( int [ ] arr , int n , int index , int x ) { arr [ index ] = x ; } static int getCount ( int [ ] arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) count += 1 ; } return count ; } static void SolveQuery ( int [ ] arr , int n , List < List < int > > Q ) { int x ; for ( int i = 0 ; i < Q . Count ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { x = getCount ( arr , n , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; Console . Write ( x + " ▁ " ) ; } else { setElement ( arr , n , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = arr . Length ; List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 3 , 5 } ) ; myList . Add ( new List < int > { 1 , 2 , 4 } ) ; myList . Add ( new List < int > { 1 , 1 , 2 } ) ; myList . Add ( new List < int > { 2 , 1 , 7 } ) ; myList . Add ( new List < int > { 1 , 1 , 2 } ) ; SolveQuery ( arr , n , myList ) ; } }
Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | C # program for the above approach ; Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Driver Code ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function call
using System ; using System . Collections . Generic ; class GFG { static List < int > generateSequence ( int [ ] freq , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { m . Add ( i , freq [ i ] ) ; total += freq [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( i ) ) { List < int > sequence = new List < int > ( ) ; Dictionary < int , int > mcopy = new Dictionary < int , int > ( m ) ; if ( mcopy . ContainsKey ( i ) && mcopy [ i ] > 0 ) mcopy [ i ] = mcopy [ i ] - 1 ; sequence . Add ( i ) ; int last = i ; for ( int i1 = 0 ; i1 < total - 1 ; i1 ++ ) { if ( mcopy . ContainsKey ( last - 1 ) && mcopy [ last - 1 ] > 0 ) { mcopy [ last - 1 ] = mcopy [ last - 1 ] - 1 ; sequence . Add ( last - 1 ) ; last -- ; } else if ( mcopy . ContainsKey ( last + 1 ) ) { mcopy [ last + 1 ] = mcopy [ last + 1 ] - 1 ; sequence . Add ( last + 1 ) ; last ++ ; } else break ; } if ( sequence . Count == total ) { return sequence ; } } } List < int > empty = new List < int > ( ) ; return empty ; } static void PrintSequence ( int [ ] freq , int n ) { List < int > sequence = generateSequence ( freq , n ) ; if ( sequence . Count == 0 ) { Console . Write ( " - 1" ) ; } else { for ( int i = 0 ; i < sequence . Count ; i ++ ) { Console . Write ( sequence [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] freq = { 2 , 2 , 2 , 3 , 1 } ; int N = 5 ; PrintSequence ( freq , N ) ; } }
Minimum distance between any most frequent and least frequent element of an array | C # implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call
using System ; using System . Collections . Generic ; public class GFG { public static void getMinimumDistance ( int [ ] a , int n ) { HashSet < int > min_set = new HashSet < int > ( ) ; HashSet < int > max_set = new HashSet < int > ( ) ; int max = 0 , min = Int32 . MaxValue ; Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! frequency . ContainsKey ( a [ i ] ) ) frequency . Add ( a [ i ] , 0 ) ; frequency [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { int count = frequency [ a [ i ] ] ; if ( count == max ) { max_set . Add ( a [ i ] ) ; } else if ( count > max ) { max_set . Clear ( ) ; max = count ; max_set . Add ( a [ i ] ) ; } if ( count == min ) { min_set . Add ( a [ i ] ) ; } else if ( count < min ) { min_set . Clear ( ) ; min = count ; min_set . Add ( a [ i ] ) ; } } int min_dist = Int32 . MaxValue ; int last_min_found = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_set . Contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . Contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . Min ( min_dist , i - last_min_found ) ; } } last_min_found = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . Contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . Contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . Min ( min_dist , last_min_found - i ) ; } } Console . WriteLine ( min_dist ) ; } static public void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 , 2 , 3 , 3 } ; int N = arr . Length ; getMinimumDistance ( arr , N ) ; } }
Construct a string that has exactly K subsequences from given string | C # program for the above approach ; Function that computes the String s ; Length of the given String str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the String ; Driver code ; Given String ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printSubsequenceString ( String str , int k ) { int n = str . Length ; int i ; List < int > factors = new List < int > ( ) ; for ( i = 2 ; i <= Math . Sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . Add ( i ) ; k /= i ; } } if ( k > 1 ) factors . Add ( k ) ; int [ ] count = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; int index = 0 ; while ( factors . Count > 0 ) { count [ index ++ ] *= factors [ factors . Count - 1 ] ; factors . Remove ( factors [ factors . Count - 1 ] ) ; if ( index == n ) index = 0 ; } String s = " " ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str [ i ] ; } } Console . Write ( s ) ; } public static void Main ( String [ ] args ) { String str = " code " ; int k = 20 ; printSubsequenceString ( str , k ) ; } }
Replace each element of Array with it 's corresponding rank | C # program for the above approach ; Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; To store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver Code ; Given array arr [ ] ; Function call ; Print the array elements
using System ; using System . Collections . Generic ; using System . Text ; class GFG { static void changeArr ( int [ ] input ) { int [ ] newArray = new int [ input . Length ] ; Array . Copy ( input , newArray , input . Length ) ; Array . Sort ( newArray ) ; Dictionary < int , int > ranks = new Dictionary < int , int > ( ) ; int rank = 1 ; for ( int index = 0 ; index < newArray . Length ; index ++ ) { int element = newArray [ index ] ; if ( ! ranks . ContainsKey ( element ) ) { ranks [ element ] = rank ; rank ++ ; } } for ( int index = 0 ; index < input . Length ; index ++ ) { input [ index ] = ranks [ input [ index ] ] ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 100 , 2 , 70 , 2 } ; changeArr ( arr ) ; Console . WriteLine ( " [ { 0 } ] " , string . Join ( " , ▁ " , arr ) ) ; } }
Find pairs in array whose sum does not exist in Array | C # program to implement the above approach ; Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void findPair ( int [ ] arr , int n ) { int i , j ; if ( n < 2 ) { Console . Write ( " - 1" ) ; } HashSet < int > hashMap = new HashSet < int > ( ) ; foreach ( int k in arr ) { hashMap . Add ( k ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . Contains ( sum ) ) { Console . Write ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) STRNEWLINE " ) ; } } } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 2 , 6 } ; int n = arr . Length ; findPair ( arr , n ) ; } }