codeparrot/xlcost-text-to-code huggingface dataset:
Create a Sorted Array Using Binary Search | C # program to create a sorted array using Binary Search ; Function to create a new sorted array using Binary Search ; Auxiliary Array ; if b is empty any element can be at first place ; Perform Binary Search to find the correct position of current element in the new array ; let the element should be at first index ; if a [ j ] is already present in the new array ; add a [ j ] at mid + 1. you can add it at mid ; if a [ j ] is lesser than b [ mid ] go right side ; means pos should be between start and mid - 1 ; else pos should be between mid + 1 and end ; if a [ j ] is the largest push it at last ; here Max ( 0 , pos ) is used because sometimes pos can be negative as smallest duplicates can be present in the array ; Print the new generated sorted array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void createSorted ( int [ ] a , int n ) { List < int > b = new List < int > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( b . Count == 0 ) b . Add ( a [ j ] ) ; else { int start = 0 , end = b . Count - 1 ; int pos = 0 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( b [ mid ] == a [ j ] ) { b . Insert ( ( Math . Max ( 0 , mid + 1 ) ) , a [ j ] ) ; break ; } else if ( b [ mid ] > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . Insert ( Math . Max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 5 , 4 , 9 , 8 } ; int n = a . Length ; createSorted ( a , n ) ; } }
Largest sub | C # Implementation of the above approach ; Function that return true if frequency of all the present characters is at least k ; If the character is present and its frequency is less than k ; Function to set every frequency to zero ; Function to return the length of the longest sub - string such that it contains every character at least k times ; To store the required maximum length ; Starting index of the sub - string ; Ending index of the sub - string ; If the frequency of every character of the current sub - string is at least k ; Update the maximum possible length ; Driver code
using System ; class GFG { static int MAX = 26 ; static Boolean atLeastK ( int [ ] freq , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] < k ) return false ; } return true ; } static void setZero ( int [ ] freq ) { for ( int i = 0 ; i < MAX ; i ++ ) freq [ i ] = 0 ; } static int findlength ( String str , int n , int k ) { int maxLen = 0 ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { setZero ( freq ) ; for ( int j = i ; j < n ; j ++ ) { freq [ str [ j ] - ' a ' ] ++ ; if ( atLeastK ( freq , k ) ) { maxLen = Math . Max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } public static void Main ( String [ ] args ) { String str = " xyxyyz " ; int n = str . Length ; int k = 2 ; Console . WriteLine ( findlength ( str , n , k ) ) ; } }
Minimum increments to convert to an array of consecutive integers | C # implementation of the approach ; Function that return true if the required array can be generated with m as the last element ; Build the desired array ; Check if the given array can be converted to the desired array with the given operation ; Function to return the minimum number of operations required to convert the given array to an increasing AP series with common difference as 1 ; Apply Binary Search ; If array can be generated with mid as the last element ; Current ans is mid ; Check whether the same can be achieved with even less operations ; Build the desired array ; Calculate the number of operations required ; Return the number of operations required ; Driver code
using System ; using System . Linq ; class GFG { static Boolean check ( int m , int n , int [ ] arr ) { int [ ] desired = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = m ; m -- ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > desired [ i ] desired [ i ] < 1 ) { return false ; } } return true ; } static int minOperations ( int [ ] arr , int n ) { int start = ( int ) arr [ n - 1 ] ; int end = arr . Max ( ) + n ; int max_arr = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , n , arr ) ) { max_arr = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } int [ ] desired = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = max_arr ; max_arr -- ; } int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { operations += ( desired [ i ] - arr [ i ] ) ; } return operations ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 4 , 5 , 5 , 7 } ; int n = arr . Length ; Console . WriteLine ( minOperations ( arr , n ) ) ; } }
Count duplicates in a given linked list | C # implementation of the approach ; Representation of node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Create a hash table insert head ; Traverse through remaining nodes ; Return the count of duplicate nodes ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head ; static void insert ( Node ref_head , int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . next = ref_head ; head = temp ; } static int countNode ( Node head ) { if ( head == null ) return 0 ; ; HashSet < int > s = new HashSet < int > ( ) ; s . Add ( head . data ) ; int count = 0 ; for ( Node curr = head . next ; curr != null ; curr = curr . next ) { if ( s . Contains ( curr . data ) ) count ++ ; s . Add ( curr . data ) ; } return count ; } public static void Main ( String [ ] args ) { insert ( head , 5 ) ; insert ( head , 7 ) ; insert ( head , 5 ) ; insert ( head , 1 ) ; insert ( head , 7 ) ; Console . WriteLine ( countNode ( head ) ) ; } }
Minimize the number of steps required to reach the end of the array | Set 2 | A C # implementation of the approach ; Function to return the minimum steps required to reach the end of the given array ; Array to determine whether a cell has been visited before ; Queue for bfs ; Push the source i . e . index 0 ; Variable to store the depth of search ; BFS algorithm ; Current queue size ; Top - most element of queue ; Base case ; If we reach the destination i . e . index ( n - 1 ) ; Marking the cell visited ; Pushing the adjacent nodes i . e . indices reachable from the current index ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minSteps ( int [ ] arr , int n ) { Boolean [ ] v = new Boolean [ n ] ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 0 ) ; int depth = 0 ; while ( q . Count > 0 ) { int x = q . Count ; while ( x -- > 0 ) { int i = q . Peek ( ) ; q . Dequeue ( ) ; if ( v [ i ] ) continue ; if ( i == n - 1 ) return depth ; v [ i ] = true ; if ( i + arr [ i ] < n ) q . Enqueue ( i + arr [ i ] ) ; if ( i - arr [ i ] >= 0 ) q . Enqueue ( i - arr [ i ] ) ; } depth ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( minSteps ( arr , n ) ) ; } }
Find the winner of the game based on greater number of divisors | C # implementation of the approach ; Function to return the count of divisors of elem ; Function to return the winner of the game ; Convert every element of A [ ] to their divisor count ; Convert every element of B [ ] to their divisor count ; Sort both the arrays ; For every element of A apply Binary Search to find number of pairs where A wins ; B wins if A doesnot win ; Driver code
using System ; class GFG { static int divisorcount ( int elem ) { int ans = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( elem ) ; i ++ ) { if ( elem % i == 0 ) { if ( i * i == elem ) ans ++ ; else ans += 2 ; } } return ans ; } static string findwinner ( int [ ] A , int [ ] B , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = divisorcount ( A [ i ] ) ; } for ( int i = 0 ; i < M ; i ++ ) { B [ i ] = divisorcount ( B [ i ] ) ; } Array . Sort ( A ) ; Array . Sort ( B ) ; int winA = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = A [ i ] ; int start = 0 ; int end = M - 1 ; int index = - 1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( B [ mid ] <= val ) { index = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } winA += ( index + 1 ) ; } int winB = N * M - winA ; if ( winA > winB ) { return " A " ; } else if ( winB > winA ) { return " B " ; } return " Draw " ; } public static void Main ( ) { int [ ] A = { 4 , 12 , 24 } ; int N = A . Length ; int [ ] B = { 25 , 28 , 13 , 45 } ; int M = B . Length ; Console . Write ( findwinner ( A , B , N , M ) ) ; } }
Minimum time required to transport all the boxes from source to the destination under the given constraints | C # implementation of the approach ; Function that returns true if it is possible to transport all the boxes in the given amount of time ; If all the boxes can be transported in the given time ; If all the boxes can 't be  transported in the given time ; Function to return the minimum time required ; Sort the two arrays ; Stores minimum time in which all the boxes can be transported ; Check for the minimum time in which all the boxes can be transported ; If it is possible to transport all the boxes in mid amount of time ; Driver code
using System ; class GFG { static bool isPossible ( int [ ] box , int [ ] truck , int n , int m , int min_time ) { int temp = 0 ; int count = 0 ; while ( count < m ) { for ( int j = 0 ; j < min_time && temp < n && truck [ count ] >= box [ temp ] ; j += 2 ) temp ++ ; count ++ ; } if ( temp == n ) return true ; return false ; } static int minTime ( int [ ] box , int [ ] truck , int n , int m ) { Array . Sort ( box ) ; Array . Sort ( truck ) ; int l = 0 ; int h = 2 * n ; int min_time = 0 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } public static void Main ( String [ ] args ) { int [ ] box = { 10 , 2 , 16 , 19 } ; int [ ] truck = { 29 , 25 } ; int n = box . Length ; int m = truck . Length ; Console . WriteLine ( " { 0 } " , minTime ( box , truck , n , m ) ) ; } }
Maximum points covered after removing an Interval | C # implementation of the approach ; Function To find the required interval ; Total Count of covered numbers ; Array to store numbers that occur exactly in one interval till ith interval ; Calculate New count by removing all numbers in range [ l , r ] occurring exactly once ; Driver code
using System ; class GFG { static void solve ( int [ , ] interval , int N , int Q ) { int [ ] Mark = new int [ Q ] ; for ( int i = 0 ; i < N ; i ++ ) { int l = interval [ i , 0 ] - 1 ; int r = interval [ i , 1 ] - 1 ; for ( int j = l ; j <= r ; j ++ ) Mark [ j ] ++ ; } int count = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( Mark [ i ] != 0 ) count ++ ; } int [ ] count1 = new int [ Q ] ; if ( Mark [ 0 ] == 1 ) count1 [ 0 ] = 1 ; for ( int i = 1 ; i < Q ; i ++ ) { if ( Mark [ i ] == 1 ) count1 [ i ] = count1 [ i - 1 ] + 1 ; else count1 [ i ] = count1 [ i - 1 ] ; } int maxindex = 0 ; int maxcoverage = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int l = interval [ i , 0 ] - 1 ; int r = interval [ i , 1 ] - 1 ; int elem1 ; if ( l != 0 ) elem1 = count1 [ r ] - count1 [ l - 1 ] ; else elem1 = count1 [ r ] ; if ( count - elem1 >= maxcoverage ) { maxcoverage = count - elem1 ; maxindex = i ; } } Console . WriteLine ( " Maximum ▁ Coverage ▁ is ▁ " + maxcoverage + " ▁ after ▁ removing ▁ interval ▁ at ▁ index ▁ " + maxindex ) ; } public static void Main ( ) { int [ , ] interval = { { 1 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 3 , 5 } } ; int N = interval . Length ; int Q = 7 ; solve ( interval , N / 2 , Q ) ; } }
Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps | C # program to implement the above approach ; Function that returns true if it is possible to reach end of the array in exactly k jumps ; Variable to store the number of steps required to reach the end ; If it is possible to reach the end in exactly k jumps ; Returns the minimum maximum distance required to reach the end of the array in exactly k jumps ; Stores the answer ; Binary search to calculate the result ; Driver code
using System ; class GFG { static bool isPossible ( int [ ] arr , int n , int dist , int k ) { int req = 0 ; int curr = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( curr != n && arr [ curr ] - arr [ prev ] <= dist ) { curr ++ ; } req ++ ; if ( curr == n ) { break ; } prev = curr - 1 ; } if ( curr != n ) { return false ; } if ( req <= k ) { return true ; } return false ; } static int minDistance ( int [ ] arr , int n , int k ) { int l = 0 ; int h = arr [ n - 1 ] ; int ans = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( isPossible ( arr , n , m , k ) ) { ans = m ; h = m - 1 ; } else { l = m + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 15 , 36 , 43 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( minDistance ( arr , n , k ) ) ; } }
Find the kth element in the series generated by the given N ranges | C # implementation of the approach ; Function to return the kth element of the required series ; To store the number of integers that lie upto the ith index ; Compute the number of integers ; Stores the index , lying from 1 to n , ; Using binary search , find the index in which the kth element will lie ; Find the position of the kth element in the interval in which it lies ; Driver code
using System ; class GFG { static int getKthElement ( int n , int k , int [ ] L , int [ ] R ) { int l = 1 ; int h = n ; int [ ] total = new int [ n + 1 ] ; total [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total [ i + 1 ] = total [ i ] + ( R [ i ] - L [ i ] ) + 1 ; } int index = - 1 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( total [ m ] > k ) { index = m ; h = m - 1 ; } else if ( total [ m ] < k ) l = m + 1 ; else { index = m ; break ; } } l = L [ index - 1 ] ; h = R [ index - 1 ] ; int x = k - total [ index - 1 ] ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( m - L [ index - 1 ] ) + 1 == x ) { return m ; } else if ( ( m - L [ index - 1 ] ) + 1 > x ) h = m - 1 ; else l = m + 1 ; } return k ; } public static void Main ( ) { int [ ] L = { 1 , 8 , 21 } ; int [ ] R = { 4 , 10 , 23 } ; int n = L . Length ; int k = 6 ; Console . WriteLine ( getKthElement ( n , k , L , R ) ) ; } }
Find minimum positive integer x such that a ( x ^ 2 ) + b ( x ) + c >= k | C # implementation of the approach ; Function to return the minimum positive integer satisfying the given equation ; Binary search to find the value of x ; Return the answer ; Driver code
using System ; class GFG { static int MinimumX ( int a , int b , int c , int k ) { int x = int . MaxValue ; if ( k <= c ) return 0 ; int h = k - c ; int l = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . Min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } public static void Main ( ) { int a = 3 , b = 2 , c = 4 , k = 15 ; Console . Write ( MinimumX ( a , b , c , k ) ) ; } }
Divide array into two parts with equal sum according to the given constraints | C # implementation of the approach ; Function that checks if the given conditions are satisfied ; To store the prefix sum of the array elements ; Sort the array ; Compute the prefix sum array ; Maximum element in the array ; Variable to check if there exists any number ; Stores the index of the largest number present in the array smaller than i ; Stores the index of the smallest number present in the array greater than i ; Find index of smallest number greater than i ; Find index of smallest number greater than i ; If there exists a number ; If no such number exists print no ; Driver code
using System ; class GFG { static void IfExists ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; Array . Sort ( arr ) ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; int max = arr [ n - 1 ] ; bool flag = false ; for ( int i = 1 ; i <= max ; i ++ ) { int findex = 0 ; int lindex = 0 ; int l = 0 ; int r = n - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( arr [ m ] < i ) { findex = m ; l = m + 1 ; } else r = m - 1 ; } l = 1 ; r = n ; flag = false ; while ( l <= r ) { int m = ( r + l ) / 2 ; if ( arr [ m ] > i ) { lindex = m ; r = m - 1 ; } else l = m + 1 ; } if ( sum [ findex ] == sum [ n - 1 ] - sum [ lindex - 1 ] ) { flag = true ; break ; } } if ( flag ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 5 } ; int n = arr . Length ; IfExists ( arr , n ) ; } }
Find missing element in a sorted array of consecutive numbers | C # implementation of the approach ; Function to return the missing element ; Check if middle element is consistent ; No inconsistency till middle elements When missing element is just after the middle element ; Move right ; Inconsistency found When missing element is just before the middle element ; Move left ; No missing element found ; Driver code
using System ; class GFG { public static int findMissing ( int [ ] arr , int n ) { int l = 0 , h = n - 1 ; int mid ; while ( h > l ) { mid = l + ( h - l ) / 2 ; if ( arr [ mid ] - mid == arr [ 0 ] ) { if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) return arr [ mid ] + 1 ; else { l = mid + 1 ; } } else { if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) return arr [ mid ] - 1 ; else { h = mid - 1 ; } } } return - 1 ; } public static void Main ( ) { int [ ] arr = { - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 } ; int n = arr . Length ; Console . WriteLine ( findMissing ( arr , n ) ) ; } }
Find maximum sum taking every Kth element in the array | C # implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Find maximum from all sequences ; Sum of the sequence starting from index i ; Update maximum ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n , int K ) { int maximum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int sumk = 0 ; for ( int j = i ; j < n ; j += K ) sumk = sumk + arr [ j ] ; maximum = Math . Max ( maximum , sumk ) ; } return maximum ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . Length ; int K = 2 ; Console . WriteLine ( maxSum ( arr , n , K ) ) ; } }
Find the number of elements greater than k in a sorted array | C # implementation of the approach ; Function to return the count of elements from the array which are greater than k ; Stores the index of the left most element from the array which is greater than k ; Finds number of elements greater than k ; If mid element is greater than k update leftGreater and r ; If mid element is less than or equal to k update l ; Return the count of elements greater than k ; Driver code
using System ; class GFG { static int countGreater ( int [ ] arr , int n , int k ) { int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 } ; int n = arr . Length ; int k = 7 ; Console . WriteLine ( countGreater ( arr , n , k ) ) ; } }
Count the number of operations required to reduce the given number | C # implementation of the approach ; To store the normalized value of all the operations ; Minimum possible value for a series of operations ; If k can be reduced with first ( i + 1 ) operations ; Impossible to reduce k ; Number of times all the operations can be performed on k without reducing it to <= 0 ; Perform operations ; Final check ; Driver code
using System ; class GFG { static int operations ( int [ ] op , int n , int k ) { int i , count = 0 ; int nVal = 0 ; int min = int . MaxValue ; for ( i = 0 ; i < n ; i ++ ) { nVal += op [ i ] ; min = Math . Min ( min , nVal ) ; if ( ( k + nVal ) <= 0 ) return ( i + 1 ) ; } if ( nVal >= 0 ) return - 1 ; int times = ( k - Math . Abs ( min ) ) / Math . Abs ( nVal ) ; k = ( k - ( times * Math . Abs ( nVal ) ) ) ; count = ( times * n ) ; while ( k > 0 ) { for ( i = 0 ; i < n ; i ++ ) { k = k + op [ i ] ; count ++ ; if ( k <= 0 ) break ; } } return count ; } static void Main ( ) { int [ ] op = { - 60 , 65 , - 1 , 14 , - 25 } ; int n = op . Length ; int k = 100000 ; Console . WriteLine ( operations ( op , n , k ) ) ; } }
Uniform Binary Search | C # implementation of above approach ; lookup table ; create the lookup table for an array of length n ; power and count variable ; multiply by 2 ; initialize the lookup table ; binary search ; mid point of the array ; count ; if the value is found ; if value is less than the mid value ; if value is greater than the mid value ; Driver code ; create the lookup table ; print the position of the array
using System ; class GFG { static int MAX_SIZE = 1000 ; static int [ ] lookup_table = new int [ MAX_SIZE ] ; static void create_table ( int n ) { int pow = 1 ; int co = 0 ; do { pow <<= 1 ; lookup_table [ co ] = ( n + ( pow >> 1 ) ) / pow ; } while ( lookup_table [ co ++ ] != 0 ) ; } static int binary ( int [ ] arr , int v ) { int index = lookup_table [ 0 ] - 1 ; int co = 0 ; while ( lookup_table [ co ] != 0 ) { if ( v == arr [ index ] ) return index ; else if ( v < arr [ index ] ) { index -= lookup_table [ ++ co ] ; return index ; } else { index += lookup_table [ ++ co ] ; return index ; } } return index ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . GetLength ( 0 ) ; create_table ( n ) ; Console . WriteLine ( " Position ▁ of ▁ 3 ▁ in ▁ array ▁ = ▁ " + binary ( arr , 3 ) ) ; } }
Find the smallest number X such that X ! contains at least Y trailing zeros . | C # implementation of the approach ; Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code
class GFG { static int countFactor ( int P , int X ) { if ( X < P ) return 0 ; return ( X / P + countFactor ( P , X / P ) ) ; } static int findSmallestX ( int Y ) { int low = 0 , high = 5 * Y ; int N = 0 ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( countFactor ( 5 , mid ) < Y ) { low = mid + 1 ; } else { N = mid ; high = mid - 1 ; } } return N ; } static void Main ( ) { int Y = 10 ; System . Console . WriteLine ( findSmallestX ( Y ) ) ; } }
Find maximum N such that the sum of square of first N natural numbers is not more than X | C # implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Driver code
using System ; class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long low = 1 , high = 100000 ; int N = 0 ; while ( low <= high ) { long mid = ( high + low ) / 2 ; if ( squareSum ( mid ) <= X ) { N = ( int ) mid ; low = mid + 1 ; } else high = mid - 1 ; } return N ; } static public void Main ( ) { long X = 25 ; Console . WriteLine ( findMaxN ( X ) ) ; } }