codeparrot/xlcost-text-to-code huggingface dataset:
Largest subset with M as smallest missing number | C # program to implement the above approach ; Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLengthOfMaxSubset ( int [ ] arr , int n , int m ) { HashSet < int > s = new HashSet < int > ( ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = arr [ i ] ; if ( tmp != m ) { s . Add ( tmp ) ; answer ++ ; } } int min = 1 ; while ( s . Contains ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 4 } ; int N = arr . Length ; int M = 3 ; Console . Write ( findLengthOfMaxSubset ( arr , N , M ) ) ; } }
Count of submatrix with sum X in a given Matrix | C # program for the above approach ; Size of a column ; Function to find the count of submatrix whose sum is X ; Copying arr to dp and making it indexed 1 ; Precalculate and store the sum of all rectangles with upper left corner at ( 0 , 0 ) ; ; Calculating sum in a 2d grid ; Stores the answer ; Minimum length of square ; Maximum length of square ; Flag to set if sub - square with sum X is found ; Calculate lower right index if upper right corner is at { i , j } ; Calculate the sum of elements in the submatrix with upper left column { i , j } and lower right column at { ni , nj } ; ; If sum X is found ; If sum > X , then size of the square with sum X must be less than mid ; If sum < X , then size of the square with sum X must be greater than mid ; If found , increment count by 1 ; ; Driver Code ; Given Matrix [ , ] arr ; Function call
using System ; class GFG { static readonly int m = 5 ; static int countSubsquare ( int [ , ] arr , int n , int X ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i + 1 , j + 1 ] = arr [ i , j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] + dp [ i , j - 1 ] - dp [ i - 1 , j - 1 ] ; } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int lo = 1 ; int hi = Math . Min ( n - i , m - j ) + 1 ; bool found = false ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int ni = i + mid - 1 ; int nj = j + mid - 1 ; int sum = dp [ ni , nj ] - dp [ ni , j - 1 ] - dp [ i - 1 , nj ] + dp [ i - 1 , j - 1 ] ; if ( sum >= X ) { if ( sum == X ) { found = true ; } hi = mid - 1 ; } else { lo = mid + 1 ; } } if ( found == true ) { cnt ++ ; } } } return cnt ; } public static void Main ( String [ ] args ) { int N = 4 , X = 10 ; int [ , ] arr = { { 2 , 4 , 3 , 2 , 10 } , { 3 , 1 , 1 , 1 , 5 } , { 1 , 1 , 2 , 1 , 4 } , { 2 , 1 , 1 , 1 , 3 } } ; Console . Write ( countSubsquare ( arr , N , X ) + " STRNEWLINE " ) ; } }
Check if two arrays can be made equal by reversing any subarray once | C # implementation to check whether two arrays can be made equal by reversing a sub - array only once ; Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
using System ; class GFG { static void checkArray ( int [ ] A , int [ ] B , int N ) { int start = 0 ; int end = N - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { start = i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { end = i ; break ; } } Array . Reverse ( A , start , end ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { Console . Write ( " No " ) ; return ; } } Console . Write ( " Yes " ) ; } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 3 , 2 , 4 } ; int [ ] B = { 1 , 2 , 3 , 4 } ; int N = A . Length ; checkArray ( A , B , N ) ; } }
Count of substrings having all distinct characters | C # program to implement the above appraoach ; Function to count total number of valid subStrings ; Stores the count of subStrings ; Stores the frequency of characters ; Initialised both pointers to beginning of the String ; If all characters in subString from index i to j are distinct ; Increment count of j - th character ; Add all subString ending at j and starting at any index between i and j to the answer ; Increment 2 nd pointer ; If some characters are repeated or j pointer has reached to end ; Decrement count of j - th character ; Increment first pointer ; Return the final count of subStrings ; Driver Code
using System ; class GFG { static int countSub ( String str ) { int n = ( int ) str . Length ; int ans = 0 ; int [ ] cnt = new int [ 26 ] ; int i = 0 , j = 0 ; while ( i < n ) { if ( j < n && ( cnt [ str [ j ] - ' a ' ] == 0 ) ) { cnt [ str [ j ] - ' a ' ] ++ ; ans += ( j - i + 1 ) ; j ++ ; } else { cnt [ str [ i ] - ' a ' ] -- ; i ++ ; } } return ans ; } public static void Main ( String [ ] args ) { String str = " gffg " ; Console . Write ( countSub ( str ) ) ; } }
Find largest factor of N such that N / F is less than K | C # program for the above approach ; Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function call
using System ; class GFG { static int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = Math . Max ( ans , j ) ; } if ( n / j <= k ) { ans = Math . Max ( ans , n / j ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int N = 8 , K = 7 ; Console . Write ( ( N / solve ( N , K ) ) ) ; } }
Maximum sum subarray of size range [ L , R ] | C # program to find Maximum sum subarray of size between L and R . ; function to find Maximum sum subarray of size between L and R ; calculating prefix sum ; maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; we maintain flag to counter if that initial 0 was erased from set or not . ; erase 0 from multiset once i = b ; insert pre [ i - L ] ; find minimum value in multiset . ; erase pre [ i - R ] ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void max_sum_subarray ( List < int > arr , int L , int R ) { int n = arr . Count ; int [ ] pre = new int [ n ] ; pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } List < int > s1 = new List < int > ( ) ; s1 . Add ( 0 ) ; int ans = Int32 . MinValue ; ans = Math . Max ( ans , pre [ L - 1 ] ) ; int flag = 0 ; for ( int i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { int it = s1 . IndexOf ( 0 ) ; s1 . RemoveAt ( it ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . Add ( pre [ i - L ] ) ; ans = Math . Max ( ans , pre [ i ] - s1 [ 0 ] ) ; if ( i - R >= 0 ) { int it = s1 . IndexOf ( pre [ i - R ] ) ; s1 . RemoveAt ( it ) ; } } Console . WriteLine ( ans ) ; } static void Main ( ) { int L , R ; L = 1 ; R = 3 ; List < int > arr = new List < int > ( ) { 1 , 2 , 2 , 1 } ; max_sum_subarray ( arr , L , R ) ; } }
Count of ways to select K consecutive empty cells from a given Matrix | C # program to find no of ways to select K consecutive empty cells from a row or column ; Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
using System ; class GFG { static int rowWise ( char [ , ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( v [ i , j ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } static int colWise ( char [ , ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j , i ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } public static void Main ( String [ ] args ) { int n = 3 , m = 3 , k = 1 ; char [ , ] v = { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; if ( k == 1 ) { Console . Write ( rowWise ( v , n , m , k ) ) ; } else { Console . Write ( colWise ( v , n , m , k ) + rowWise ( v , n , m , k ) ) ; } } }
Check if the count of inversions of two given types on an Array are equal or not | C # program to implement the above approach ; Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver code
using System ; class GFG { static bool solve ( int [ ] a , int n ) { int mx = Int32 . MinValue ; for ( int j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = Math . Max ( mx , a [ j - 1 ] ) ; } return true ; } public static void Main ( ) { int [ ] a = { 1 , 0 , 2 } ; int n = a . Length ; bool possible = solve ( a , n ) ; if ( possible ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of prime factors of N to be added at each step to convert N to M | C # program to find the minimum steps required to convert a number N to M . ; Array to store shortest prime factor of every integer ; Function to precompute shortest prime factors ; Function to insert distinct prime factors of every integer into a set ; Store distinct prime factors ; Function to return minimum steps using BFS ; Queue of pairs to store the current number and distance from root . ; Set to store distinct prime factors ; Run BFS ; Find out the prime factors of newNum ; Iterate over every prime factor of newNum . ; If M is obtained ; Return number of operations ; If M is exceeded ; Otherwise ; Update and store the new number obtained by prime factor ; If M cannot be obtained ; Driver code
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int [ ] spf = new int [ 100009 ] ; static void sieve ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( int i = 2 ; i * i <= 100005 ; i ++ ) { for ( int j = i ; j <= 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } static HashSet < int > findPrimeFactors ( HashSet < int > s , int n ) { while ( n > 1 ) { s . Add ( spf [ n ] ) ; n /= spf [ n ] ; } return s ; } static int MinimumSteps ( int n , int m ) { Queue < pair > q = new Queue < pair > ( ) ; HashSet < int > s = new HashSet < int > ( ) ; q . Enqueue ( new pair ( n , 0 ) ) ; while ( q . Count != 0 ) { int newNum = q . Peek ( ) . first ; int distance = q . Peek ( ) . second ; q . Dequeue ( ) ; HashSet < int > k = findPrimeFactors ( s , newNum ) ; foreach ( int i in k ) { if ( newNum == m ) { return distance ; } else if ( newNum > m ) { break ; } else { q . Enqueue ( new pair ( newNum + i , distance + 1 ) ) ; } } } return - 1 ; } public static void Main ( String [ ] args ) { int N = 7 , M = 16 ; sieve ( ) ; Console . Write ( MinimumSteps ( N , M ) ) ; } }
Count of elements which is product of a pair or an element square | C # Program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] v = new List < int > [ 100000 ] ; static void div ( int n ) { for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . Add ( i ) ; } } } static int prodof2elements ( int [ ] arr , int n ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } Array . Sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . Count == 0 ) div ( arr [ i ] ) ; foreach ( int j in v [ arr [ i ] ] ) { if ( Array . BinarySearch ( arr2 , j ) >= 0 && Array . BinarySearch ( arr2 , ( int ) arr [ i ] / j ) >= 0 ) { ans ++ ; break ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = arr . Length ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; Console . Write ( prodof2elements ( arr , N ) ) ; } }
Largest subarray with frequency of all elements same | C # program for the above approach ; Function to find maximum subarray size ; Generating all subarray i -> starting index j -> end index ; Map 1 to hash frequency of all elements in subarray ; Map 2 to hash frequency of all frequencies of elements ; ele_count is the previous frequency of arr [ j ] ; Finding previous frequency of arr [ j ] in map 1 ; Increasing frequency of arr [ j ] by 1 ; Check if previous frequency is present in map 2 ; Delete previous frequency if hash is equal to 1 ; Decrement the hash of previous frequency ; Incrementing hash of new frequency in map 2 ; Check if map2 size is 1 and updating answer ; Return the maximum size of subarray ; Driver Code ; Given array arr [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int max_subarray_size ( int N , int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { Dictionary < int , int > map1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > map2 = new Dictionary < int , int > ( ) ; for ( int j = i ; j < N ; j ++ ) { int ele_count ; if ( ! map1 . ContainsKey ( arr [ j ] ) ) { ele_count = 0 ; } else { ele_count = map1 [ arr [ j ] ] ; } if ( map1 . ContainsKey ( arr [ j ] ) ) { map1 [ arr [ j ] ] ++ ; } else { map1 . Add ( arr [ j ] , 1 ) ; } if ( map2 . ContainsKey ( ele_count ) ) { if ( map2 [ ele_count ] == 1 ) { map2 . Remove ( ele_count ) ; } else { map2 [ ele_count ] -- ; } } if ( map2 . ContainsKey ( ele_count + 1 ) ) { map2 [ ele_count + 1 ] ++ ; } else { map2 . Add ( ele_count + 1 , 1 ) ; } if ( map2 . Count == 1 ) ans = Math . Max ( ans , j - i + 1 ) ; } } return ans ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 5 , 6 , 5 , 6 } ; int N = arr . Length ; Console . WriteLine ( max_subarray_size ( N , arr ) ) ; } }
Longest substring consisting of vowels using Binary Search | C # implementation of the above approach ; Function to check if a character is vowel or not ; 0 - a 1 - b 2 - c and so on 25 - z ; Function to check if any subString of length k exists which contains only vowels ; Applying sliding window to get all subStrings of length K ; Remove the occurence of ( i - k + 1 ) th character ; Function to perform Binary Search ; Doing binary search on the lengths ; Driver Code
using System ; class GFG { static bool vowel ( int vo ) { if ( vo == 0 vo == 4 vo == 8 vo == 14 vo == 20 ) return true ; else return false ; } static bool check ( String s , int k ) { int [ ] cnt = new int [ 26 ] ; for ( int i = 0 ; i < k - 1 ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } for ( int i = k - 1 ; i < s . Length ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; int flag1 = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( vowel ( j ) == false && cnt [ j ] > 0 ) { flag1 = 1 ; break ; } } if ( flag1 == 0 ) return true ; cnt [ s [ i - k + 1 ] - ' a ' ] -- ; } return false ; } static int longestSubString ( String s ) { int l = 1 , r = s . Length ; int maxi = 0 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( check ( s , mid ) ) { l = mid + 1 ; maxi = Math . Max ( maxi , mid ) ; } else r = mid - 1 ; } return maxi ; } public static void Main ( String [ ] args ) { String s = " sedrewaefhoiu " ; Console . Write ( longestSubString ( s ) ) ; } }
Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | C # program to finds the number of pairs ( x , y ) from [ ] X and [ ] Y such that x ^ y > y ^ x ; Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver code
using System ; class GFG { static int countPairs ( int [ ] X , int [ ] Y , int m , int n ) { int [ ] suffix = new int [ 1005 ] ; long total_pairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( int i = ( int ) 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return ( int ) total_pairs ; } public static void Main ( String [ ] args ) { int [ ] X = { 10 , 19 , 18 } ; int [ ] Y = { 11 , 15 , 9 } ; int m = X . Length ; int n = Y . Length ; Console . Write ( countPairs ( X , Y , m , n ) ) ; } }
Min number of moves to traverse entire Matrix through connected cells with equal values | C # program to find the minimum number of moves to traverse a given matrix ; Function to find the minimum number of moves to traverse the given matrix ; Constructing another matrix consisting of distinct values ; Updating the array B by checking the values of A that if there are same values connected through an edge or not ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Store all distinct elements in a set ; Return answer ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int solve ( int [ , ] A , int N , int M ) { int [ , ] B = new int [ N , M ] ; int c = 1 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) B [ i , j ] = c ++ ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i != 0 ) { if ( A [ i - 1 , j ] == A [ i , j ] ) B [ i - 1 , j ] = B [ i , j ] ; } if ( i != N - 1 ) { if ( A [ i + 1 , j ] == A [ i , j ] ) B [ i + 1 , j ] = B [ i , j ] ; } if ( j != 0 ) { if ( A [ i , j - 1 ] == A [ i , j ] ) B [ i , j - 1 ] = B [ i , j ] ; } if ( j != M - 1 ) { if ( A [ i , j + 1 ] == A [ i , j ] ) B [ i , j + 1 ] = B [ i , j ] ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) s . Add ( B [ i , j ] ) ; } return s . Count ; } public static void Main ( String [ ] args ) { int N = 2 , M = 3 ; int [ , ] A = { { 2 , 1 , 3 } , { 1 , 1 , 2 } } ; Console . Write ( solve ( A , N , M ) ) ; } }
Number of times an array can be partitioned repetitively into two subarrays with equal sum | C # program for the above approach ; Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Driver Code ; Given array ; Function call
using System ; class GFG { static int splitArray ( int start , int end , int [ ] arr , int [ ] prefix_sum ) { if ( start >= end ) return 0 ; for ( int k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } static void solve ( int [ ] arr , int n ) { int [ ] prefix_sum = new int [ n + 1 ] ; prefix_sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } Console . Write ( splitArray ( 1 , n , arr , prefix_sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 3 , 3 , 0 , 3 , 3 } ; int N = arr . Length ; solve ( arr , N ) ; } }
Minimise the maximum element of N subarrays of size K | C # implementation to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; Calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
using System ; class GFG { static int minDays ( int [ ] arr , int n , int k ) { int l = arr . Length ; int left = 1 , right = ( int ) 1e9 ; if ( n * k > l ) return - 1 ; while ( left < right ) { int mid = ( left + right ) / 2 , cnt = 0 , product = 0 ; for ( int j = 0 ; j < l ; ++ j ) { if ( arr [ j ] > mid ) { cnt = 0 ; } else if ( ++ cnt >= k ) { product ++ ; cnt = 0 ; } } if ( product < n ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 3 , 10 , 2 } ; int n = 3 , k = 1 ; Console . Write ( minDays ( arr , n , k ) + " STRNEWLINE " ) ; } }
Count of distinct power of prime factor of N | C # program for the above approach ; Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Driver code ; Given Number N ; Function Call
using System ; class GFG { static int countFac ( int n ) { int m = n ; int count = 0 ; for ( int i = 2 ; ( i * i ) <= m ; ++ i ) { int total = 0 ; while ( n % i == 0 ) { n /= i ; ++ total ; } int temp = 0 ; for ( int j = 1 ; ( temp + j ) <= total ; ++ j ) { temp += j ; ++ count ; } } if ( n != 1 ) ++ count ; return count ; } public static void Main ( ) { int N = 24 ; Console . Write ( countFac ( N ) ) ; } }