codeparrot/xlcost-text-to-code huggingface dataset:
Sort the array in a given index range | C # program to sort the array in a given index range ; Function to sort the elements of the array from index a to index b ; Variables to store start and end of the index range ; Temporary array ; Sort the temporary array ; Modifying original array with temporary array elements ; Print the modified array ; Driver code ; length of the array
using System ; class GFG { static void partSort ( int [ ] arr , int N , int a , int b ) { int l = Math . Min ( a , b ) ; int r = Math . Max ( a , b ) ; int [ ] temp = new int [ r - l + 1 ] ; int j = 0 ; for ( int i = l ; i <= r ; i ++ ) { temp [ j ] = arr [ i ] ; j ++ ; } Array . Sort ( temp ) ; j = 0 ; for ( int i = l ; i <= r ; i ++ ) { arr [ i ] = temp [ j ] ; j ++ ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 7 , 8 , 4 , 5 , 2 } ; int a = 1 , b = 4 ; int N = arr . Length ; partSort ( arr , N , a , b ) ; } }
Sorting rows of matrix in descending order followed by columns in ascending order | C # implementation to sort the rows of matrix in descending order followed by sorting the columns in ascending order ; function to sort each row of the matrix according to the order specified by descending . ; function to sort each row of the matrix ; sorting row number ' i ' ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of [ , ] mat in descending order ; get transpose of [ , ] mat ; again sort rows of [ , ] mat in ascending order . ; again get transpose of [ , ] mat ; function to print the matrix ; Driver code
using System ; class GFG { static int MAX_SIZE = 10 ; static void sortByRow ( int [ , ] mat , int n , bool descending ) { int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( descending == true ) { int t = i ; for ( int p = 0 ; p < n ; p ++ ) { for ( int j = p + 1 ; j < n ; j ++ ) { if ( mat [ t , p ] < mat [ t , j ] ) { temp = mat [ t , p ] ; mat [ t , p ] = mat [ t , j ] ; mat [ t , j ] = temp ; } } } } else sortByRow ( mat , i , n ) ; } } static void sortByRow ( int [ , ] mat , int row , int n ) { for ( int i = row ; i < row + 1 ; i ++ ) { for ( int j = 0 ; j < n - 1 ; j ++ ) { if ( mat [ i , j ] > mat [ i , j + 1 ] ) { var temp = mat [ i , j ] ; mat [ i , j ] = mat [ i , j + 1 ] ; mat [ i , j + 1 ] = temp ; } } } } static void transpose ( int [ , ] mat , int n ) { int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { temp = mat [ i , j ] ; mat [ i , j ] = mat [ j , i ] ; mat [ j , i ] = temp ; } } } static void sortMatRowAndColWise ( int [ , ] mat , int n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } static void printMat ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int n = 3 ; int [ , ] mat = { { 3 , 2 , 1 } , { 9 , 8 , 7 } , { 6 , 5 , 4 } } ; Console . WriteLine ( " Original ▁ Matrix : " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; Console . WriteLine ( " STRNEWLINE Matrix ▁ After ▁ Sorting : " ) ; printMat ( mat , n ) ; } }
Move all zeroes to end of array | C # program to push zeroes to back of array ; Function which pushes all zeros to end of an array . ; Count of non - zero elements ; Traverse the array . If element encountered is non - zero , then replace the element at index A . . countA . . with this element ; here count is incremented ; Now all non - zero elements have been shifted to front and A . . countA . . is set as index of first 0. Make all elements 0 from count to end . ; Driver function
using System ; class PushZero { static void pushZerosToEnd ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = arr . Length ; pushZerosToEnd ( arr , n ) ; Console . WriteLine ( " Array ▁ after ▁ pushing ▁ all ▁ zeros ▁ to ▁ the ▁ back : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Move all zeroes to end of array | Set | C # implementation to move all zeroes at the end of array ; function to move all zeroes at the end of array ; Count of non - zero elements ; Traverse the array . If arr [ i ] is non - zero , then swap the element at index ' count ' with the element at index ' i ' ; function to print the array elements ; Driver program to test above
using System ; class GFG { static void moveZerosToEnd ( int [ ] arr , int n ) { int count = 0 ; int temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] != 0 ) ) { temp = arr [ count ] ; arr [ count ] = arr [ i ] ; arr [ i ] = temp ; count = count + 1 ; } } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = arr . Length ; Console . Write ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; moveZerosToEnd ( arr , n ) ; Console . Write ( " STRNEWLINE Modified ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
Double the first element and move zero to end | C # implementation to rearrange the array elements after modification ; function which pushes all zeros to end of an array . ; Count of non - zero elements ; Traverse the array . If element encountered is non - zero , then replace the element at index ' count ' with this element ; here count is incremented ; Now all non - zero elements have been shifted to front and ' count ' is set as index of first 0. Make all elements 0 from count to end . ; function to rearrange the array elements after modification ; if ' arr [ ] ' contains a single element only ; traverse the array ; if true , perform the required modification ; double current index value ; put 0 in the next index ; increment by 1 so as to move two indexes ahead during loop iteration ; push all the zeros at the end of ' arr [ ] ' ; function to print the array elements ; Driver program to test above
using System ; class GFG { static void pushZerosToEnd ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } static void modifyAndRearrangeArr ( int [ ] arr , int n ) { if ( n == 1 ) return ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( arr [ i ] != 0 ) && ( arr [ i ] == arr [ i + 1 ] ) ) { arr [ i ] = 2 * arr [ i ] ; arr [ i + 1 ] = 0 ; i ++ ; } } pushZerosToEnd ( arr , n ) ; } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 2 , 2 , 0 , 6 , 6 , 0 , 0 , 8 } ; int n = arr . Length ; Console . Write ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; modifyAndRearrangeArr ( arr , n ) ; Console . Write ( " Modified ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
Rearrange positive and negative numbers with constant extra space | C # program to Rearrange positive and negative numbers in a array ; A utility function to print an array of size n ; Function to Rearrange positive and negative numbers in a array ; if current element is positive do nothing ; if current element is negative , shift positive elements of arr [ 0. . i - 1 ] , to one position to their right ; Put negative element at its right position ; Driver program
using System ; class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void RearrangePosNeg ( int [ ] arr , int n ) { int key , j ; for ( int i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; if ( key > 0 ) continue ; j = i - 1 ; while ( j >= 0 && arr [ j ] > 0 ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } public static void Main ( ) { int [ ] arr = { - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 } ; int n = arr . Length ; RearrangePosNeg ( arr , n ) ; printArray ( arr , n ) ; } }
Rearrange positive and negative numbers with constant extra space | C # program to Rearrange positive and negative numbers in a array ; Function to print an array ; Function to reverse an array . An array can be reversed in O ( n ) time and O ( 1 ) space . ; Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; Initial index of 1 st subarray ; Initial index of IInd ; arr [ i . . m ] is positive ; arr [ j . . r ] is positive reverse positive part of left sub - array ( arr [ i . . m ] ) ; reverse negative part of right sub - array ( arr [ m + 1. . j - 1 ] ) ; reverse arr [ i . . j - 1 ] ; Function to Rearrange positive and negative numbers in a array ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; Driver code
using System ; class GFG { static void printArray ( int [ ] A , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; ; } static void reverse ( int [ ] arr , int l , int r ) { if ( l < r ) { arr = swap ( arr , l , r ) ; reverse ( arr , ++ l , -- r ) ; } } static void merge ( int [ ] arr , int l , int m , int r ) { int i = l ; int j = m + 1 ; while ( i <= m && arr [ i ] < 0 ) i ++ ; while ( j <= r && arr [ j ] < 0 ) j ++ ; reverse ( arr , i , m ) ; reverse ( arr , m + 1 , j - 1 ) ; reverse ( arr , i , j - 1 ) ; } static void RearrangePosNeg ( int [ ] arr , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; RearrangePosNeg ( arr , l , m ) ; RearrangePosNeg ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } 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 ( ) { int [ ] arr = { - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 } ; int arr_size = arr . Length ; RearrangePosNeg ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ; } }
Rearrange positive and negative numbers with constant extra space | C # implementation of the above approach ; Loop until arr [ i ] < 0 and still inside the array ; Loop until arr [ j ] > 0 and still inside the array ; if i is less than j ; Driver Code
using System ; public class GFG { public static void RearrangePosNeg ( int [ ] arr ) { int i = 0 ; int j = arr . Length - 1 ; while ( true ) { while ( arr [ i ] < 0 && i < arr . Length ) i ++ ; while ( arr [ j ] > 0 && j >= 0 ) j -- ; if ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } else break ; } } static public void Main ( ) { int [ ] arr = { - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 } ; RearrangePosNeg ( arr ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Find the player to be able to replace the last element that can be replaced by its divisors | C # program for the above approach ; Function to find the winner of the game played based on given conditions ; A wins if size of array is odd ; Otherwise , B wins ; Driver Code ; Input array ; Size of the array
using System ; public class GFG { static void winner ( int [ ] arr , int N ) { if ( N % 2 == 1 ) { Console . Write ( " A " ) ; } else { Console . Write ( " B " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 24 , 45 , 45 , 24 } ; int N = arr . Length ; winner ( arr , N ) ; } }
Queries to calculate sum of array elements present at every Yth index starting from the index X | C # program for the above approach ; Function to sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... for all possible values of X and Y , where Y is less than or equal to Math . Sqrt ( N ) . ; Iterate over all possible values of X ; Precompute for all possible values of an expression such that y <= Math . Sqrt ( N ) ; If i + j less than N ; Update dp [ i , j ] ; Update dp [ i , j ] ; Function to Find the sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... for all queries ; dp [ x , y ] : Stores sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... ; Traverse the query array , Q [ , ] ; If y is less than or equal to Math . Sqrt ( N ) ; Stores the sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... ; Traverse the array , [ ] arr ; Update sum ; Update x ; Driver Code
using System ; class GFG { static int sz = 20 ; static int sqr = ( int ) ( Math . Sqrt ( sz ) ) + 1 ; static void precomputeExpressionForAllVal ( int [ ] arr , int N , int [ , ] dp ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = 1 ; j <= Math . Sqrt ( N ) ; j ++ ) { if ( i + j < N ) { dp [ i , j ] = arr [ i ] + dp [ i + j , j ] ; } else { dp [ i , j ] = arr [ i ] ; } } } } static void querySum ( int [ ] arr , int N , int [ , ] Q , int M ) { int [ , ] dp = new int [ sz , sqr ] ; precomputeExpressionForAllVal ( arr , N , dp ) ; for ( int i = 0 ; i < M ; i ++ ) { int x = Q [ i , 0 ] ; int y = Q [ i , 1 ] ; if ( y <= Math . Sqrt ( N ) ) { Console . Write ( dp [ x , y ] + " ▁ " ) ; continue ; } int sum = 0 ; while ( x < N ) { sum += arr [ x ] ; x += y ; } Console . Write ( sum + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 7 , 5 , 4 } ; int [ , ] Q = { { 2 , 1 } , { 3 , 2 } } ; int N = arr . Length ; int M = Q . GetLength ( 0 ) ; querySum ( arr , N , Q , M ) ; } }
Find all elements in array which have at | C # program to find all elements in array which have at least two greater elements itself . ; Pick elements one by one and count greater elements . If count is more than 2 , print that element . ; Driver code
using System ; class GFG { static void findElements ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; if ( count >= 2 ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . Length ; findElements ( arr , n ) ; } }
Find all elements in array which have at | Sorting based C # program to find all elements in array which have atleast two greater elements itself . ; Driver code
using System ; class GFG { static void findElements ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . Length ; findElements ( arr , n ) ; } }
Find all elements in array which have at | C # program to find all elements in array which have atleast two greater elements itself . ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver code
using System ; class GFG { static void findElements ( int [ ] arr , int n ) { int first = int . MinValue ; int second = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < second ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . Length ; findElements ( arr , n ) ; } }
Minimize count of increments of each element of subarrays required to make Array non | C # program for the above approach ; Function to find the minimum number of operations required to make the array non - increasing ; Stores the count of required operations ; If arr [ i ] > arr [ i + 1 ] , no increments required . Otherwise , add their difference to the answer ; Return the result res ; Driver Code
using System ; class GFG { public static int getMinOps ( int [ ] arr ) { int res = 0 ; for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { res += Math . Max ( arr [ i + 1 ] - arr [ i ] , 0 ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 1 , 2 } ; Console . WriteLine ( getMinOps ( arr ) ) ; } }
Find the smallest missing number | C # program to find the smallest elements missing in a sorted array . ; function that returns smallest elements missing in a sorted array . ; Left half has all elements from 0 to mid ; Driver program to test the above function
using System ; class GFG { static int findFirstMissing ( int [ ] array , int start , int end ) { if ( start > end ) return end + 1 ; if ( start != array [ start ] ) return start ; int mid = ( start + end ) / 2 ; if ( array [ mid ] == mid ) return findFirstMissing ( array , mid + 1 , end ) ; return findFirstMissing ( array , start , mid ) ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 10 } ; int n = arr . Length ; Console . Write ( " smallest ▁ Missing ▁ element ▁ is ▁ : ▁ " + findFirstMissing ( arr , 0 , n - 1 ) ) ; } }
Find the smallest missing number | C # program for above approach ; Program to find missing element ; Index matches with value at that index , means missing element cannot be upto that point ; Program to find Smallest Missing in Sorted Array ; Check if 0 is missing in the array ; Check is all numbers 0 to n - 1 are prsent in array ; Driver code ; Function Call
using System ; class GFG { int findFirstMissing ( int [ ] arr , int start , int end , int first ) { if ( start < end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] != mid + first ) return findFirstMissing ( arr , start , mid , first ) ; else return findFirstMissing ( arr , mid + 1 , end , first ) ; } return start + first ; } int findSmallestMissinginSortedArray ( int [ ] arr ) { if ( arr [ 0 ] != 0 ) return 0 ; if ( arr [ arr . Length - 1 ] == arr . Length - 1 ) return arr . Length ; int first = arr [ 0 ] ; return findFirstMissing ( arr , 0 , arr . Length - 1 , first ) ; } static public void Main ( ) { GFG small = new GFG ( ) ; int [ ] arr = { 0 , 1 , 2 , 3 , 4 , 5 , 7 } ; int n = arr . Length ; Console . WriteLine ( " First ▁ Missing ▁ element ▁ is ▁ : ▁ " + small . findSmallestMissinginSortedArray ( arr ) ) ; } }
Maximum sum such that no two elements are adjacent | Program to return max sum such that no two elements are adjacent ; Function to return max sum such that no two elements are adjacent ; current max excluding i ; current max including i ; return max of incl and excl ; Driver program to test above functions
using System ; class GFG { static int FindMaxSum ( int [ ] arr , int n ) { int incl = arr [ 0 ] ; int excl = 0 ; int excl_new ; int i ; for ( i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 5 , 5 , 10 , 100 , 10 , 5 } ; Console . Write ( FindMaxSum ( arr , arr . Length ) ) ; } }
Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2 | C # program for the above approach ; Function for counting minimum number of changes ; Distance of elements from ( 0 , 0 ) will is i range [ 0 , n + m - 2 ] ; Store frequencies of [ 0 , 9 ] at distance i ; Initialize frequencies as 0 ; Count frequencies of [ 0 , 9 ] ; Increment frequency of value matrix [ i , j ] at distance i + j ; Find value with max frequency and count total cells at distance i from front end and rear end ; Change all values to the value with max frequency ; Return the answer ; Driver Code ; Given matrix ; Function call
using System ; class GFG { static readonly int N = 7 ; static int countChanges ( int [ , ] matrix , int n , int m ) { int dist = n + m - 1 ; int [ , ] freq = new int [ dist , 10 ] ; for ( int i = 0 ; i < dist ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) freq [ i , j ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { freq [ i + j , matrix [ i , j ] ] ++ ; } } int min_changes_sum = 0 ; for ( int i = 0 ; i < dist / 2 ; i ++ ) { int maximum = 0 ; int total_values = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) { maximum = Math . Max ( maximum , freq [ i , j ] + freq [ n + m - 2 - i , j ] ) ; total_values += ( freq [ i , j ] + freq [ n + m - 2 - i , j ] ) ; } min_changes_sum += ( total_values - maximum ) ; } return min_changes_sum ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 } , { 3 , 5 } } ; Console . Write ( countChanges ( mat , 2 , 2 ) ) ; } }
Sparse Table | C # program to do range minimum query using sparse table ; lookup [ i ] [ j ] is going to store 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 . ; 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 ] [ 7 ] ] and arr [ lookup [ 3 ] [ 10 ] ] ; Returns minimum of arr [ L . . R ] ; Find highest power of 2 that is smaller than or equal to count of elements in given range . For [ 2 , 10 ] , j = 3 ; Compute minimum of last 2 ^ j elements with first 2 ^ j elements in range . For [ 2 , 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] , ; Driver program
using System ; public class GFG { static int MAX = 500 ; static int [ , ] lookup = new int [ MAX , MAX ] ; static void buildSparseTable ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i , 0 ] = arr [ i ] ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( lookup [ i , j - 1 ] < 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 L , int R ) { int j = ( int ) Math . Log ( R - L + 1 ) ; if ( lookup [ L , j ] <= lookup [ R - ( 1 << j ) + 1 , j ] ) return lookup [ L , j ] ; else return lookup [ R - ( 1 << j ) + 1 , j ] ; } static public void Main ( ) { int [ ] a = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . Length ; buildSparseTable ( a , n ) ; Console . WriteLine ( query ( 0 , 4 ) ) ; Console . WriteLine ( query ( 4 , 7 ) ) ; Console . WriteLine ( query ( 7 , 8 ) ) ; } }
Sparse Table | C # program to do range minimum query using sparse table ; lookup [ i , j ] is going to store GCD of 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 . ; it builds sparse table . ; GCD of single element is element itself ; Build sparse table ; Returns GCD of arr [ L . . R ] ; Find highest power of 2 that is smaller than or equal to count of elements in given range . For [ 2 , 10 ] , j = 3 ; Compute GCD of last 2 ^ j elements with first 2 ^ j elements in range . For [ 2 , 10 ] , we find GCD of arr [ lookup [ 0 , 3 ] ] and arr [ lookup [ 3 , 3 ] ] , ; Driver Code
using System ; class GFG { static readonly int MAX = 500 ; static int [ , ] table = new int [ MAX , MAX ] ; static void buildSparseTable ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) table [ i , 0 ] = arr [ i ] ; for ( int j = 1 ; j <= n ; j ++ ) for ( int i = 0 ; i <= n - ( 1 << j ) ; i ++ ) table [ i , j ] = __gcd ( table [ i , j - 1 ] , table [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ) ; } static int query ( int L , int R ) { int j = ( int ) Math . Log ( R - L + 1 ) ; return __gcd ( table [ L , j ] , table [ R - ( 1 << j ) + 1 , j ] ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . Length ; buildSparseTable ( a , n ) ; Console . Write ( query ( 0 , 2 ) + " STRNEWLINE " ) ; Console . Write ( query ( 1 , 3 ) + " STRNEWLINE " ) ; Console . Write ( query ( 4 , 5 ) + " STRNEWLINE " ) ; } }