codeparrot/xlcost-text-to-code huggingface dataset:
Search an element in given N ranges | C # implementation of the approach ; Function to return the index of the range in which K lies and uses linear search ; Iterate and find the element ; If K lies in the current range ; K doesn 't lie in any  of the given ranges ; Driver code
using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair [ ] a , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( K >= a [ i ] . first && K <= a [ i ] . second ) return i ; } return - 1 ; } public static void Main ( String [ ] args ) { pair [ ] a = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . Length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) Console . WriteLine ( index ) ; else Console . WriteLine ( - 1 ) ; } }
Search an element in given N ranges | C # implementation of the above approach ; Function to return the index of the range in which K lies and uses binary search ; Binary search ; Find the mid element ; If element is found ; Check in first half ; Check in second half ; Not found ; Driver code
using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair [ ] a , int n , int K ) { int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( K >= a [ mid ] . first && K <= a [ mid ] . second ) return mid ; else if ( K < a [ mid ] . first ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { pair [ ] a = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . Length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) Console . WriteLine ( index ) ; else Console . WriteLine ( - 1 ) ; } }
Two equal sum segment range queries | C # implementation of the approach ; Function to find the required prefix sum ; Function to q all the values of prefix sum array in an unordered map ; Function to check if a range can be divided into two equal parts ; To store the value of sum of entire range ; If value of sum is odd ; To store p_arr [ l - 1 ] ; If the value exists in the map ; Driver code ; prefix - sum array ; Map to store the values of prefix - sum ; Perform queries
using System ; using System . Collections . Generic ; class GFG { static void prefixSum ( int [ ] p_arr , int [ ] arr , int n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } static void qPrefixSum ( int [ ] p_arr , int n , HashSet < int > q ) { for ( int i = 0 ; i < n ; i ++ ) q . Add ( p_arr [ i ] ) ; } static void canDivide ( int [ ] p_arr , int n , HashSet < int > q , int l , int r ) { int sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } int beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . Contains ( beg + sum / 2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 } ; int n = arr . Length ; int [ ] p_arr = new int [ n ] ; prefixSum ( p_arr , arr , n ) ; HashSet < int > q = new HashSet < int > ( ) ; qPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ; } }
Cost to make a string Panagram | Set 2 | C # implementation of the approach ; Function to return the cost to make str a Panagram ; Count the occurrences of each lowercase character ; To store the total gain ; If some character is missing , it has to be added at twice the cost ; If some character appears more than once all of its occurrences except 1 can be traded for some gain ; If gain is more than the cost ; Return the total cost if gain < 0 ; Driver code
using System ; class GFG { static int costToPanagram ( string str , int [ ] cost ) { int i , n = str . Length ; int [ ] occurrences = new int [ 26 ] ; for ( i = 0 ; i < n ; i ++ ) occurrences [ str [ i ] - ' a ' ] ++ ; int gain = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( occurrences [ i ] == 0 ) gain -= ( 2 * cost [ i ] ) ; else if ( occurrences [ i ] > 1 ) gain += ( cost [ i ] * ( occurrences [ i ] - 1 ) ) ; } if ( gain >= 0 ) return 0 ; return ( gain * - 1 ) ; } public static void Main ( ) { int [ ] cost = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; string str = " geeksforgeeks " ; Console . WriteLine ( costToPanagram ( str , cost ) ) ; } }
Weighted sum of the characters of a string in an array | Set 2 | C # implementation of the approach ; Function to return the required string score ; create a hash map of strings in str ; Store every string in the map along with its position in the array ; If given string is not present in str [ ] ; Multiply sum of alphabets with position ; Driver code
using System ; using System . Collections . Generic ; class GfG { static int strScore ( string [ ] str , string s , int n ) { Dictionary < string , int > m = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] = i + 1 ; if ( ! m . ContainsKey ( s ) ) return 0 ; int score = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) score += s [ i ] - ' a ' + 1 ; score = score * m [ s ] ; return score ; } public static void Main ( ) { string [ ] str = { " geeksforgeeks " , " algorithms " , " stack " } ; string s = " algorithms " ; int n = str . Length ; Console . WriteLine ( strScore ( str , s , n ) ) ; } }
Number of subarrays have bitwise OR >= K | C # implementation of the approach ; Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code
using System ; class GFG { static int countSubArrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = 0 ; for ( int k = i ; k <= j ; k ++ ) { bitwise_or = bitwise_or | arr [ k ] ; } if ( bitwise_or >= K ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }
Number of subarrays have bitwise OR >= K | C # implementation of the approach ; Function to build the segment tree ; Function to return the bitwise OR of segment [ L . . R ] ; Function to return the count of required sub - arrays ; Build segment tree ; Query segment tree for bitwise OR of sub - array [ i . . j ] ; Driver code
using System ; class GFG { static int N = 100002 ; static int [ ] tree = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { build ( arr , 1 , 0 , n - 1 ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = query ( 1 , 0 , n - 1 , i , j ) ; if ( bitwise_or >= K ) count ++ ; } } return count ; } static void Main ( ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }
Number of subarrays have bitwise OR >= K | C # implementation of the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree .
using System ; class GFG { static int N = 100002 ; static int [ ] tree = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int low = i , high = n - 1 , index = int . MaxValue ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = Math . Min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != int . MaxValue ) { count += n - index ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; build ( arr , 1 , 0 , n - 1 ) ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }
Occurrences of a pattern in binary representation of a number | C # program to find the number of times pattern p occurred in binary representation on n . ; Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of pattern_int and all_ones ; If the pattern occurs in the last digits of n ; Right shift n by 1 bit ; Driver code
using System ; class GFG { static int countPattern ( int n , string pat ) { int pattern_int = 0 ; int power_two = 1 ; int all_ones = 0 ; for ( int i = pat . Length - 1 ; i >= 0 ; i -- ) { int current_bit = pat [ i ] - '0' ; pattern_int += ( power_two * current_bit ) ; all_ones = all_ones + power_two ; power_two = power_two * 2 ; } int count = 0 ; while ( n != 0 && n >= pattern_int ) { if ( ( n & all_ones ) == pattern_int ) { count ++ ; } n = n >> 1 ; } return count ; } public static void Main ( ) { int n = 500 ; string pat = "10" ; Console . WriteLine ( countPattern ( n , pat ) ) ; } }
Remove exactly one element from the array such that max | C # implementation of the above approach ; function to calculate max - min ; Driver code
using System ; class GFG { static int max_min ( int [ ] a , int n ) { Array . Sort ( a ) ; return Math . Min ( a [ n - 2 ] - a [ 0 ] , a [ n - 1 ] - a [ 1 ] ) ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 3 , 7 } ; int n = a . Length ; Console . WriteLine ( max_min ( a , n ) ) ; } }
Count numbers < = N whose difference with the count of primes upto them is > = K | C # implementation of the above approach ; primeUpto [ i ] denotes count of prime numbers upto i ; Function to compute all prime numbers and update primeUpto array ; 0 and 1 are not primes ; If i is prime ; Set all multiples of i as non - prime ; Compute primeUpto array ; Function to return the count of valid numbers ; Compute primeUpto array ; Check if the number is valid , try to reduce it ; ans is the minimum valid number ; Driver Code
using System ; public class GFG { static int MAX = 1000001 ; static int [ ] primeUpto = new int [ MAX ] ; static void SieveOfEratosthenes ( ) { int [ ] isPrime = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = 1 ; isPrime [ 0 ] = isPrime [ 1 ] = 0 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( isPrime [ i ] == 1 ) { for ( int j = i * 2 ; j < MAX ; j += i ) isPrime [ j ] = 0 ; } } for ( int i = 1 ; i < MAX ; i ++ ) { primeUpto [ i ] = primeUpto [ i - 1 ] ; if ( isPrime [ i ] == 1 ) primeUpto [ i ] ++ ; } } static int countOfNumbers ( int N , int K ) { SieveOfEratosthenes ( ) ; int low = 1 , high = N , ans = 0 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( mid - primeUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } ans = ans != 0 ? N - ans + 1 : 0 ; return ans ; } public static void Main ( ) { int N = 10 , K = 3 ; Console . WriteLine ( countOfNumbers ( N , K ) ) ; } }
Find the element whose multiplication with | C # program to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
using System ; class GFG { static int minIndex ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , - 5 , 3 , 4 } ; int n = arr . Length ; Console . Write ( minIndex ( arr , n ) ) ; } }
Ceiling of every element in same array | C # implementation of efficient algorithm to find floor of every element ; Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element . ; Find the first element that is greater than the given element ; Since arr [ i ] also exists in array , * ( it - 1 ) will be same as arr [ i ] . Let us check * ( it - 2 ) is also same as arr [ i ] . If true , then arr [ i ] exists twice in array , so ceiling is same same as arr [ i ] ; If next element is also same , then there are multiple occurrences , so print it ; Driver code
using System ; class GFG { static void printPrevGreater ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( " - 1" ) ; return ; } int [ ] v = new int [ arr . GetLength ( 0 ) ] ; Array . Copy ( arr , v , arr . GetLength ( 0 ) ) ; Array . Sort ( v ) ; for ( int i = 0 ; i < n ; i ++ ) { int it = Array . BinarySearch ( v , arr [ i ] ) ; it ++ ; if ( ( it - 1 ) != 0 && v [ it - 2 ] == arr [ i ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; } else if ( it != v . Length ) Console . Write ( v [ it ] + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 11 , 10 , 20 , 12 } ; int n = arr . Length ; printPrevGreater ( arr , n ) ; } }
Find smallest and largest element from square matrix diagonals | C # program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of square matrix ; declare and initialize variables with appropriate value ; take new smallest value ; take new largest value ; Condition for secondary diagonal is mat [ n - 1 - i ] [ i ] take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix
using System ; public class GFG { static void diagonalsMinMax ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; if ( n == 0 ) return ; int principalMin = mat [ 0 , 0 ] , principalMax = mat [ 0 , 0 ] ; int secondaryMin = mat [ n - 1 , 0 ] , secondaryMax = mat [ n - 1 , 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( mat [ i , i ] < principalMin ) { principalMin = mat [ i , i ] ; } if ( mat [ i , i ] > principalMax ) { principalMax = mat [ i , i ] ; } if ( mat [ n - 1 - i , i ] < secondaryMin ) { secondaryMin = mat [ n - 1 - i , i ] ; } if ( mat [ n - 1 - i , i ] > secondaryMax ) { secondaryMax = mat [ n - 1 - i , i ] ; } } Console . WriteLine ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; Console . WriteLine ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } public static void Main ( ) { int [ , ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }
Check if array can be sorted with one swap | A linear C # program to check if array becomes sorted after one swap ; Find counts and positions of elements that are out of order . ; If there are more than two elements are out of order . ; If all elements are sorted already ; Cases like { 1 , 5 , 3 , 4 , 2 } We swap 5 and 2. ; Cases like { 1 , 2 , 4 , 3 , 5 } ; Now check if array becomes sorted for cases like { 4 , 1 , 2 , 3 } ; Driver Code
using System ; class GFG { static bool checkSorted ( int n , int [ ] arr ) { int first = 0 , second = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { count ++ ; if ( first == 0 ) first = i ; else second = i ; } } if ( count > 2 ) return false ; if ( count == 0 ) return true ; if ( count == 2 ) swap ( arr , first - 1 , second ) ; else if ( count == 1 ) swap ( arr , first - 1 , first ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] < arr [ i - 1 ] ) return false ; return true ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 3 , 2 } ; int n = arr . Length ; if ( checkSorted ( n , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the intersection of two Matrices | C # program to find intersection of two matrices ; Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code
using System ; class GFG { static int N = 4 ; static int M = 4 ; static void printIntersection ( int [ , ] A , int [ , ] B ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i , j ] == B [ i , j ] ) Console . Write ( A [ i , j ] + " ▁ " ) ; else Console . Write ( " * ▁ " ) ; } Console . WriteLine ( " ▁ " ) ; } } public static void Main ( ) { int [ , ] A = { { 2 , 4 , 6 , 8 } , { 1 , 3 , 5 , 7 } , { 8 , 6 , 4 , 2 } , { 7 , 5 , 3 , 1 } } ; int [ , ] B = { { 2 , 3 , 6 , 8 } , { 1 , 3 , 5 , 2 } , { 8 , 1 , 4 , 2 } , { 3 , 5 , 4 , 1 } } ; printIntersection ( A , B ) ; } }
Count Triplets such that one of the numbers can be written as sum of the other two | C # program to count Triplets such that at least one of the numbers can be written as sum of the other two ; Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code
using System ; class GFG { static int countWays ( int [ ] arr , int n ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . Max ( max_val , arr [ i ] ) ; int [ ] freq = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( int i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( int i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( int i = 1 ; i <= max_val ; i ++ ) { for ( int j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( countWays ( arr , n ) ) ; } }
Count pairs from two arrays having sum equal to K | C # implementation of above approach . ; Function to return the count of pairs having sum equal to K ; Initialize pairs to 0 ; create map of elements of array A1 ; count total pairs ; Every element can be part of at most one pair . ; return total pairs ; Driver program ; function call to print required answer
using System ; using System . Collections . Generic ; class GfG { static int countPairs ( int [ ] A1 , int [ ] A2 , int n1 , int n2 , int K ) { int res = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { int a ; if ( m . ContainsKey ( A1 [ i ] ) ) { a = m [ A1 [ i ] ] + 1 ; m . Remove ( A1 [ i ] ) ; m . Add ( A1 [ i ] , a ) ; } else m . Add ( A1 [ i ] , 1 ) ; } for ( int i = 0 ; i < n2 ; ++ i ) { int temp = K - A2 [ i ] ; if ( m . ContainsKey ( temp ) && m [ temp ] != 0 ) { res ++ ; m . Remove ( temp ) ; m . Add ( temp , m [ A1 [ i ] ] - 1 ) ; } } return res ; } public static void Main ( ) { int [ ] A1 = { 1 , 1 , 3 , 4 , 5 , 6 , 6 } ; int [ ] A2 = { 1 , 4 , 4 , 5 , 7 } ; int K = 10 ; int n1 = A1 . Length ; int n2 = A2 . Length ; Console . WriteLine ( countPairs ( A1 , A2 , n1 , n2 , K ) ) ; } }
Count pairs in an array such that frequency of one is at least value of other | C # program to illustrate how to create a dictionary ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int countOrderedPairs ( int [ ] A , int n ) { int orderedPairs = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . ContainsKey ( A [ i ] ) ) m . Add ( A [ i ] , 1 ) ; else { m [ A [ i ] ] ++ ; } } foreach ( KeyValuePair < int , int > entry in m ) { int X = entry . Key ; int Y = entry . Value ; for ( int j = 1 ; j <= Y ; j ++ ) { if ( m [ j ] >= X ) orderedPairs ++ ; } } return orderedPairs ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 } ; int n = A . Length ; Console . Write ( countOrderedPairs ( A , n ) ) ; } }
Longest subarray with elements having equal modulo K | C # implementation of above approach ; function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code
using System ; class GFG { static int LongestSubarray ( int [ ] arr , int n , int k ) { int count = 1 ; int max_length = 1 ; int prev_mod = arr [ 0 ] % k ; for ( int i = 1 ; i < n ; i ++ ) { int curr_mod = arr [ i ] % k ; if ( curr_mod == prev_mod ) { count ++ ; } else { max_length = Math . Max ( max_length , count ) ; count = 1 ; prev_mod = curr_mod ; } } return Math . Max ( max_length , count ) ; } public static void Main ( ) { int [ ] arr = { 4 , 9 , 7 , 18 , 29 , 11 } ; int n = arr . Length ; int k = 11 ; Console . Write ( LongestSubarray ( arr , n , k ) ) ; } }