codeparrot/xlcost-text-to-code huggingface dataset:
Longest set of Palindrome Numbers from the range [ L , R ] with at most K difference between its maximum and minimum | C # program for the above approach ; Function to find the maximum size of group of palindrome numbers having difference between maximum and minimum element at most K ; Stores the all the palindromic numbers in the range [ L , R ] ; Traverse over the range [ L , R ] ; If i is a palindrome ; Append the number in the list ; Stores count of maximum palindromic numbers ; Iterate each element in the list ; Calculate rightmost index in the list < current element + K ; Check if there is rightmost index from the current index ; Return the count ; Function to search the rightmost index of given number ; Store the rightmost index ; Calculate the mid ; If given number <= num ; Assign ans = mid ; Update low ; Update high ; return ans ; Function to check if the given number is palindrome or not ; Generate reverse of the given number ; If n is a palindrome ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countNumbers ( int L , int R , int K ) { List < int > list = new List < int > ( ) ; for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) ) { list . Add ( i ) ; } } int count = 0 ; for ( int i = 0 ; i < list . Count ; i ++ ) { int right_index = search ( list , list [ i ] + K - 1 ) ; if ( right_index != - 1 ) count = Math . Max ( count , right_index - i + 1 ) ; } return count ; } static int search ( List < int > list , int num ) { int low = 0 , high = list . Count - 1 ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( list [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } static bool isPalindrome ( int n ) { int rev = 0 ; int temp = n ; while ( n > 0 ) { rev = rev * 10 + n % 10 ; n /= 10 ; } return rev == temp ; } public static void Main ( string [ ] args ) { int L = 98 , R = 112 ; int K = 13 ; Console . WriteLine ( countNumbers ( L , R , K ) ) ; } }
Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array | C # Program to implement the above approach ; Function to find the maximum sum by subtracting same value from all elements of a Subarray ; Stores previous smaller element ; Stores next smaller element ; Calculate contribution of each element ; Return answer ; Function to generate previous smaller element for each array element ; The first element has no previous smaller ; Stack to keep track of elements that have occurred previously ; Push the first index ; Pop all the elements until the previous element is smaller than current element ; Store the previous smaller element ; Push the index of the current element ; Return the array ; Function to generate next smaller element for each array element ; Stack to keep track of elements that have occurring next ; Iterate in reverse order for calculating next smaller ; Pop all the elements until the next element is smaller than current element ; Store the next smaller element ; Push the index of the current element ; Return the array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int findMaximumSum ( int [ ] a , int n ) { int [ ] prev_smaller = findPrevious ( a , n ) ; int [ ] next_smaller = findNext ( a , n ) ; int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_value = Math . Max ( max_value , a [ i ] * ( next_smaller [ i ] - prev_smaller [ i ] - 1 ) ) ; } return max_value ; } public static int [ ] findPrevious ( int [ ] a , int n ) { int [ ] ps = new int [ n ] ; ps [ 0 ] = - 1 ; Stack < int > stack = new Stack < int > ( ) ; stack . Push ( 0 ) ; for ( int i = 1 ; i < a . Length ; i ++ ) { while ( stack . Count > 0 && a [ stack . Peek ( ) ] >= a [ i ] ) stack . Pop ( ) ; ps [ i ] = stack . Count > 0 ? stack . Peek ( ) : - 1 ; stack . Push ( i ) ; } return ps ; } public static int [ ] findNext ( int [ ] a , int n ) { int [ ] ns = new int [ n ] ; ns [ n - 1 ] = n ; Stack < int > stack = new Stack < int > ( ) ; stack . Push ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( stack . Count > 0 && a [ stack . Peek ( ) ] >= a [ i ] ) stack . Pop ( ) ; ns [ i ] = stack . Count > 0 ? stack . Peek ( ) : a . Length ; stack . Push ( i ) ; } return ns ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ ] a = { 80 , 48 , 82 } ; Console . WriteLine ( findMaximumSum ( a , n ) ) ; } }
Check if the given string is shuffled substring of another string | This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver code
static bool compare ( int [ ] arr1 , int [ ] arr2 ) { for ( int i = 0 ; i < 256 ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static bool search ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int [ ] countP = new int [ 256 ] ; int [ ] countTW = new int [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) { countP [ i ] = 0 ; countTW [ i ] = 0 ; } for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) return true ; ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } public static void Main ( ) { string txt = " BACDGABCDA " ; string pat = " ABCD " ; if ( search ( pat , txt ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " NO " ) ; } }
Maximize the median of the given array after adding K elements to the same array | C # implementation of the approach ; Function to return the maximized median ; Sort the array ; If size is even ; If size is odd ; Driver code
using System ; using System . Linq ; class GFG { static double getMaxMedian ( int [ ] arr , int n , int k ) { int size = n + k ; Array . Sort ( arr ) ; if ( size % 2 == 0 ) { double median = ( double ) ( arr [ ( size / 2 ) - 1 ] + arr [ size / 2 ] ) / 2 ; return median ; } double median1 = arr [ size / 2 ] ; return median1 ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 3 , 4 , 2 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( getMaxMedian ( arr , n , k ) ) ; } }
Sort 3 Integers without using if condition or using only max ( ) function | C # program to print three numbers in sorted order using max function ; Find maximum element ; Find minimum element ; Driver code
using System ; class GFG { static void printSorted ( int a , int b , int c ) { int get_max = Math . Max ( a , Math . Max ( b , c ) ) ; int get_min = - Math . Max ( - a , Math . Max ( - b , - c ) ) ; int get_mid = ( a + b + c ) - ( get_max + get_min ) ; Console . Write ( get_min + " ▁ " + get_mid + " ▁ " + get_max ) ; } public static void Main ( ) { int a = 4 , b = 1 , c = 9 ; printSorted ( a , b , c ) ; } }
Binary Insertion Sort | iterative implementation ; Function to sort an array a [ ] of size ' n ' ; find location where selected should be inseretd ; Move all elements after location to create space ; Driver Code
using System ; class GFG { static int binarySearch ( int [ ] a , int item , int low , int high ) { while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( item == a [ mid ] ) return mid + 1 ; else if ( item > a [ mid ] ) low = mid + 1 ; else high = mid - 1 ; } return low ; } static void insertionSort ( int [ ] a , int n ) { int i , loc , j , selected ; for ( i = 1 ; i < n ; ++ i ) { j = i - 1 ; selected = a [ i ] ; loc = binarySearch ( a , selected , 0 , j ) ; while ( j >= loc ) { a [ j + 1 ] = a [ j ] ; j -- ; } a [ j + 1 ] = selected ; } } public static void Main ( String [ ] args ) { int [ ] a = { 37 , 23 , 0 , 17 , 12 , 72 , 31 , 46 , 100 , 88 , 54 } ; int n = a . Length , i ; insertionSort ( a , n ) ; Console . WriteLine ( " Sorted ▁ array : " ) ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Insertion Sort | C # program for implementation of Insertion Sort ; Function to sort array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print array of size n ; Driver Code
using System ; class InsertionSort { void sort ( int [ ] arr ) { int n = arr . Length ; for ( int i = 1 ; i < n ; ++ i ) { int key = arr [ i ] ; int j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 } ; InsertionSort ob = new InsertionSort ( ) ; ob . sort ( arr ) ; printArray ( arr ) ; } }
Count of distinct permutation of a String obtained by swapping only unequal characters | Importing Dictionary class ; Function to calculate total number of valid permutations ; Creating count which is equal to the Total number of characters present and ans that will store the number of unique permutations ; Storing frequency of each character present in the string ; Adding count of characters by excluding characters equal to current char ; Reduce the frequency of the current character and count by 1 , so that it cannot interfere with the calculations of the same elements present to the right of it . ; Return ans + 1 ( Because the given string is also a unique permutation )
using System ; using System . Collections . Generic ; public class GFG { static int validPermutations ( String str ) { Dictionary < char , int > m = new Dictionary < char , int > ( ) ; int count = str . Length , ans = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( m . ContainsKey ( str [ i ] ) ) m [ str [ i ] ] = m [ str [ i ] ] + 1 ; else m . Add ( str [ i ] , 1 ) ; } for ( int i = 0 ; i < str . Length ; i ++ ) { ans += count - m [ str [ i ] ] ; if ( m . ContainsKey ( str [ i ] ) ) m [ str [ i ] ] = m [ str [ i ] ] - 1 ; count -- ; } return ans + 1 ; } public static void Main ( String [ ] args ) { String str = " sstt " ; Console . WriteLine ( validPermutations ( str ) ) ; } }
Counts paths from a point to reach Origin | C # program to count total number of paths from a point to origin ; DP based function to count number of paths ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code
using System ; public class GFG { static int countPaths ( int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 , i ] = 1 ; 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 ] ; return dp [ n , m ] ; } public static void Main ( ) { int n = 3 , m = 2 ; Console . WriteLine ( " ▁ Number ▁ of " + " ▁ Paths ▁ " + countPaths ( n , m ) ) ; } }
Check if two strings are same ignoring their cases | Function to compare two Strings ignoring their cases ; Convert to lowercase using toUpperCase function ; Comparing both using inbuilt function ; if Strings are equal , return true otherwise false ; Function to print the same or not same if Strings are equal or not equal ; Driver Code
using System ; public class GFG { static bool equalIgnoreCase ( String str1 , String str2 ) { str1 = str1 . ToUpper ( ) ; str2 = str2 . ToUpper ( ) ; int x = str1 . CompareTo ( str2 ) ; if ( x != 0 ) { return false ; } else { return true ; } } static void equalIgnoreCaseUtil ( String str1 , String str2 ) { bool res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) { Console . WriteLine ( " Same " ) ; } else { Console . WriteLine ( " Not ▁ Same " ) ; } } public static void Main ( ) { String str1 , str2 ; str1 = " Geeks " ; str2 = " geeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " Geek " ; str2 = " geeksforgeeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; } }
Replace every consonant sequence with its length in the given string | c # implementation of the approach ; Function to return the converted string after replacing every consonant sequence with its length ; To store the resultant string ; Checking each character for consonant sequence ; Count the length of consonants sequence ; Add the length in the string ; Add the vowel ; Check for the last consonant sequence in the string ; Return the resultant string ; Driver code
using System ; public class GFG { public static string replaceConsonants ( string str ) { string res = " " ; int i = 0 , count = 0 ; while ( i < str . Length ) { if ( str [ i ] != ' a ' && str [ i ] != ' e ' && str [ i ] != ' i ' && str [ i ] != ' o ' && str [ i ] != ' u ' ) { i ++ ; count ++ ; } else { if ( count > 0 ) { res += count ; } res += str [ i ] ; i ++ ; count = 0 ; } } if ( count > 0 ) { res += count ; } return res ; } public static void Main ( string [ ] args ) { string str = " abcdeiop " ; Console . WriteLine ( replaceConsonants ( str ) ) ; } }
Encrypt string with product of number of vowels and consonants in substring of size k | C # Program to Encrypt String with product of number of vowels and consonants in every substring of size k ; isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the dtring ; for each substring ; substring of size k ; counting number of vowels and consonants ; append product to answer . ; Driven Program
using System ; public class GFG { static bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static String encryptString ( String s , int n , int k ) { int countVowels = 0 ; int countConsonants = 0 ; String ans = " " ; for ( int l = 0 ; l <= n - k ; l ++ ) { countVowels = 0 ; countConsonants = 0 ; for ( int r = l ; r <= l + k - 1 ; r ++ ) { if ( isVowel ( s [ r ] ) == true ) { countVowels ++ ; } else { countConsonants ++ ; } } ans += Convert . ToString ( countVowels * countConsonants ) ; } return ans ; } static public void Main ( ) { String s = " hello " ; int n = s . Length ; int k = 2 ; Console . Write ( encryptString ( s , n , k ) ) ; } }
String containing first letter of every word in a given string with spaces | C # implementation of above approach ; we are splitting the input based on spaces ( s ) + : this regular expression will handle scenarios where we have words separated by multiple spaces ; charAt ( 0 ) will pick only the first character from the string and append to buffer ; Driver code
using System ; using System . Text ; class GFG { private static StringBuilder charBuffer = new StringBuilder ( ) ; public static String processWords ( String input ) { String [ ] s = input . Split ( ' ▁ ' ) ; foreach ( String values in s ) { charBuffer . Append ( values [ 0 ] ) ; } return charBuffer . ToString ( ) ; } public static void Main ( ) { String input = " geeks ▁ for ▁ geeks " ; Console . WriteLine ( processWords ( input ) ) ; } }
Generate all binary strings without consecutive 1 's | C # program to Generate all binary string without consecutive 1 's of size K ; Array conversion to String -- ; Base Condition when we reached at the end of Array * * ; Printing the Generated String * * Return to the previous case * ; If the first Character is Zero then adding * * ; If the Character is One then add Zero to next * * ; Calling Recursively for the next value of Array ; Initializing first character to Zero ; Generating Strings starting with Zero -- ; Initialized first Character to one -- ; Driver code ; Calling function fun with argument k
using System ; class GFG { static string toString ( char [ ] a ) { string String = new string ( a ) ; return String ; } static void generate ( int k , char [ ] ch , int n ) { if ( n == k ) { Console . Write ( toString ( ch ) + " ▁ " ) ; return ; } if ( ch [ n - 1 ] == '0' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; ch [ n ] = '1' ; generate ( k , ch , n + 1 ) ; } if ( ch [ n - 1 ] == '1' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; } } static void fun ( int k ) { if ( k <= 0 ) { return ; } char [ ] ch = new char [ k ] ; ch [ 0 ] = '0' ; generate ( k , ch , 1 ) ; ch [ 0 ] = '1' ; generate ( k , ch , 1 ) ; } static void Main ( ) { int k = 3 ; fun ( k ) ; } }
Largest right circular cylinder within a cube | C # Program to find the biggest right circular cylinder that can be fit within a cube ; Function to find the biggest right circular cylinder ; side cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code
using System ; class GFG { static float findVolume ( float a ) { if ( a < 0 ) return - 1 ; float r = a / 2 ; float h = a ; float V = ( float ) ( 3.14 * Math . Pow ( r , 2 ) * h ) ; return V ; } public static void Main ( ) { float a = 5 ; Console . WriteLine ( findVolume ( a ) ) ; } }
Program for volume of Pyramid | C # Program for volume of Pyramid . ; Function to find the volume of triangular pyramid ; Function to find the volume of square pyramid ; Function to find the volume of pentagonal pyramid ; Function to find the volume of hexagonal pyramid ; Driver Code
using System ; class GFG { public static float volumeTriangular ( int a , int b , int h ) { float vol = ( float ) ( 0.1666 ) * a * b * h ; return vol ; } public static float volumeSquare ( int b , int h ) { float vol = ( float ) ( 0.33 ) * b * b * h ; return vol ; } public static float volumePentagonal ( int a , int b , int h ) { float vol = ( float ) ( 0.83 ) * a * b * h ; return vol ; } public static float volumeHexagonal ( int a , int b , int h ) { float vol = ( float ) a * b * h ; return vol ; } public static void Main ( ) { int b = 4 , h = 9 , a = 4 ; Console . WriteLine ( " Volume ▁ of ▁ triangular " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeTriangular ( a , b , h ) ) ; Console . WriteLine ( " Volume ▁ of ▁ square ▁ " + " base ▁ pyramid ▁ is ▁ " + volumeSquare ( b , h ) ) ; Console . WriteLine ( " Volume ▁ of ▁ pentagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumePentagonal ( a , b , h ) ) ; Console . WriteLine ( " Volume ▁ of ▁ Hexagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeHexagonal ( a , b , h ) ) ; } }
Program to find area of a Trapezoid | C # program to calculate area of a trapezoid ; Function for the area ; Driver Code
using System ; class GFG { static double Area ( int b1 , int b2 , int h ) { return ( ( b1 + b2 ) / 2 ) * h ; } public static void Main ( ) { int base1 = 8 , base2 = 10 , height = 6 ; double area = Area ( base1 , base2 , height ) ; Console . WriteLine ( " Area ▁ is : ▁ " + area ) ; } }
Find number of diagonals in n sided convex polygon | C # function to find number of diagonals in n sided convex polygon ; driver code to test above function
using System ; class GFG { static int numberOfDiagonals ( int n ) { return n * ( n - 3 ) / 2 ; } public static void Main ( ) { int n = 5 ; Console . Write ( n + " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ) ; Console . WriteLine ( numberOfDiagonals ( n ) + " ▁ diagonals " ) ; } }
Area of the largest Rectangle without a given point | C # implementation to find area of largest Rectangle without hole within a given Rectangle ; Function to find the maximum area such that it does not contains any hole ; Area for all the possible positions of the cut ; Find the maximum area among the above rectangles ; Driver Code ; Function call
using System ; class GFG { static void maximumArea ( int l , int b , int x , int y ) { int left , right , above , below ; left = x * b ; right = ( l - x - 1 ) * b ; above = l * y ; below = ( b - y - 1 ) * l ; Console . Write ( Math . Max ( Math . Max ( left , right ) , Math . Max ( above , below ) ) ) ; } public static void Main ( String [ ] args ) { int L = 8 , B = 8 ; int X = 0 , Y = 0 ; maximumArea ( L , B , X , Y ) ; } }
Minimize cost of removals required to make all remaining characters of the string unique | C # program for the above approach ; Function to find the minimum cost of removing characters to make the string unique ; Store the minimum cost required ; Create a dictionary to store the maximum cost of removal a character ; Create a dictionary to store the total deletion cost of a character ; Traverse the string , S ; Keep track of maximum cost of each character ; Update the maximum deletion cost ; Keep track of the total cost of each character ; Update the total deletion cost ; Traverse through all the unique characters ; Keep the maximum cost character and delete the rest ; Return the answer ; Driver code ; Given string ; Given cost array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int delCost ( string s , int [ ] cost ) { int ans = 0 ; Dictionary < int , int > forMax = new Dictionary < int , int > ( ) ; Dictionary < int , int > forTot = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! forMax . ContainsKey ( s [ i ] ) ) { forMax [ s [ i ] ] = cost [ i ] ; } else { forMax [ s [ i ] ] = Math . Max ( cost [ i ] , forMax [ s [ i ] ] ) ; } if ( ! forTot . ContainsKey ( s [ i ] ) ) { forTot [ s [ i ] ] = cost [ i ] ; } else { forTot [ s [ i ] ] += cost [ i ] ; } } foreach ( KeyValuePair < int , int > i in forMax ) { ans += forTot [ i . Key ] - i . Value ; } return ans ; } static void Main ( ) { string s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; Console . WriteLine ( delCost ( s , cost ) ) ; } }
Smallest divisor of N closest to X | C # program for the above approach ; Define macros ; Stores divisors for all numbers in the vector divisors ; i is the divisor and j is the multiple ; Function to compare the closeness of the given target ; Function to find the element closest to target in divisors vector ; Corner cases ; Perform binary search ; Check if target is less than the array element then search in left half ; Check if target is greater than previous to mid , return closest of two ; Repeat for left half ; Check if target is greater than mid ; Update i ; Only single element left after search ; Function to print the divisor of N closest to X ; Function call to calculate and stores divisors of all numbers in a vector ; Stores the closest value to target ; Print the answer ; Driver Code ; Given N & X ; Function Call
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 10000 ; static List < int > [ ] divisors = new List < int > [ MAX + 1 ] ; static void computeDivisors ( ) { for ( int i = 1 ; i <= MAX ; i ++ ) { for ( int j = i ; j <= MAX ; j += i ) { divisors [ j ] . Add ( i ) ; } } } static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } static int findClosest ( List < int > array , int n , int target ) { int [ ] arr = array . ToArray ( ) ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } static void printClosest ( int N , int X ) { computeDivisors ( ) ; int ans = findClosest ( divisors [ N ] , divisors [ N ] . Count , X ) ; Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int N = 16 , X = 5 ; for ( int i = 0 ; i < divisors . Length ; i ++ ) divisors [ i ] = new List < int > ( ) ; printClosest ( N , X ) ; } }