codeparrot/xlcost-text-to-code huggingface dataset:
Online algorithm for checking palindrome in a stream | C # program for online algorithm for palindrome checking ; d is the number of characters in input alphabet ; q is a prime number used for evaluating Rabin Karp 's Rolling hash  ; Length of input string ; A single character is always a palindrome ; Return if string has only one character ; Initialize first half reverse and second half for as firstr and second characters ; Now check for palindromes from second character onward ; If the hash values of ' firstr ' and ' second ' match , then only check individual characters ; Check if str [ 0. . i ] is palindrome using simple character by character match ; Calculate hash values for next iteration . Don 't calculate hash for next characters   if this is the last character of string  ; If i is even ( next i is odd ) ; Add next character after first half at beginning of ' firstr ' ; Add next character after second half at the end of second half . ; If next i is odd ( next i is even ) then we need not to change firstr , we need to remove first character of second and append a character to it . ; Driver Code
using System ; class GFG { public const int d = 256 ; public const int q = 103 ; public static void checkPalindromes ( string str ) { int N = str . Length ; Console . WriteLine ( str [ 0 ] + " ▁ Yes " ) ; if ( N == 1 ) { return ; } int firstr = str [ 0 ] % q ; int second = str [ 1 ] % q ; int h = 1 , i , j ; for ( i = 1 ; i < N ; i ++ ) { if ( firstr == second ) { for ( j = 0 ; j < i / 2 ; j ++ ) { if ( str [ j ] != str [ i - j ] ) { break ; } } Console . WriteLine ( ( j == i / 2 ) ? str [ i ] + " ▁ Yes " : str [ i ] + " ▁ No " ) ; } else { Console . WriteLine ( str [ i ] + " ▁ No " ) ; } if ( i != N - 1 ) { if ( i % 2 == 0 ) { h = ( h * d ) % q ; firstr = ( firstr + h * str [ i / 2 ] ) % q ; second = ( second * d + str [ i + 1 ] ) % q ; } else { second = ( d * ( second + q - str [ ( i + 1 ) / 2 ] * h ) % q + str [ i + 1 ] ) % q ; } } } } public static void Main ( string [ ] args ) { string txt = " aabaacaabaa " ; checkPalindromes ( txt ) ; } }
Find zeroes to be flipped so that number of consecutive 1 's is maximized | C # to find positions of zeroes flipping which produces maximum number of consecutive 1 's ; m is maximum of number zeroes allowed to flip ; Left and right indexes of current window ; Left index and size of the widest window ; Count of zeroes in current window ; While right boundary of current window doesn 't cross right end ; If zero count of current window is less than m , widen the window toward right ; If zero count of current window is more than m , reduce the window from left ; Update widest window if this window size is more ; Print positions of zeroes in the widest window ; Driver method to test the above function
using System ; class Test { static int [ ] arr = new int [ ] { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; static void findZeroes ( int m ) { int wL = 0 , wR = 0 ; int bestL = 0 , bestWindow = 0 ; int zeroCount = 0 ; while ( wR < arr . Length ) { if ( zeroCount <= m ) { if ( arr [ wR ] == 0 ) zeroCount ++ ; wR ++ ; } if ( zeroCount > m ) { if ( arr [ wL ] == 0 ) zeroCount -- ; wL ++ ; } if ( ( wR - wL > bestWindow ) && ( zeroCount <= m ) ) { bestWindow = wR - wL ; bestL = wL ; } } for ( int i = 0 ; i < bestWindow ; i ++ ) { if ( arr [ bestL + i ] == 0 ) Console . Write ( bestL + i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int m = 2 ; Console . Write ( " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ) ; findZeroes ( m ) ; } }
Count Strictly Increasing Subarrays | C # program to count number of strictly increasing subarrays ; Initialize count of subarrays as 0 ; Pick starting point ; Pick ending point ; If subarray arr [ i . . j ] is not strictly increasing , then subarrays after it , i . e . , arr [ i . . j + 1 ] , arr [ i . . j + 2 ] , ... . cannot be strictly increasing ; Driver Code
using System ; class Test { static int [ ] arr = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) cnt ++ ; else break ; } } return cnt ; } public static void Main ( String [ ] args ) { Console . Write ( " Count ▁ of ▁ strictly ▁ increasing " + " subarrays ▁ is ▁ " + countIncreasing ( arr . Length ) ) ; } }
Count Strictly Increasing Subarrays | C # program to count number of strictly increasing subarrays ; Initialize result ; Initialize length of current increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is greater than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver method to test the above function
using System ; class GFG { static int [ ] arr = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] > arr [ i ] ) len ++ ; else { cnt += ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } public static void Main ( ) { Console . WriteLine ( " Count ▁ of ▁ strictly ▁ " + " increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . Length ) ) ; } }
Maximum difference between group of k | C # to find maximum group difference ; utility function for array sum ; function for finding maximum group difference of array ; sort the array ; find array sum ; difference for k - smallest diff1 = ( arraysum - k_smallest ) - k_smallest ; reverse array for finding sum of 1 st k - largest ; difference for k - largest diff2 = ( arraysum - k_largest ) - k_largest ; return maximum difference value ; Driver program
using System ; public class GFG { static long arraySum ( int [ ] arr , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum ; } static long maxDiff ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; long arraysum = arraySum ( arr , n ) ; long diff1 = Math . Abs ( arraysum - 2 * arraySum ( arr , k ) ) ; Array . Reverse ( arr ) ; long diff2 = Math . Abs ( arraysum - 2 * arraySum ( arr , k ) ) ; return ( Math . Max ( diff1 , diff2 ) ) ; } static public void Main ( ) { int [ ] arr = { 1 , 7 , 4 , 8 , - 1 , 5 , 2 , 1 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n , k ) ) ; } }
Minimum number of elements to add to make median equals x | C # program to find minimum number of elements needs to add to the array so that its median equals x . ; Returns count of elements to be added to make median x . This function assumes that a [ ] has enough extra space . ; to sort the array in increasing order . ; Driver code
using System ; class GFG { static int minNumber ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int k ; for ( k = 0 ; a [ ( n ) / 2 ] != x ; k ++ ) { a [ n ++ ] = x ; Array . Sort ( a ) ; } return k ; } public static void Main ( String [ ] args ) { int x = 10 ; int [ ] a = { 10 , 20 , 30 } ; int n = 3 ; Console . WriteLine ( minNumber ( a , n - 1 , x ) ) ; } }
Minimum number of elements to add to make median equals x | C # program to find minimum number of elements to add so that its median equals x . ; no . of elements equals to x , that is , e . ; no . of elements greater than x , that is , h . ; no . of elements smaller than x , that is , l . ; subtract the no . of elements that are equal to x . ; Driver Code
using System ; class GFG { public static int minNumber ( int [ ] a , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void Main ( ) { int x = 10 ; int [ ] a = { 10 , 20 , 30 } ; int n = a . Length ; Console . WriteLine ( minNumber ( a , n , x ) ) ; } }
Find whether a subarray is in form of a mountain or not | C # program to check whether a subarray is in mountain form or not ; Utility method to construct left and right array ; initialize first left index as that index only ; if current value is greater than previous , update last increasing ; initialize last right index as that index only ; if current value is greater than next , update first decreasing ; method returns true if arr [ L . . R ] is in mountain form ; return true only if right at starting range is greater than left at ending range ; Driver Code
using System ; class GFG { static void preprocess ( int [ ] arr , int N , int [ ] left , int [ ] right ) { left [ 0 ] = 0 ; int lastIncr = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) lastIncr = i ; left [ i ] = lastIncr ; } right [ N - 1 ] = N - 1 ; int firstDecr = N - 1 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) firstDecr = i ; right [ i ] = firstDecr ; } } static bool isSubarrayMountainForm ( int [ ] arr , int [ ] left , int [ ] right , int L , int R ) { return ( right [ L ] >= left [ R ] ) ; } static public void Main ( ) { int [ ] arr = { 2 , 3 , 2 , 4 , 4 , 6 , 3 , 2 } ; int N = arr . Length ; int [ ] left = new int [ N ] ; int [ ] right = new int [ N ] ; preprocess ( arr , N , left , right ) ; int L = 0 ; int R = 2 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) Console . WriteLine ( " Subarray ▁ is ▁ in ▁ " + " mountain ▁ form " ) ; else Console . WriteLine ( " Subarray ▁ is ▁ not ▁ " + " in ▁ mountain ▁ form " ) ; L = 1 ; R = 3 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) Console . WriteLine ( " Subarray ▁ is ▁ in ▁ " + " mountain ▁ form " ) ; else Console . WriteLine ( " Subarray ▁ is ▁ not ▁ " + " in ▁ mountain ▁ form " ) ; } }
Range sum queries without updates | Program to find sum between two indexes when there is no update . ; Returns sum of elements in arr [ i . . j ] It is assumed that i <= j ; Driver code ; Function call ;
using System ; class GFG { public static void preCompute ( int [ ] arr , int n , int [ ] pre ) { pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } public static int rangeSum ( int i , int j , int [ ] pre ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int [ ] pre = new int [ n ] ; preCompute ( arr , n , pre ) ; Console . WriteLine ( rangeSum ( 1 , 3 , pre ) ) ; Console . WriteLine ( rangeSum ( 2 , 4 , pre ) ) ; } } \
Check in binary array the number represented by a subarray is odd or even | C # program to find if a subarray is even or odd . ; prints if subarray is even or odd ; if arr [ r ] = 1 print odd ; if arr [ r ] = 0 print even ; driver code
using System ; class GFG { static void checkEVENodd ( int [ ] arr , int n , int l , int r ) { if ( arr [ r ] == 1 ) Console . WriteLine ( " odd " ) ; else Console . WriteLine ( " even " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 0 , 1 } ; int n = arr . Length ; checkEVENodd ( arr , n , 1 , 3 ) ; } }
Mean of range in array | C # program to find floor value of mean in range l to r ; To find mean of range in l to r ; Both sum and count are initialize to 0 ; To calculate sum and number of elements in range l to r ; Calculate floor value of mean ; Returns mean of array in range l to r ; Driver program to test findMean ( )
using System ; public class GFG { static int findMean ( int [ ] arr , int l , int r ) { int sum = 0 , count = 0 ; for ( int i = l ; i <= r ; i ++ ) { sum += arr [ i ] ; count ++ ; } int mean = ( int ) Math . Floor ( ( double ) sum / count ) ; return mean ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; Console . WriteLine ( findMean ( arr , 0 , 2 ) ) ; Console . WriteLine ( findMean ( arr , 1 , 3 ) ) ; Console . WriteLine ( findMean ( arr , 0 , 4 ) ) ; } }
Mean of range in array | C # program to find floor value of mean in range l to r ; To calculate prefixSum of array ; Calculate prefix sum of array ; To return floor of mean in range l to r ; Sum of elements in range l to r is prefixSum [ r ] - prefixSum [ l - 1 ] Number of elements in range l to r is r - l + 1 ; Driver program to test above functions
using System ; public class GFG { public static readonly int MAX = 1000005 ; static int [ ] prefixSum = new int [ MAX ] ; static void calculatePrefixSum ( int [ ] arr , int n ) { prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } static int findMean ( int l , int r ) { if ( l == 0 ) return ( int ) Math . Floor ( ( double ) ( prefixSum [ r ] / ( r + 1 ) ) ) ; return ( int ) Math . Floor ( ( double ) ( prefixSum [ r ] - prefixSum [ l - 1 ] ) / ( r - l + 1 ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; calculatePrefixSum ( arr , n ) ; Console . WriteLine ( findMean ( 1 , 2 ) ) ; Console . WriteLine ( findMean ( 1 , 3 ) ) ; Console . WriteLine ( findMean ( 1 , 4 ) ) ; } }
Print modified array after executing the commands of addition and subtraction | Program to find modified array after executing m commands / queries ; Update function for every command ; If q == 0 , add ' k ' and ' - k ' to ' l - 1' and ' r ' index ; If q == 1 , add ' - k ' and ' k ' to ' l - 1' and ' r ' index ; Function to generate final array after executing all commands ; Generate final array with the help of DP concept ; Driver code ; Generate final array ; Printing the final modified array
using System ; class GFG { static void updateQuery ( int [ ] arr , int n , int q , int l , int r , int k ) { if ( q == 0 ) { arr [ l - 1 ] += k ; arr [ r ] += - k ; } else { arr [ l - 1 ] += - k ; arr [ r ] += k ; } return ; } static void generateArray ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; ++ i ) arr [ i ] += arr [ i - 1 ] ; } public static void Main ( ) { int n = 5 ; int [ ] arr = new int [ n + 1 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) arr [ i ] = 0 ; int q = 0 , l = 1 , r = 3 , k = 2 ; updateQuery ( arr , n , q , l , r , k ) ; q = 1 ; l = 3 ; r = 5 ; k = 3 ; updateQuery ( arr , n , q , l , r , k ) ; q = 0 ; l = 2 ; r = 5 ; k = 1 ; updateQuery ( arr , n , q , l , r , k ) ; generateArray ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Products of ranges in an array | Product in range Queries in O ( N ) ; Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code
using System ; class GFG { static int calculateProduct ( int [ ] A , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans = 1 ; for ( int i = L ; i <= R ; i ++ ) { ans = ans * A [ i ] ; ans = ans % P ; } return ans ; } static public void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 229 ; int L = 2 , R = 5 ; Console . WriteLine ( calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; Console . WriteLine ( calculateProduct ( A , L , R , P ) ) ; } }
Products of ranges in an array | C # program to find Product in range Queries in O ( 1 ) ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; calculating pre_product array ; Cacluating inverse_product array . ; Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code ; Array ; Prime P ; Calculating PreProduct and InverseProduct ; Range [ L , R ] in 1 base index
using System ; class GFG { static int MAX = 100 ; int [ ] pre_product = new int [ MAX ] ; int [ ] inverse_product = new int [ MAX ] ; int modInverse ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } void calculate_Pre_Product ( int [ ] A , int N , int P ) { pre_product [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pre_product [ i ] = pre_product [ i - 1 ] * A [ i ] ; pre_product [ i ] = pre_product [ i ] % P ; } } void calculate_inverse_product ( int [ ] A , int N , int P ) { inverse_product [ 0 ] = modInverse ( pre_product [ 0 ] , P ) ; for ( int i = 1 ; i < N ; i ++ ) inverse_product [ i ] = modInverse ( pre_product [ i ] , P ) ; } int calculateProduct ( int [ ] A , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans ; if ( L == 0 ) ans = pre_product [ R ] ; else ans = pre_product [ R ] * inverse_product [ L - 1 ] ; return ans ; } public static void Main ( ) { GFG d = new GFG ( ) ; int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 113 ; d . calculate_Pre_Product ( A , A . Length , P ) ; d . calculate_inverse_product ( A , A . Length , P ) ; int L = 2 , R = 5 ; Console . WriteLine ( d . calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; Console . WriteLine ( d . calculateProduct ( A , L , R , P ) ) ; } }
Count Primes in Ranges | C # program to answer queries for count of primes in given range . ; prefix [ i ] is going to store count of primes till i ( including i ) . ; Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Build prefix array ; Returns count of primes in range from L to R ( both inclusive ) . ; Driver code
using System ; class GFG { static int MAX = 10000 ; static int [ ] prefix = new int [ MAX + 1 ] ; static void buildPrefix ( ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] == false ) prefix [ p ] ++ ; } } static int query ( int L , int R ) { return prefix [ R ] - prefix [ L - 1 ] ; } public static void Main ( ) { buildPrefix ( ) ; int L = 5 , R = 10 ; Console . WriteLine ( query ( L , R ) ) ; L = 1 ; R = 10 ; Console . WriteLine ( query ( L , R ) ) ; } }
Binary array after M range toggle operations | C # program to find modified array after m range toggle operations . ; function for toggle ; function for final processing of array ; function for printing result ; Driver Code ; function call for toggle ; process array ; print result
using System ; class GFG { static void command ( bool [ ] arr , int a , int b ) { arr [ a ] ^= true ; arr [ b + 1 ] ^= true ; } static void process ( bool [ ] arr , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { arr [ k ] ^= arr [ k - 1 ] ; } } static void result ( bool [ ] arr , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { if ( arr [ k ] == true ) Console . Write ( "1" + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; } } public static void Main ( ) { int n = 5 , m = 3 ; bool [ ] arr = new bool [ n + 2 ] ; command ( arr , 1 , 5 ) ; command ( arr , 2 , 5 ) ; command ( arr , 3 , 5 ) ; process ( arr , n ) ; result ( arr , n ) ; } }
Check if any two intervals intersects among a given set of intervals | C # program to check if any two intervals overlap ; An interval has start time and end time ; Function to check if any two intervals overlap ; Find the overall maximum element ; Initialize an array of size max_ele ; starting point of the interval ; end point of the interval ; Calculating the prefix Sum ; Overlap ; If we reach here , then no Overlap ; Driver Code
using System ; class GFG { class Interval { public int start ; public int end ; public Interval ( int start , int end ) { this . start = start ; this . end = end ; } } ; static bool isIntersect ( Interval [ ] arr , int n ) { int max_ele = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_ele < arr [ i ] . end ) max_ele = arr [ i ] . end ; } int [ ] aux = new int [ max_ele + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] . start ; int y = arr [ i ] . end ; aux [ x ] ++ ; aux [ y ] -- ; } for ( int i = 1 ; i <= max_ele ; i ++ ) { aux [ i ] += aux [ i - 1 ] ; if ( aux [ i ] > 1 ) return true ; } return false ; } public static void Main ( String [ ] args ) { Interval [ ] arr1 = { new Interval ( 1 , 3 ) , new Interval ( 7 , 9 ) , new Interval ( 4 , 6 ) , new Interval ( 10 , 13 ) } ; int n1 = arr1 . Length ; if ( isIntersect ( arr1 , n1 ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; Interval [ ] arr2 = { new Interval ( 6 , 8 ) , new Interval ( 1 , 3 ) , new Interval ( 2 , 4 ) , new Interval ( 4 , 7 ) } ; int n2 = arr2 . Length ; if ( isIntersect ( arr2 , n2 ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Print modified array after multiple array range increment operations | C # implementation to increment values in the given range by a value d for multiple queries ; structure to store the ( start , end ) index pair for each query ; function to increment values in the given range by a value d for multiple queries ; for each ( start , end ) index pair , perform the following operations on ' sum [ ] ' ; increment the value at index ' start ' by the given value ' d ' in ' sum [ ] ' ; if the index ' ( end + 1 ) ' exists then decrement the value at index ' ( end + 1 ) ' by the given value ' d ' in ' sum [ ] ' ; Now , perform the following operations : accumulate values in the ' sum [ ] ' array and then add them to the corresponding indexes in ' arr [ ] ' ; function to print the elements of the given array ; Driver code ; modifying the array for multiple queries
using System ; class GFG { public class query { public int start , end ; public query ( int start , int end ) { this . start = start ; this . end = end ; } } public static void incrementByD ( int [ ] arr , query [ ] q_arr , int n , int m , int d ) { int [ ] sum = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { sum [ q_arr [ i ] . start ] += d ; if ( ( q_arr [ i ] . end + 1 ) < n ) sum [ q_arr [ i ] . end + 1 ] -= d ; } arr [ 0 ] += sum [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; arr [ i ] += sum [ i ] ; } } public static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 4 , 8 , 6 , 1 } ; query [ ] q_arr = new query [ 5 ] ; q_arr [ 0 ] = new query ( 0 , 3 ) ; q_arr [ 1 ] = new query ( 4 , 5 ) ; q_arr [ 2 ] = new query ( 1 , 4 ) ; q_arr [ 3 ] = new query ( 0 , 1 ) ; q_arr [ 4 ] = new query ( 2 , 5 ) ; int n = arr . Length ; int m = q_arr . Length ; int d = 2 ; Console . WriteLine ( " Original ▁ Array : " ) ; printArray ( arr , n ) ; incrementByD ( arr , q_arr , n , m , d ) ; Console . WriteLine ( " STRNEWLINE Modified ▁ Array : " ) ; printArray ( arr , n ) ; } }
No of pairs ( a [ j ] >= a [ i ] ) with k numbers in range ( a [ i ] , a [ j ] ) that are divisible by x | C # program to calculate the number pairs satisfying th condition ; Function to calculate the number of pairs ; Traverse through all elements ; Current number 's divisor ; Use binary search to find the element after k multiples of x ; Use binary search to find the element after k + 1 multiples of x so that we get the answer bu subtracting ; The difference of index will be the answer ; Driver Code ; Function call to get the number of pairs
using System ; class GFG { static int countPairs ( int [ ] a , int n , int x , int k ) { Array . Sort ( a ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int d = ( a [ i ] - 1 ) / x ; int a1 = Math . Max ( ( d + k ) * x , a [ i ] ) ; int it1 = Array . BinarySearch ( a , a1 ) ; int a2 = Math . Max ( ( d + k + 1 ) * x , a [ i ] ) ; int it2 = Array . BinarySearch ( a , a2 ) ; ans += Math . Abs ( it2 - it1 ) ; } return ans ; } static void Main ( ) { int [ ] a = { 1 , 3 , 5 , 7 } ; int n = a . Length ; int x = 2 , k = 1 ; Console . Write ( countPairs ( a , n , x , k ) ) ; } }