codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the minimum array element by M subarray increments of size S | C # program for the above approach ; Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array or M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver code ; Function Call
using System ; class GFG { static int min1 ( int [ ] a , int n ) { int minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } static int maximizeMin ( int [ ] A , int N , int S , int M ) { int minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min1 ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == - 1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min1 ( A , N ) ; return A [ minIndex ] ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; int S = 2 , M = 3 ; Console . Write ( maximizeMin ( arr , N , S , M ) ) ; } }
Sum of nodes in the path from root to N | C # program for the above approach ; Function to find sum of aint nodes from root to N ; If N is equal to 1 ; If N is equal to 2 or 3 ; Stores the number of nodes at ( i + 1 ) - th level ; Stores the number of nodes ; Stores if the current level is even or odd ; If level is odd ; If level is even ; If level with node N is reached ; Push into vector ; Compute prefix sums of count of nodes in each level ; Stores the level in which node N s present ; Stores the required sum ; Add temp to the sum ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int sumOfPathNodes ( int N ) { if ( N == 1 ) { return 1 ; } else if ( N == 2 N == 3 ) { return N + 1 ; } List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; int k = 1 ; bool flag = true ; while ( k < N ) { if ( flag == true ) { k *= 2 ; flag = false ; } else { k *= 4 ; flag = true ; } if ( k > N ) { break ; } arr . Add ( k ) ; } int len = arr . Count ; int [ ] prefix = new int [ len ] ; prefix [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) { prefix [ i ] = arr [ i ] + prefix [ i - 1 ] ; } int it = lowerBound ( prefix , 0 , len , N ) + 1 ; int ind = it - prefix [ 0 ] ; int final_ans = 0 ; int temp = N ; while ( ind > 1 ) { int val = temp - prefix [ ind - 1 ] ; if ( ind % 2 != 0 ) { temp = prefix [ ind - 2 ] + ( val + 1 ) / 2 ; } else { temp = prefix [ ind - 2 ] + ( val + 3 ) / 4 ; } -- ind ; final_ans += temp ; } final_ans += ( N + 1 ) ; return final_ans ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void Main ( String [ ] args ) { int N = 13 ; Console . Write ( sumOfPathNodes ( N ) + " STRNEWLINE " ) ; } }
Check if given point lies in range of any of the given towers | C # program to implement the above approach ; Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise
using System ; class GFG { static bool checkPointRange ( int [ , ] arr , int X , int Y , int N ) { for ( int i = 0 ; i < N ; i ++ ) { double dist = Math . Sqrt ( ( arr [ i , 0 ] - X ) * ( arr [ i , 0 ] - X ) + ( arr [ i , 1 ] - Y ) * ( arr [ i , 1 ] - Y ) ) ; if ( dist <= arr [ i , 2 ] ) { return true ; } } return false ; } public static void Main ( ) { int [ , ] arr = { { 1 , 1 , 3 } , { 10 , 10 , 3 } , { 15 , 15 , 15 } } ; int X = 5 , Y = 5 ; int N = arr . Length ; if ( checkPointRange ( arr , X , Y , N ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }
Find the winner of the Game of removing odd or replacing even array elements | C # program for the above approach ; Function to evaluate the winner of the game ; Stores count of odd array elements ; Stores count of even array elements ; Traverse the array ; If array element is odd ; Otherwise ; If count of even is zero ; If count of odd is even ; If count of odd is odd ; If count of odd is odd and count of even is one ; Otherwise ; Driver Code
using System ; class GFG { static void findWinner ( int [ ] arr , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { Console . WriteLine ( " Player ▁ 2" ) ; } else if ( odd % 2 == 1 ) { Console . WriteLine ( " Player ▁ 1" ) ; } } else if ( even == 1 && odd % 2 == 1 ) { Console . WriteLine ( " Player ▁ 1" ) ; } else { Console . WriteLine ( - 1 ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 1 , 9 , 7 } ; int N = arr . Length ; findWinner ( arr , N ) ; } }
Rearrange array to maximize count of local minima | C # program to implement the above approach ; Function to rearrange array elements to maximize count of local minima in the array ; Sort the array in ascending order ; Stores index of left pointer ; Stores index of right pointer ; Traverse the array elements ; If right is less than N ; Print array element ; Update right ; Print array element ; Update left ; Driver Code
using System ; class GFG { static void rearrangeArrMaxcntMinima ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int left = 0 ; int right = N / 2 ; while ( left < N / 2 right < N ) { if ( right < N ) { Console . Write ( arr [ right ] + " ▁ " ) ; right ++ ; } if ( left < N / 2 ) { Console . Write ( arr [ left ] + " ▁ " ) ; left ++ ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; rearrangeArrMaxcntMinima ( arr , N ) ; } }
Minimum jumps required to make a group of persons sit together | C # program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the String ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the readonly count ; Driver Code ; Given arrange of seats ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int MOD = ( int ) 1e9 + 7 ; static int minJumps ( String seats ) { List < int > position = new List < int > ( ) ; int count = 0 ; int len = seats . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' x ' ) { position . Add ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position [ med_index ] ; int ans = 0 ; for ( int i = 0 ; i < position . Count ; i ++ ) { ans = ( ans % MOD + Math . Abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } public static void Main ( String [ ] args ) { String S = " . . . . x . . xx . . . x . . " ; Console . Write ( minJumps ( S ) ) ; } }
Count pairs whose product contains single distinct prime factor | C # program to implement the above approach ; Function to find a single distinct prime factor of N ; Stores distinct prime factors of N ; Calculate prime factor of N ; Calculate distinct prime factor ; Insert i into disPrimeFact ; Update N ; If N is not equal to 1 ; Insert N into disPrimeFact ; If N contains a single distinct prime factor ; Return single distinct prime factor of N ; If N contains more than one distinct prime factor ; Function to count pairs in the array whose product contains only single distinct prime factor ; Stores count of 1 s in the array ; mp [ i ] : Stores count of array elements whose distinct prime factor is only i ; Traverse the array arr [ ] ; If current element is 1 ; Store distinct prime factor of arr [ i ] ; If arr [ i ] contains more than one prime factor ; If arr [ i ] contains a single prime factor ; Stores the count of pairs whose product of elements contains only a single distinct prime factor ; Traverse the map mp [ ] ; Stores count of array elements whose prime factor is ( it . first ) ; Update res ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int singlePrimeFactor ( int N ) { HashSet < int > disPrimeFact = new HashSet < int > ( ) ; for ( int i = 2 ; i * i <= N ; ++ i ) { while ( N % i == 0 ) { disPrimeFact . Add ( i ) ; N /= i ; } } if ( N != 1 ) { disPrimeFact . Add ( N ) ; } if ( disPrimeFact . Count == 1 ) { foreach ( int i in disPrimeFact ) return i ; } return - 1 ; } static int cntsingleFactorPair ( int [ ] arr , int N ) { int countOf1 = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { countOf1 ++ ; continue ; } int factorValue = singlePrimeFactor ( arr [ i ] ) ; if ( factorValue == - 1 ) { continue ; } else { if ( mp . ContainsKey ( factorValue ) ) mp [ factorValue ] = mp [ factorValue ] + 1 ; else mp . Add ( factorValue , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < int , int > ele1 in mp ) { int X = ele1 . Value ; res += countOf1 * X + ( X * ( X - 1 ) ) / 2 ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( cntsingleFactorPair ( arr , N ) ) ; } }
Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | C # program to implement the above approach ; Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; If str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1
using System ; class GFG { static int MincntBothPalin ( string str1 , string str2 , int N ) { int i = 0 ; int j = N - 1 ; int cntOp = 0 ; while ( i < j ) { if ( str1 [ i ] == str1 [ j ] && str2 [ i ] != str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] == str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == str2 [ j ] && str2 [ i ] == str1 [ j ] ) { cntOp += 1 ; } else { cntOp += 2 ; } } i += 1 ; j -= 1 ; } return cntOp ; } public static void Main ( ) { string str1 = " dbba " ; string str2 = " abcd " ; int N = str1 . Length ; Console . WriteLine ( MincntBothPalin ( str1 , str2 , N ) ) ; } }
Find an integral solution of the non | C # program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code
using System ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( ( N & 1 ) != 0 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void findValX_Y ( int N ) { if ( N <= 1 ) { Console . WriteLine ( - 1 ) ; return ; } int xMax ; xMax = ( int ) Math . Log ( N ) ; int yMax ; yMax = ( int ) ( Math . Log ( N ) / Math . Log ( 5.0 ) ) ; for ( int i = 1 ; i <= xMax ; i ++ ) { for ( int j = 1 ; j <= yMax ; j ++ ) { int a = power ( 2 , i ) ; int b = power ( 5 , j ) ; if ( a + b == N ) { Console . Write ( i + " ▁ " + j ) ; return ; } } } Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int N = 129 ; findValX_Y ( N ) ; } }
Count pairs from two arrays with difference exceeding K | set 2 | C # program for the above approach ; Function to count pairs that satisfy the given conditions ; Stores the count of pairs ; If v1 [ ] is smaller than v2 [ ] ; Sort the array v1 [ ] ; Traverse the array v2 [ ] ; Returns the address of the first number which is >= v2 [ j ] - k ; Increase the count by all numbers less than v2 [ j ] - k ; Otherwise ; Sort the array v2 [ ] ; Traverse the array v1 [ ] ; Returns the address of the first number which is > v1 [ i ] + k ; Increase the count by all numbers greater than v1 [ i ] + k ; Return the total count of pairs ; Driver Code
using System ; class GFG { static int countPairs ( int [ ] v1 , int [ ] v2 , int n , int m , int k ) { int count = 0 ; if ( n <= m ) { Array . Sort ( v1 ) ; for ( int j = 0 ; j < m ; j ++ ) { int index = lowerBound ( v1 , 0 , n , v2 [ j ] - k ) ; count += index ; } } else { Array . Sort ( v2 ) ; for ( int i = 0 ; i < n ; i ++ ) { int index = upperBound ( v2 , 0 , m , v1 [ i ] + k ) ; count += m - index ; } } return count ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int upperBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 9 , 1 , 8 } ; int [ ] brr = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = arr . Length ; int M = brr . Length ; Console . Write ( countPairs ( arr , brr , N , M , K ) ) ; } }
Print siblings of a given Node in N | C # program for the above approach ; Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node / value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Driver Code ; Stores root of the constructed tree ; Print siblings of Node X
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public List < Node > child = new List < Node > ( ) ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; return temp ; } static void Siblings ( Node root , int value ) { int flag = 0 ; if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < temp . child . Count ; i ++ ) { if ( temp . child [ i ] . key == value ) { flag = 1 ; for ( int j = 0 ; j < temp . child . Count ; j ++ ) { if ( value != temp . child [ j ] . key ) Console . Write ( temp . child [ j ] . key + " ▁ " ) ; } break ; } q . Enqueue ( temp . child [ i ] ) ; } } if ( flag == 0 ) Console . Write ( " No ▁ siblings ! ! " ) ; } static Node constructTree ( ) { Node root = newNode ( 10 ) ; ( root . child ) . Add ( newNode ( 20 ) ) ; ( root . child ) . Add ( newNode ( 30 ) ) ; ( root . child ) . Add ( newNode ( 40 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 50 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 60 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 70 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 80 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 90 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 100 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 110 ) ) ; return root ; } public static void Main ( String [ ] args ) { Node root = constructTree ( ) ; int X = 30 ; Siblings ( root , X ) ; } }
Maximum sum subarray of size K with sum less than X | C # program for the above approach ; Function to calculate maximum sum among all subarrays of size K with the sum less than X ; Initialize sum_K to 0 ; Calculate sum of first K elements ; If sum_K is less than X ; Initialize MaxSum with sum_K ; Iterate over the array from ( K + 1 ) - th index ; Subtract the first element from the previous K elements and add the next element ; If sum_K is less than X ; Update the Max_Sum ; Driver Code ; Size of Array ; Function Call
using System ; class GFG { private static void maxSumSubarr ( int [ ] A , int N , int K , int X ) { int sum_K = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum_K += A [ i ] ; } int Max_Sum = 0 ; if ( sum_K < X ) { Max_Sum = sum_K ; } for ( int i = K ; i < N ; i ++ ) { sum_K -= ( A [ i - K ] - A [ i ] ) ; if ( sum_K < X ) { Max_Sum = Math . Max ( Max_Sum , sum_K ) ; } } Console . WriteLine ( Max_Sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 } ; int K = 5 ; int X = 30 ; int N = arr . Length ; maxSumSubarr ( arr , N , K , X ) ; } }
Rearrange array to make sum of all subarrays starting from first index non | C # program for the above approach ; Function to rearrange the array such that sum of all elements of subarrays from the 1 st index is non - zero ; Initialize sum of subarrays ; Sum of all elements of array ; If sum is 0 , the required array could never be formed ; If sum is non zero , array might be formed ; Sort array in ascending order ; When current subarray sum becomes 0 replace it with the largest element ; Swap Operation ; If largest element is same as element to be replaced , then rearrangement impossible ; If b = 1 , then rearrangement is not possible . Hence check with reverse configuration ; Sort array in descending order ; When current subarray sum becomes 0 replace it with the smallest element ; Swap Operation ; If smallest element is same as element to be replaced , then rearrangement impossible ; If neither of the configurations worked then print " - 1" ; Otherwise , print the formed rearrangement ; Driver Code ; Given array ; Size of array ; Function Call
using System ; class GFG { static void rearrangeArray ( int [ ] a , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } if ( sum == 0 ) { Console . Write ( " - 1" ) ; return ; } sum = 0 ; int b = 0 ; Array . Sort ( a ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ N - 1 ] ) { sum -= a [ i ] ; int temp = a [ i ] ; a [ i ] = a [ N - 1 ] ; a [ N - 1 ] = temp ; sum += a [ i ] ; } else { b = 1 ; break ; } } } if ( b == 1 ) { b = 0 ; sum = 0 ; Array . Sort ( a ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ 0 ] ) { sum -= a [ i ] ; int temp = a [ i ] ; a [ i ] = a [ 0 ] ; a [ 0 ] = temp ; sum += a [ i ] ; } else { b = 1 ; break ; } } } } if ( b == 1 ) { Console . Write ( " - 1" + " ▁ " ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , - 1 , 2 , 4 , 0 } ; int N = arr . Length ; rearrangeArray ( arr , N ) ; } }
Length of smallest subarray to be removed to make sum of remaining elements divisible by K | C # program for the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is divisible by K ; Stores the remainder of each arr [ i ] when divided by K ; Stores total sum of elements ; K has been added to each arr [ i ] to handle - ve integers ; Update the total sum ; Remainder when total_sum is divided by K ; If given array is already divisible by K ; Stores curr_remainder and the most recent index at which curr_remainder has occurred ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; If not possible ; Print the result ; Driver Code ; Given array [ ] arr ; Size of array ; Given K ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void removeSmallestSubarray ( int [ ] arr , int n , int k ) { int [ ] mod_arr = new int [ n ] ; int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mod_arr [ i ] = ( arr [ i ] + k ) % k ; total_sum += arr [ i ] ; } int target_remainder = total_sum % k ; if ( target_remainder == 0 ) { Console . Write ( "0" ) ; return ; } Dictionary < int , int > map1 = new Dictionary < int , int > ( ) ; map1 . Add ( 0 , - 1 ) ; int curr_remainder = 0 ; int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 [ curr_remainder ] = i ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . ContainsKey ( mod ) ) res = Math . Min ( res , i - map1 [ mod ] ) ; } if ( res == int . MaxValue res == n ) { res = - 1 ; } Console . Write ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 4 , 2 } ; int N = arr . Length ; int K = 6 ; removeSmallestSubarray ( arr , N , K ) ; } }
Queries to flip characters of a binary string in given range | C # program to implement the above approach ; Function to find the binary String by performing all the given queries ; Stores length of the String ; prefixCnt [ i ] : Stores number of times str [ i ] toggled by performing all the queries ; Update prefixCnt [ Q [ i , 0 ] ] ; Update prefixCnt [ Q [ i , 1 ] + 1 ] ; Calculate prefix sum of prefixCnt [ i ] ; Traverse prefixCnt [ ] array ; If ith element toggled odd number of times ; Toggled i - th element of binary String ; Driver Code
using System ; class GFG { static String toggleQuery ( char [ ] str , int [ , ] Q , int M ) { int N = str . Length ; int [ ] prefixCnt = new int [ N + 1 ] ; for ( int i = 0 ; i < M ; i ++ ) { prefixCnt [ Q [ i , 0 ] ] += 1 ; prefixCnt [ Q [ i , 1 ] + 1 ] -= 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { prefixCnt [ i ] += prefixCnt [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixCnt [ i ] % 2 == 1 ) { str [ i ] = ( char ) ( '1' - str [ i ] + '0' ) ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = "101010" ; int [ , ] Q = { { 0 , 1 } , { 2 , 5 } , { 2 , 3 } , { 1 , 4 } , { 0 , 5 } } ; int M = Q . GetLength ( 0 ) ; Console . Write ( toggleQuery ( str . ToCharArray ( ) , Q , M ) ) ; } }