codeparrot/xlcost-text-to-code huggingface dataset:
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | C # program to find the smallest positive value that cannot be represented as sum of subsets of a given sorted array ; Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver code
using System ; class GFG { static int findSmallest ( int [ ] arr , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } public static void Main ( ) { int [ ] arr1 = { 1 , 3 , 4 , 5 } ; int n1 = arr1 . Length ; Console . WriteLine ( findSmallest ( arr1 , n1 ) ) ; int [ ] arr2 = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = arr2 . Length ; Console . WriteLine ( findSmallest ( arr2 , n2 ) ) ; int [ ] arr3 = { 1 , 1 , 1 , 1 } ; int n3 = arr3 . Length ; Console . WriteLine ( findSmallest ( arr3 , n3 ) ) ; int [ ] arr4 = { 1 , 1 , 3 , 4 } ; int n4 = arr4 . Length ; Console . WriteLine ( findSmallest ( arr4 , n4 ) ) ; } }
Count distinct possible Bitwise XOR values of subsets of an array | C # program for the above approach ; Stores the Bitwise XOR of every possible subset ; Function to generate all combinations of subsets and store their Bitwise XOR in set S ; If the end of the subset is reached ; Stores the Bitwise XOR of the current subset ; Iterate comb [ ] to find XOR ; Insert the Bitwise XOR of R elements ; Otherwise , iterate to generate all possible subsets ; Recursive call for next index ; Function to find the size of the set having Bitwise XOR of all the subsets of the given array ; Iterate ove the given array ; Generate all possible subsets ; Print the size of the set ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static HashSet < int > s ; static void countXOR ( int [ ] arr , int [ ] comb , int start , int end , int index , int r ) { if ( index == r ) { int new_xor = 0 ; for ( int j = 0 ; j < r ; j ++ ) { new_xor ^= comb [ j ] ; } s . Add ( new_xor ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { comb [ index ] = arr [ i ] ; countXOR ( arr , comb , i + 1 , end , index + 1 , r ) ; } } static void maxSizeSet ( int [ ] arr , int N ) { for ( int r = 1 ; r <= N ; r ++ ) { int [ ] comb = new int [ r + 1 ] ; countXOR ( arr , comb , 0 , N - 1 , 0 , r ) ; } Console . WriteLine ( s . Count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; s = new HashSet < int > ( ) ; maxSizeSet ( arr , N ) ; } }
Size of The Subarray With Maximum Sum | C # program to print length of the largest contiguous array sum ; Function to find maximum subarray sum ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } return ( end - start + 1 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . Length ; Console . Write ( maxSubArraySum ( a , n ) ) ; } }
Longest subarray of an array which is a subsequence in another array | C # program for the above approach ; Function to find the length of the longest subarray in arr1 [ ] which is a subsequence in arr2 [ ] ; Length of the array arr1 [ ] ; Length of the array arr2 [ ] ; Length of the required longest subarray ; Initialize DP [ ] array ; Traverse array arr1 [ ] ; Traverse array arr2 [ ] ; arr1 [ i - 1 ] contributes to the length of the subarray ; Otherwise ; Find the maximum value present in DP [ ] [ ] ; Return the result ; Driver Code ; Function call to find the length of the longest required subarray
using System ; class GFG { private static int LongSubarrSeq ( int [ ] arr1 , int [ ] arr2 ) { int M = arr1 . Length ; int N = arr2 . Length ; int maxL = 0 ; int [ , ] DP = new int [ M + 1 , N + 1 ] ; for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( arr1 [ i - 1 ] == arr2 [ j - 1 ] ) { DP [ i , j ] = 1 + DP [ i - 1 , j - 1 ] ; } else { DP [ i , j ] = DP [ i , j - 1 ] ; } } } for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { maxL = Math . Max ( maxL , DP [ i , j ] ) ; } } return maxL ; } static public void Main ( ) { int [ ] arr1 = { 4 , 2 , 3 , 1 , 5 , 6 } ; int [ ] arr2 = { 3 , 1 , 4 , 6 , 5 , 2 } ; Console . WriteLine ( LongSubarrSeq ( arr1 , arr2 ) ) ; } }
Find minimum difference between any two elements | C # implementation of simple method to find minimum difference between any pair ; Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver method to test the above function
using System ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { int diff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . Abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; Console . Write ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . Length ) ) ; } }
Minimize removal or insertions required to make two strings equal | C # program for the above approach ; Function to find the longest substring in string A which is a subsequence in B ; If the indices are out of bounds ; If an already computed subproblem occurred ; Required answer if the all the characters of A and B are the same ; Required answer if there is no substring A which is a subsequence in B ; Required answer if the current character in B is skipped ; The answer for the subproblem is the maximum among the three ; Function to return the minimum strings operations required to make two strings equal ; Initialize the dp vector ; Driver Code
using System ; class GFG { static int findLongestSubstring ( int posA , int posB , String A , String B , int canSkip , int n , int [ , , ] dp ) { if ( posA >= n posB >= n ) { return 0 ; } if ( dp [ posA , posB , canSkip ] != - 1 ) { return dp [ posA , posB , canSkip ] ; } int op1 = 0 ; int op2 = 0 ; int op3 = 0 ; if ( A [ posA ] == B [ posB ] ) { op1 = 1 + findLongestSubstring ( posA + 1 , posB + 1 , A , B , 0 , n , dp ) ; } if ( canSkip == 1 ) { op2 = findLongestSubstring ( posA + 1 , posB , A , B , canSkip , n , dp ) ; } op3 = findLongestSubstring ( posA , posB + 1 , A , B , canSkip , n , dp ) ; return dp [ posA , posB , canSkip ] = Math . Max ( op1 , Math . Max ( op2 , op3 ) ) ; } static void minOperations ( String A , String B , int N ) { int [ , , ] dp = new int [ N , N , 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . WriteLine ( N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ) ; } static public void Main ( ) { String A = " abab " ; String B = " baba " ; int N = A . Length ; minOperations ( A , B , N ) ; } }
Count possible N | C # program for the above approach ; Macros for modulus ; DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the readonly count ; Print the total count ; Driver Code ; Initialize the dp array with - 1 Fill each row with - 1. ; Function Call
using System ; using System . Collections . Generic ; using System ; using System . Collections . Generic ; public class GFG { static int MOD = 1000000007 ; static int [ , , ] dp = new int [ 5005 , 12 , 12 ] ; static int findCountUtil ( int N , int [ ] maxDigit , int position , int previous , int count ) { if ( position == N ) { return 1 ; } int ans = dp [ position , previous , count ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( count == 0 && previous != i ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } else if ( count != 0 ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , ( previous == i && position != 0 ) ? count - 1 : maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } } return ans ; } static void findCount ( int N , int [ ] maxDigit ) { int position = 0 ; int previous = 0 ; int count = 1 ; int ans = findCountUtil ( N , maxDigit , position , previous , count ) ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 2 ; int [ ] maxDigit = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { for ( int k = 0 ; k < dp . GetLength ( 2 ) ; k ++ ) dp [ i , j , k ] = - 1 ; } } findCount ( N , maxDigit ) ; } }
Find minimum difference between any two elements | C # program to find minimum difference between any pair in an unsorted array ; Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver Code
using System ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int diff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; Console . WriteLine ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . Length ) ) ; } }
Space optimization using bit manipulations | C # program to mark numbers as multiple of 2 or 5 ; Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
using System ; class GFG { static public void Main ( ) { int a = 2 , b = 10 ; int size = Math . Abs ( b - a ) + 1 ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; Console . WriteLine ( " MULTIPLES ▁ of ▁ 2" + " ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) Console . Write ( i + " ▁ " ) ; } }
Remove all nodes which don 't lie in any path with sum>= k | C # program to implement the above approach ; A utility function to get maximum of two integers ; A Binary Tree Node ; A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Initialize left and right sums as sum from root to this node ( including this node ) ; Recursively prune left and right subtrees ; Get the maximum of left and right sums ; If maximum is smaller than k , then this node must be deleted ; A wrapper over pruneUtil ( ) ; Driver Code ; k is 45
using System ; public class GFG { public static int max ( int l , int r ) { return ( l > r ? l : r ) ; } public class Node { public int data ; public Node left , right ; } public class INT { public int v ; public INT ( int a ) { v = a ; } } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return node ; } public static void print ( Node root ) { if ( root != null ) { print ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; print ( root . right ) ; } } public static Node pruneUtil ( Node root , int k , INT sum ) { if ( root == null ) { return null ; } INT lsum = new INT ( sum . v + ( root . data ) ) ; INT rsum = new INT ( lsum . v ) ; root . left = pruneUtil ( root . left , k , lsum ) ; root . right = pruneUtil ( root . right , k , rsum ) ; sum . v = max ( lsum . v , rsum . v ) ; if ( sum . v < k ) { root = null ; } return root ; } public static Node prune ( Node root , int k ) { INT sum = new INT ( 0 ) ; return pruneUtil ( root , k , sum ) ; } public static void Main ( string [ ] args ) { int k = 45 ; Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 12 ) ; root . right . right . left = newNode ( 10 ) ; root . right . right . left . right = newNode ( 11 ) ; root . left . left . right . left = newNode ( 13 ) ; root . left . left . right . right = newNode ( 14 ) ; root . left . left . right . right . left = newNode ( 15 ) ; Console . WriteLine ( " Tree ▁ before ▁ truncation STRNEWLINE " ) ; print ( root ) ; root = prune ( root , k ) ; Console . WriteLine ( " STRNEWLINE STRNEWLINE Tree ▁ after ▁ truncation STRNEWLINE " ) ; print ( root ) ; } }
Tree Traversals ( Inorder , Preorder and Postorder ) | C # program for different tree traversals ; Class containing left and right child of current node and key value ; Root of Binary Tree ; Given a binary tree , print its nodes according to the " bottom - up " postorder traversal . ; first recur on left subtree ; then recur on right subtree ; now deal with the node ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Given a binary tree , print its nodes in preorder ; first print data of node ; then recur on left sutree ; now recur on right subtree ; Wrappers over above recursive functions ; Driver Code
using System ; class Node { public int key ; public Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( ) { root = null ; } void printPostorder ( Node node ) { if ( node == null ) return ; printPostorder ( node . left ) ; printPostorder ( node . right ) ; Console . Write ( node . key + " ▁ " ) ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; Console . Write ( node . key + " ▁ " ) ; printInorder ( node . right ) ; } void printPreorder ( Node node ) { if ( node == null ) return ; Console . Write ( node . key + " ▁ " ) ; printPreorder ( node . left ) ; printPreorder ( node . right ) ; } void printPostorder ( ) { printPostorder ( root ) ; } void printInorder ( ) { printInorder ( root ) ; } void printPreorder ( ) { printPreorder ( root ) ; } static public void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; Console . WriteLine ( " Preorder ▁ traversal ▁ " + " of ▁ binary ▁ tree ▁ is ▁ " ) ; tree . printPreorder ( ) ; Console . WriteLine ( " STRNEWLINE Inorder ▁ traversal ▁ " + " of ▁ binary ▁ tree ▁ is ▁ " ) ; tree . printInorder ( ) ; Console . WriteLine ( " STRNEWLINE Postorder ▁ traversal ▁ " + " of ▁ binary ▁ tree ▁ is ▁ " ) ; tree . printPostorder ( ) ; } }
Maximum subarray sum possible after removing at most K array elements | C # program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores overlapping subproblems of the recurrence relation ; Initialize dp [ , ] to - 1 ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ , ] ; Update res ; If all array elements are negative ; Update res ; Driver Code
using System ; using System . Collections ; class GFG { static int M = 100 ; static int mxSubSum ( int i , int [ ] arr , int j , int [ , ] dp ) { if ( i == 0 ) { return dp [ i , j ] = Math . Max ( 0 , arr [ i ] ) ; } if ( dp [ i , j ] != - 1 ) { return dp [ i , j ] ; } int X = Math . Max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j , dp ) ) ; if ( j == 0 ) { return dp [ i , j ] = X ; } int Y = mxSubSum ( i - 1 , arr , j - 1 , dp ) ; return dp [ i , j ] = Math . Max ( X , Y ) ; } static int MaximumSubarraySum ( int n , int [ ] arr , int k ) { int [ , ] dp = new int [ M , M ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) dp [ i , j ] = - 1 ; mxSubSum ( n - 1 , arr , k , dp ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { res = Math . Max ( res , dp [ i , j ] ) ; } } Array . Sort ( arr ) ; if ( arr [ n - 1 ] < 0 ) { res = arr [ n - 1 ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 } ; int K = 1 ; int N = arr . Length ; Console . WriteLine ( MaximumSubarraySum ( N , arr , K ) ) ; } }
Minimum number of operations required to make all elements of at least one row of given Matrix prime | C # program to implement the above approach ; Function to generate all prime numbers using Sieve of Eratosthenes ; Function to check if a number is prime or not ; prime [ i ] : Check if i is a prime number or not ; Initialize prime [ ] array to true ; Iterate over the range [ 2 , sqrt ( n ) ] ; If p is a prime number ; Mark all multiples of i to false ; Update i ; Function to find minimum operations to make all elements of at least one row of the matrix as prime numbers ; dp [ i ] : Stores minimum operations to get i prime numbers in a row ; Initialize [ ] dp array to ( n + 1 ) ; Traverse the array ; Stores count of prime numbers in a i - th row ; Iterate over the range [ ( 1 << m ) - 1 , 0 ] ; If a row exist which contains j prime numbers ; Update dp [ j bitmask ] ; Update dp [ bitmask ] ; Return minimum operations to get a row of the matrix with all prime numbers ; Function to count prime numbers in a row ; i - th bit of bitmask check if i - th column is a prime or not ; Traverse the array ; if a [ i ] is a prime number ; Update bitmask ; Driver Code ; Stores count of columns in the matrix ; Stores length ; Calculate all prime numbers in range [ 1 , max ] using sieve ; Function Call
using System ; public class GFG { private static bool [ ] prime ; private static void sieve ( int n ) { prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } private static int MinWays ( int [ , ] a , int n , int m ) { int [ ] dp = new int [ 1 << m ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) { dp [ i ] = n + 1 ; } for ( int i = 0 ; i < a . GetLength ( 0 ) ; i ++ ) { int bitmask = BitMask ( GetRow ( a , i ) ) ; for ( int j = ( 1 << m ) - 1 ; j >= 0 ; j -- ) { if ( dp [ j ] != n + 1 ) { dp [ j bitmask ] = Math . Min ( dp [ j bitmask ] , dp [ j ] + 1 ) ; } } dp [ bitmask ] = 1 ; } return ( dp [ ( 1 << m ) - 1 ] - 1 ) == ( n + 1 ) ? - 1 : ( dp [ ( 1 << m ) - 1 ] - 1 ) ; } private static int BitMask ( int [ ] a ) { int bitmask = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( prime [ a [ i ] ] ) { bitmask |= ( 1 << i ) ; } } return bitmask ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 4 , 6 , 5 , 8 } , { 2 , 9 , 12 , 14 } , { 32 , 7 , 18 , 16 } , { 12 , 4 , 35 , 17 } } ; int m = mat . GetLength ( 0 ) ; int n = mat . GetLength ( 1 ) ; int max = 10000 ; sieve ( max ) ; Console . WriteLine ( MinWays ( mat , n , m ) ) ; } }
Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | C # program for the above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Initialize the table cost [ ] [ ] ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int INF = 10000 ; static void minCost ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int m = 2 * n ; int [ , ] cost = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { cost [ i , j ] = INF ; } } cost [ 0 , 0 ] = 0 ; int prev = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { prev = cost [ i - 1 , 0 ] ; for ( int j = 1 ; j <= m ; j ++ ) { cost [ i , j ] = Math . Min ( cost [ i , j ] , prev + Math . Abs ( j - arr [ i - 1 ] ) ) ; prev = Math . Min ( prev , cost [ i - 1 , j ] ) ; } } int minCost = INF ; for ( int j = 1 ; j <= m ; j ++ ) { minCost = Math . Min ( minCost , cost [ n , j ] ) ; } Console . Write ( minCost ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 4 , 5 , 2 } ; int N = arr . Length ; minCost ( arr , N ) ; } }
Space optimization using bit manipulations | C # code to for marking multiples ; index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
using System ; class GFG { static bool checkbit ( int [ ] array , int index ) { int val = array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; if ( val == 0 ) return false ; return true ; } static void setbit ( int [ ] array , int index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } public static void Main ( String [ ] args ) { int a = 2 , b = 10 ; int size = Math . Abs ( b - a ) ; size = ( int ) Math . Ceiling ( ( double ) size / 32 ) ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; Console . WriteLine ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) Console . Write ( i + " ▁ " ) ; } }