codeparrot/xlcost-text-to-code huggingface dataset:
Sqrt ( or Square Root ) Decomposition Technique | Set 1 ( Introduction ) | C # program to demonstrate working of Square Root Decomposition . ; original array ; decomposed array ; block size ; Time Complexity : O ( 1 ) ; Time Complexity : O ( sqrt ( n ) ) ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Fills values in input [ ] ; initiating block pointer ; calculating size of block ; building the decomposed array ; entering next block incementing block pointer ; Driver code ; We have used separate array for input because the purpose of this code is to explain SQRT decomposition in competitive programming where we have multiple inputs .
using System ; class GFG { static int MAXN = 10000 ; static int SQRSIZE = 100 ; static int [ ] arr = new int [ MAXN ] ; static int [ ] block = new int [ SQRSIZE ] ; static int blk_sz ; static void update ( int idx , int val ) { int blockNumber = idx / blk_sz ; block [ blockNumber ] += val - arr [ idx ] ; arr [ idx ] = val ; } static int query ( int l , int r ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { sum += arr [ l ] ; l ++ ; } while ( l + blk_sz <= r ) { sum += block [ l / blk_sz ] ; l += blk_sz ; } while ( l <= r ) { sum += arr [ l ] ; l ++ ; } return sum ; } static void preprocess ( int [ ] input , int n ) { int blk_idx = - 1 ; blk_sz = ( int ) Math . Sqrt ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = input [ i ] ; if ( i % blk_sz == 0 ) { blk_idx ++ ; } block [ blk_idx ] += arr [ i ] ; } } public static void Main ( String [ ] args ) { int [ ] input = { 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 } ; int n = input . Length ; preprocess ( input , n ) ; Console . WriteLine ( " query ( 3 , ▁ 8 ) ▁ : ▁ " + query ( 3 , 8 ) ) ; Console . WriteLine ( " query ( 1 , ▁ 6 ) ▁ : ▁ " + query ( 1 , 6 ) ) ; update ( 8 , 0 ) ; Console . WriteLine ( " query ( 8 , ▁ 8 ) ▁ : ▁ " + query ( 8 , 8 ) ) ; } }
Find two numbers with sum N such that neither of them contains digit K | C # implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break String D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
using System ; class GFG { static String removeLeadingZeros ( String str ) { int i = 0 ; int n = str . Length ; while ( str [ i ] == '0' && i < n ) i ++ ; str = str . Substring ( i ) ; return str ; } static void findPairs ( int sum , int K ) { String A , B ; A = " " ; B = " " ; String N = String . Join ( " " , sum ) ; int n = N . Length ; for ( int i = 0 ; i < n ; i ++ ) { int D = N [ i ] - '0' ; if ( D == K ) { int D1 , D2 ; D1 = D / 2 ; D2 = D / 2 + D % 2 ; A = A + ( char ) ( D1 + '0' ) ; B = B + ( char ) ( D2 + '0' ) ; } else { A = A + ( char ) ( D + '0' ) ; B = B + '0' ; } } A = removeLeadingZeros ( A ) ; B = removeLeadingZeros ( B ) ; Console . Write ( A + " , ▁ " + B + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 33673 ; int K = 3 ; findPairs ( N , K ) ; } }
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | C # program to do range minimum query in O ( 1 ) time with O ( n * n ) extra space and O ( n * n ) preprocessing time . ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] ; Structure to represent a query range ; Fills lookup array lookup [ n ] [ n ] for all possible values of query ranges ; Initialize lookup [ ] [ ] for the intervals with length 1 ; Fill rest of the entries in bottom up manner ; To find minimum of [ 0 , 4 ] , we compare minimum of arr [ lookup [ 0 ] [ 3 ] ] with arr [ 4 ] . ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fill lookup table for all possible input queries ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver Code
using System ; class GFG { static int MAX = 500 ; static int [ , ] lookup = new int [ MAX , MAX ] ; public class Query { public int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i , i ] = i ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ lookup [ i , j - 1 ] ] < arr [ j ] ) lookup [ i , j ] = lookup [ i , j - 1 ] ; else lookup [ i , j ] = j ; } } static void RMQ ( int [ ] arr , int n , Query [ ] q , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; Console . WriteLine ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + arr [ lookup [ L , R ] ] ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . Length ; Query [ ] q = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . Length ; RMQ ( a , n , q , m ) ; } }
Minimum salary hike for each employee such that no employee feels unfair | C # program to find the minimum hike of each employee such that no adjacent employee feels unfair ; As hikes are positive integers , keeping minimum value ; Pass - 1 : compare with previous employee ; Pass - 2 : compare with Next employee ; Driver Code ; Function Call ; result -> [ 2 , 1 , 3 , 2 , 1 , 2 ] ; Function Call ; result -> [ 1 , 2 , 3 , 1 ] ; Function Call ; result -> [ 1 , 2 ]
using System ; class GFG { public static int [ ] findMinimumHike ( int [ ] ratings , int employees ) { int [ ] hikes = new int [ employees ] ; for ( int i = 0 ; i < employees ; i ++ ) hikes [ i ] = 1 ; for ( int i = 1 ; i < employees ; i ++ ) { if ( ratings [ i - 1 ] < ratings [ i ] && hikes [ i - 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i - 1 ] + 1 ; } for ( int i = employees - 2 ; i >= 0 ; i -- ) { if ( ratings [ i ] > ratings [ i + 1 ] && hikes [ i + 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i + 1 ] + 1 ; } return hikes ; } public static void Main ( String [ ] args ) { int [ ] data = new int [ ] { 5 , 3 , 4 , 2 , 1 , 6 } ; int [ ] result = findMinimumHike ( data , data . Length ) ; Console . WriteLine ( " [ " + String . Join ( " , " , result ) + " ] " ) ; data = new int [ ] { 1 , 3 , 5 , 4 } ; result = findMinimumHike ( data , data . Length ) ; Console . WriteLine ( " [ " + String . Join ( " , " , result ) + " ] " ) ; data = new int [ ] { 1 , 4 } ; result = findMinimumHike ( data , data . Length ) ; Console . WriteLine ( " [ " + String . Join ( " , " , result ) + " ] " ) ; } }
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | C # program to do range minimum query in O ( 1 ) time with O ( n Log n ) extra space and O ( n Log n ) preprocessing time ; lookup [ i , j ] is going to store index of minimum value in arr [ i . . j ] . Ideally lookup table size should not be fixed and should be determined using n Log n . It is kept constant to keep code simple . ; Structure to represent a query range ; Fills lookup array lookup [ , ] in bottom up manner . ; Initialize M for the intervals with length 1 ; Compute values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 , 10 ] , we compare arr [ lookup [ 0 , 3 ] ] and arr [ lookup [ 3 , 3 ] ] ; Returns minimum of arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , we compare arr [ lookup [ 0 , 3 ] ] and arr [ lookup [ 3 , 3 ] ] , ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fills table lookup [ n , Log n ] ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver Code
using System ; class GFG { static int MAX = 500 ; static int [ , ] lookup = new int [ MAX , MAX ] ; public class Query { public int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i , 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i , j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ] ) lookup [ i , j ] = lookup [ i , j - 1 ] ; else lookup [ i , j ] = lookup [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ; } } } static int query ( int [ ] arr , int L , int R ) { int j = ( int ) Math . Log ( R - L + 1 ) ; if ( arr [ lookup [ L , j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 , j ] ] ) return arr [ lookup [ L , j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 , j ] ] ; } static void RMQ ( int [ ] arr , int n , Query [ ] q , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; Console . WriteLine ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + query ( arr , L , R ) ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . Length ; Query [ ] q = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . Length ; RMQ ( a , n , q , m ) ; } }
Find sum of all nodes of the given perfect binary tree | C # code to find sum of all nodes of the given perfect binary tree ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static double sumNodes ( int l ) { double leafNodeCount = Math . Pow ( 2 , l - 1 ) ; double sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; double sum = sumLastLevel * l ; return sum ; } public static void Main ( ) { int l = 3 ; Console . Write ( sumNodes ( l ) ) ; } }
Longest subsequence with different adjacent characters | C # program for the above approach ; dp table ; A recursive function to find the update the [ , ] dp table ; If we reach end of the String ; If subproblem has been computed ; Initialise variable to find the maximum length ; Choose the current character ; Omit the current character ; Return the store answer to the current subproblem ; Function to find the longest Subsequence with different adjacent character ; Length of the String s ; Initialise the memoisation table ; Return the readonly ans after every recursive call ; Driver Code ; Function call
using System ; public class GFG { static int [ , ] dp = new int [ 100005 , 27 ] ; static int calculate ( int pos , int prev , String s ) { if ( pos == s . Length ) { return 0 ; } if ( dp [ pos , prev ] != - 1 ) return dp [ pos , prev ] ; int val = 0 ; if ( s [ pos ] - ' a ' + 1 != prev ) { val = Math . Max ( val , 1 + calculate ( pos + 1 , s [ pos ] - ' a ' + 1 , s ) ) ; } val = Math . Max ( val , calculate ( pos + 1 , prev , s ) ) ; return dp [ pos , prev ] = val ; } static int longestSubsequence ( String s ) { int n = s . Length ; for ( int i = 0 ; i < 100005 ; i ++ ) { for ( int j = 0 ; j < 27 ; j ++ ) { dp [ i , j ] = - 1 ; } } return calculate ( 0 , 0 , s ) ; } public static void Main ( String [ ] args ) { String str = " ababa " ; Console . Write ( longestSubsequence ( str ) ) ; } }
Constant time range add operation on an array | C # program to get updated array after many array range add operation ; Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries
using System ; class GFG { static void add ( int [ ] arr , int N , int lo , int hi , int val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } static void updateArray ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } static void printArr ( int [ ] arr , int N ) { updateArray ( arr , N ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( " " + arr [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( ) { int N = 6 ; int [ ] arr = new int [ N ] ; add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ; } }
Count of elements in an Array whose set bits are in a multiple of K | C # implementation of above approach ; Function to find the count of numbers ; Get the set - bits count of each element ; Check if the setbits count is divisible by K ; Increment the count of required numbers by 1 ;  ; Driver code
using System ; class GFG { static int find_count ( int [ ] arr , int k ) { int ans = 0 ; foreach ( int i in arr ) { int x = countSetBits ( i ) ; if ( x % k == 0 ) ans += 1 ; } return ans ; } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 345 , 2 , 68 , 7896 } ; int K = 2 ; Console . Write ( find_count ( arr , K ) ) ; } }
Check if a number can be expressed as a product of exactly K prime divisors | C # implementation to Check if a number can be expressed as a product of exactly K prime divisors ; function to find K prime divisors ; count number of 2 s that divide N ; N must be odd at this point . So we can skip one element ; divide the value of N ; increment count ; Condition to handle the case when n is a prime number greater than 2 ; check if maximum_split is less than K then it not possible ; Driver code ; initialise N and K
using System ; class GFG { static void KPrimeDivisors ( int N , int K ) { int maximum_split = 0 ; while ( N % 2 == 0 ) { maximum_split ++ ; N /= 2 ; } for ( int i = 3 ; i * i <= N ; i = i + 2 ) { while ( N % i == 0 ) { N = N / i ; maximum_split ++ ; } } if ( N > 2 ) maximum_split ++ ; if ( maximum_split < K ) { Console . WriteLine ( " No " ) ; return ; } Console . WriteLine ( " Yes " ) ; } public static void Main ( String [ ] args ) { int N = 12 ; int K = 3 ; KPrimeDivisors ( N , K ) ; } }
GCDs of given index ranges in an array | C # Program to find GCD of a number in a given Range using segment Trees ; Array to store segment tree ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Function to find gcd of 2 numbers . ; If b greater than a swap a and b ; A recursive function to get gcd of given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree si -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ si ] qs & qe -- > Starting and ending indexes of query range ; Finding The gcd of given Range ; Driver Code ; Starting index of range . ; Last index of range .
using System ; class GFG { private static int [ ] st ; public static int constructST ( int [ ] arr , int ss , int se , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return st [ si ] ; } int mid = ss + ( se - ss ) / 2 ; st [ si ] = gcd ( constructST ( arr , ss , mid , si * 2 + 1 ) , constructST ( arr , mid + 1 , se , si * 2 + 2 ) ) ; return st [ si ] ; } public static int [ ] constructSegmentTree ( int [ ] arr ) { int height = ( int ) Math . Ceiling ( Math . Log ( arr . Length ) / Math . Log ( 2 ) ) ; int size = 2 * ( int ) Math . Pow ( 2 , height ) - 1 ; st = new int [ size ] ; constructST ( arr , 0 , arr . Length - 1 , 0 ) ; return st ; } private static int gcd ( int a , int b ) { if ( a < b ) { int temp = b ; b = a ; a = temp ; } if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static int findGcd ( int ss , int se , int qs , int qe , int si ) { if ( ss > qe se < qs ) return 0 ; if ( qs <= ss && qe >= se ) return st [ si ] ; int mid = ss + ( se - ss ) / 2 ; return gcd ( findGcd ( ss , mid , qs , qe , si * 2 + 1 ) , findGcd ( mid + 1 , se , qs , qe , si * 2 + 2 ) ) ; } public static int findRangeGcd ( int ss , int se , int [ ] arr ) { int n = arr . Length ; if ( ss < 0 se > n - 1 ss > se ) { Console . WriteLine ( " Invalid ▁ arguments " ) ; return int . MinValue ; } return findGcd ( 0 , n - 1 , ss , se , 0 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 6 , 9 , 5 } ; constructSegmentTree ( a ) ; int l = 1 ; int r = 3 ; Console . Write ( " GCD ▁ of ▁ the ▁ given ▁ range ▁ is : ▁ " ) ; Console . Write ( findRangeGcd ( l , r , a ) ) ; } }
Find sum in range L to R in given sequence of integers | C # program to find the sum in given range L to R ; Function to find the sum within the given range ; Generating array from given sequence ; Calculate the desired sum ; return the sum ; Driver code ; Initialise the range
using System ; using System . Collections ; class GFG { public static int findSum ( int L , int R ) { ArrayList arr = new ArrayList ( ) ; int i = 0 ; int x = 2 ; while ( i <= R ) { arr . Add ( i + x ) ; if ( i + 1 <= R ) arr . Add ( i + 1 + x ) ; x *= - 1 ; i += 2 ; } int sum = 0 ; for ( i = L ; i <= R ; ++ i ) sum += ( int ) arr [ i ] ; return sum ; } public static void Main ( string [ ] args ) { int L = 0 , R = 5 ; Console . Write ( findSum ( L , R ) ) ; } }
Queries for GCD of all numbers of an array except elements in a given range | C # program for queries of GCD excluding given range of elements . ; Calculating GCD using euclid algorithm ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = GCD ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = GCD ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver Code
using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void FillPrefixSuffix ( int [ ] prefix , int [ ] arr , int [ ] suffix , int n ) { prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) ; suffix [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) ; } static int GCDoutsideRange ( int l , int r , int [ ] prefix , int [ ] suffix , int n ) { if ( l == 0 ) return suffix [ r + 1 ] ; if ( r == n - 1 ) return prefix [ l - 1 ] ; return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 2 , 6 , 9 } ; int n = arr . Length ; int [ ] prefix = new int [ n ] ; int [ ] suffix = new int [ n ] ; FillPrefixSuffix ( prefix , arr , suffix , n ) ; int l = 0 , r = 0 ; Console . WriteLine ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 1 ; Console . WriteLine ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 2 ; Console . Write ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; } }
Count of pairs from arrays A and B such that element in A is greater than element in B at that index | C # program to find the maximum count of values that follow the given condition ; Function to find the maximum count of values that follow the given condition ; Initializing the max - heap for the array A [ ] ; Adding the values of A [ ] into max heap ; Adding the values of B [ ] into max heap ; Counter variable ; Loop to iterate through the heap ; Comparing the values at the top . If the value of heap A [ ] is greater , then counter is incremented ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int check ( int [ ] A , int [ ] B , int N ) { List < int > pq1 = new List < int > ( ) ; List < int > pq2 = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . Add ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . Add ( B [ i ] ) ; } pq1 . Sort ( ) ; pq1 . Reverse ( ) ; pq2 . Sort ( ) ; pq2 . Reverse ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 [ 0 ] > pq2 [ 0 ] ) { c ++ ; pq1 . RemoveAt ( 0 ) ; pq2 . RemoveAt ( 0 ) ; } else { if ( pq2 . Count == 0 ) { break ; } pq2 . RemoveAt ( 0 ) ; } } return c ; } static public void Main ( ) { int [ ] A = { 10 , 3 , 7 , 5 , 8 } ; int [ ] B = { 8 , 6 , 2 , 5 , 9 } ; int N = A . Length ; Console . WriteLine ( check ( A , B , N ) ) ; } }
Number of elements less than or equal to a given number in a given subarray | Set 2 ( Including Updates ) | Number of elements less than or equal to a given number in a given subarray and allowing update operations . ; updating the bit array of a valid block ; answering the query ; traversing the first block in range ; Traversing completely overlapped blocks in range for such blocks bit array of that block is queried ; Traversing the last block ; Preprocessing the array ; updating the bit array at the original and new value of array ; Driver method ; size of block size will be equal to square root of n ; initialising bit array of each block as elements of array cannot exceed 10 ^ 4 so size of bit array is accordingly
using System ; class GFG { static int MAX = 10001 ; static void update ( int idx , int blk , int val , int [ , ] bit ) { for ( ; idx < MAX ; idx += ( idx & - idx ) ) bit [ blk , idx ] += val ; } static int query ( int l , int r , int k , int [ ] arr , int blk_sz , int [ , ] bit ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } while ( l + blk_sz <= r ) { int idx = k ; for ( ; idx > 0 ; idx -= idx & - idx ) sum += bit [ l / blk_sz , idx ] ; l += blk_sz ; } while ( l <= r ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } return sum ; } static void preprocess ( int [ ] arr , int blk_sz , int n , int [ , ] bit ) { for ( int i = 0 ; i < n ; i ++ ) update ( arr [ i ] , i / blk_sz , 1 , bit ) ; } static void preprocessUpdate ( int i , int v , int blk_sz , int [ ] arr , int [ , ] bit ) { update ( arr [ i ] , i / blk_sz , - 1 , bit ) ; update ( v , i / blk_sz , 1 , bit ) ; arr [ i ] = v ; } public static void Main ( ) { int [ ] arr = { 5 , 1 , 2 , 3 , 4 } ; int blk_sz = ( int ) Math . Sqrt ( arr . Length ) ; int [ , ] bit = new int [ blk_sz + 1 , MAX ] ; preprocess ( arr , blk_sz , arr . Length , bit ) ; Console . WriteLine ( query ( 1 , 3 , 1 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 3 , 10 , blk_sz , arr , bit ) ; Console . WriteLine ( query ( 3 , 3 , 4 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 2 , 1 , blk_sz , arr , bit ) ; preprocessUpdate ( 0 , 2 , blk_sz , arr , bit ) ; Console . WriteLine ( query ( 0 , 4 , 5 , arr , blk_sz , bit ) ) ; } }
Queries for counts of array elements with values in given range | Simple C # program to count number of elements with values in given range . ; function to count elements within given range ; initialize result ; check if element is in range ; Driver Code ; Answer queries
using System ; class GFG { static int countInRange ( int [ ] arr , int n , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 9 , 10 , 3 } ; int n = arr . Length ; int i = 1 , j = 4 ; Console . WriteLine ( countInRange ( arr , n , i , j ) ) ; i = 9 ; j = 12 ; Console . WriteLine ( countInRange ( arr , n , i , j ) ) ; } }