"Smallest number S such that N is a factor of S factorial or S ! | C # Program to find factorial that N belongs to ; Calculate prime factors for a given number ; Container for prime factors ; Iterate from 2 to i ^ 2 finding all factors ; If we still have a remainder it is also a prime factor ; Calculate occurrence of an element in factorial of a number ; Iterate through prime factors ; Check if factorial contains less occurrences of prime factor ; Method to binary search 1 to N ; Prime factors are not in the factorial Increase the lowerbound ; We have reached smallest occurrence ; Smaller factorial satisfying requirements may exist , decrease upperbound ; Calculate prime factors and search ; Driver method"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class Test { static Dictionary < long , int > primeFactors ( long num ) { Dictionary < long , int > ans = new Dictionary < long , int > ( ) ; for ( long i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { num /= i ; if ( ! ans . ContainsKey ( i ) ) { ans [ i ] = 0 ; } ans [ i ] ++ ; } } if ( num > 1 ) { if ( ! ans . ContainsKey ( num ) ) { ans [ num ] = 0 ; } ans [ num ] ++ ; } return ans ; } static long legendre ( long factor , long num ) { long count = 0 , fac2 = factor ; while ( num >= factor ) { count += num / factor ; factor *= fac2 ; } return count ; } static bool possible ( Dictionary < long , int > factors , long num ) { foreach ( int itr in factors . Keys ) { if ( legendre ( itr , num ) < factors [ itr ] ) return false ; } return true ; } static long search ( long start , long end , Dictionary < long , int > factors ) { long mid = ( start + end ) / 2 ; if ( ! possible ( factors , mid ) ) return search ( mid + 1 , end , factors ) ; if ( start == mid ) return mid ; return search ( start , mid , factors ) ; } static long findFact ( long num ) { Dictionary < long , int > factors = primeFactors ( num ) ; return search ( 1 , num , factors ) ; } public static void Main ( ) { Console . WriteLine ( findFact ( 6 ) ) ; Console . WriteLine ( findFact ( 997587429953L ) ) ; } }


"Smallest number dividing minimum number of elements in the Array | C # implementation of the approach ; Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code"
Solution in C#:

using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . Max ( m , arr [ i ] ) ; int [ ] cnt = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else { cnt [ j ] ++ ; cnt [ arr [ i ] / j ] ++ ; } } } } for ( int i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 12 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }


"Smallest number dividing minimum number of elements in the array | Set 2 | C # implementation of the approach ; Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code"
Solution in C#:

using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . Max ( m , arr [ i ] ) ; int [ ] freq = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( int i = 1 ; i <= m + 1 ; i ++ ) { int j = i ; int cnt = 0 ; while ( j <= m ) { cnt += freq [ j ] ; j += i ; } if ( cnt == 0 ) return i ; } return m + 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 12 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }


"Smallest number divisible by first n numbers | C # program to find smallest number evenly divisible by all numbers 1 to n ; Function for gcd ; Function returns the lcm of first n numbers ; Driver program to test the above function"
Solution in C#:

using System ; public class GFG { static long gcd ( long a , long b ) { if ( a % b != 0 ) return gcd ( b , a % b ) ; else return b ; } static long lcm ( long n ) { long ans = 1 ; for ( long i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( gcd ( ans , i ) ) ; return ans ; } static public void Main ( ) { long n = 20 ; Console . WriteLine ( lcm ( n ) ) ; } }


"Smallest number greater or equals to N such that it has no odd positioned bit set | C # implementation of the approach ; Function to count the total bits ; Iterate and find the number of set bits ; Right shift the number by 1 ; Function to find the nearest number ; Count the total number of bits ; To get the position ; If the last set bit is at odd position then answer will always be a number with the left bit set ; Set all the even bits which are possible ; If the number still is less than N ; Return the number by setting the next even set bit ; If we have reached this position it means tempsum > n hence turn off even bits to get the first possible number ; Turn off the bit ; If it gets lower than N then set it and return that number ; Driver code"
Solution in C#:

using System ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } static int findNearestNumber ( int n ) { int cnt = countBits ( n ) ; cnt -= 1 ; if ( cnt % 2 == 1 ) { return 1 << ( cnt + 1 ) ; } else { int tempnum = 0 ; for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum += 1 << i ; } if ( tempnum < n ) { return ( 1 << ( cnt + 2 ) ) ; } else if ( tempnum == n ) { return n ; } for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum -= ( 1 << i ) ; if ( tempnum < n ) { return tempnum += ( 1 << i ) ; } } } return int . MinValue ; } public static void Main ( ) { int n = 19 ; Console . WriteLine ( findNearestNumber ( n ) ) ; } }


"Smallest number greater than X which is K | C # program to find the smallest K periodic integer greater than X ; Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary String ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the readonly String ; Driver Code"
Solution in C#:

using System ; class GFG { static String Kperiodicinteger ( char [ ] X , int N , int K ) { String temp = String . Join ( " " , X ) ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( String . Join ( " " , X ) . CompareTo ( temp ) >= 0 ) { return String . Join ( " " , X ) ; } int POS = 0 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return String . Join ( " " , X ) ; } public static void Main ( String [ ] args ) { int N = 4 , K = 2 ; String X = "1215" ; Console . Write ( Kperiodicinteger ( X . ToCharArray ( ) , N , K ) ) ; } }


"Smallest number greater than n that can be represented as a sum of distinct power of k | C # implementation of the above approach ; Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; List P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code"
Solution in C#:

 using System ; class GFG { static void greaterK ( int n , int k ) { int [ ] p = new int [ String . Join ( " " , n ) . Length + 2 ] ; int index = 0 ; int x = n ; int j ; while ( x > 0 ) { p [ index ] = ( int ) ( x % k ) ; x /= k ; index ++ ; } int idx = 0 ; for ( int i = 0 ; i < p . Length - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } j = p . Length - 1 ; if ( p [ j ] >= 2 ) { p [ index ] = 1 ; index ++ ; } int ans = 0 ; for ( int i = p . Length - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int n = 29 , k = 7 ; greaterK ( n , k ) ; } }


"Smallest number greater than or equal to N divisible by K | C # implementation of the above approach ; Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code"
Solution in C#:

public class GFG { static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } static void Main ( ) { int N = 45 , K = 6 ; System . Console . WriteLine ( " Smallest ▁ number ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " + K + " ▁ is ▁ " + findNum ( N , K ) ) ; } }


"Smallest number greater than or equal to N using only digits 1 to K | C # program to find the smallest number greater than or equal to N which is made up of first K digits ; Function to count the digits greater than K ; Function to print the list ; Function to find the number greater than or equal to n , which is only made of first k digits ; If the number itself satisfy the conditions ; Check digit from back ; If digit > K is present previously and current digit is less than K ; If current digit is greater than K ; If an extra digit needs to be added ; Print the number ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int CountGreater ( int n , int k ) { int a = 0 ; while ( n > 0 ) { if ( ( n % 10 ) > k ) { a ++ ; } n = n / 10 ; } return a ; } static void PrintList ( List < int > ans ) { foreach ( int it in ans ) Console . Write ( it ) ; } static void getNumber ( int n , int k ) { int count = CountGreater ( n , k ) ; if ( count == 0 ) { Console . Write ( n ) ; return ; } List < int > ans = new List < int > ( ) ; bool changed = false ; while ( n > 0 ) { int digit = n % 10 ; if ( changed == true ) { ans . Insert ( 0 , digit ) ; } else { if ( count == 0 && digit < k ) { ans . Insert ( 0 , digit + 1 ) ; changed = true ; } else { ans . Insert ( 0 , 1 ) ; if ( digit > k ) { count -- ; } } } n = n / 10 ; } if ( changed == false ) { ans . Insert ( 0 , 1 ) ; } PrintList ( ans ) ; return ; } public static void Main ( String [ ] args ) { int N = 51234 ; int K = 4 ; getNumber ( N , K ) ; } }


"Smallest number greater than or equal to N which is divisible by its non | C # program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { int possible = 1 ; int temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { Console . Write ( i ) ; return ; } } } public static void Main ( String [ ] args ) { int N = 31 ; findSmallestNumber ( N ) ; } }


"Smallest number k such that the product of digits of k is equal to n | C # implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static long smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) { return n ; } Stack < int > digits = new Stack < int > ( ) ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . Push ( i ) ; n = n / i ; } } if ( n != 1 ) { return - 1 ; } long k = 0 ; while ( digits . Count != 0 ) { k = k * 10 + digits . Peek ( ) ; digits . Pop ( ) ; } return k ; } static public void Main ( ) { int n = 100 ; Console . Write ( smallestNumber ( n ) ) ; } }


"Smallest number not less than N which is divisible by all digits of N | C # program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int LCM ( int A , int B ) { return ( A * B / __gcd ( A , B ) ) ; } static void findSmallestNumber ( int X ) { int lcm = 1 ; int temp = X ; while ( temp > 0 ) { int last = temp % 10 ; temp /= 10 ; if ( last == 0 ) continue ; lcm = LCM ( lcm , last ) ; } int answer = ( ( X + lcm - 1 ) / lcm ) * lcm ; Console . Write ( answer ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int X = 280 ; findSmallestNumber ( X ) ; } }


"Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | C # program for the above approach ; Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code"
Solution in C#:

using System ; class GFG { static int minPossibleValue ( int N , int K , int X ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } public static void Main ( ) { int N = 7 , K = 4 , X = 1 ; Console . WriteLine ( minPossibleValue ( N , K , X ) ) ; } }


"Smallest number possible by swapping adjacent even odd pairs | C # program to find the smallest number possible by swapping adjacent digits of different parity ; Function to return the smallest number possible ; Arrays to store odd and even digits in the order their appearance in the given String ; Insert the odd and even digits ; Pointer to odd digit ; Pointer to even digit ; In case number of even and odd digits are not equal If odd digits are remaining ; If even digits are remaining ; Removal of leading 0 's ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String findAns ( String s ) { int digit ; List < int > odd = new List < int > ( ) ; List < int > even = new List < int > ( ) ; foreach ( char c in s . ToCharArray ( ) ) { digit = c - '0' ; if ( digit % 2 == 1 ) odd . Add ( digit ) ; else even . Add ( digit ) ; } int i = 0 ; int j = 0 ; String ans = " " ; while ( i < odd . Count && j < even . Count ) { if ( odd [ i ] < even [ j ] ) ans += ( char ) ( odd [ i ++ ] + '0' ) ; else ans += ( char ) ( even [ j ++ ] + '0' ) ; } while ( i < odd . Count ) ans += ( char ) ( odd [ i ++ ] + '0' ) ; while ( j < even . Count ) ans += ( char ) ( even [ j ++ ] + '0' ) ; while ( ans [ 0 ] == '0' ) { ans = ans . Substring ( 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = "894687536" ; Console . Write ( findAns ( s ) ) ; } }


"Smallest number required to be added to M to make it divisible by N | C # program for the above approach ; Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { public static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static int findSmallest ( int M , int N ) { int x = findNum ( M , N ) ; return x - M ; } public static void Main ( ) { int M = 100 , N = 28 ; Console . WriteLine ( findSmallest ( M , N ) ) ; } }


"Smallest number that can replace all | C # program for the above approach ; Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { public static void findMissingValue ( int [ ] arr , int N ) { int minE = Int32 . MaxValue , maxE = Int32 . MinValue ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . Min ( minE , arr [ i + 1 ] ) ; maxE = Math . Max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . Min ( minE , arr [ i ] ) ; maxE = Math . Max ( maxE , arr [ i ] ) ; } } if ( minE == Int32 . MaxValue && maxE == Int32 . MinValue ) { Console . WriteLine ( "0" ) ; } else { Console . WriteLine ( ( minE + maxE ) / 2 ) ; } } public static void Main ( ) { int [ ] arr = { 1 , - 1 , - 1 , - 1 , 5 } ; int N = arr . Length ; findMissingValue ( arr , N ) ; } }


"Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | C # program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int moduloEquality ( int [ ] A , int [ ] B , int n , int m ) { Dictionary < int , int > mapA = new Dictionary < int , int > ( ) ; Dictionary < int , int > mapB = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . ContainsKey ( A [ i ] ) ) { mapA [ A [ i ] ] = mapA [ A [ i ] ] + 1 ; } else { mapA . Add ( A [ i ] , 1 ) ; } if ( mapB . ContainsKey ( B [ i ] ) ) { mapB [ B [ i ] ] = mapB [ B [ i ] ] + 1 ; } else { mapB . Add ( B [ i ] , 1 ) ; } } HashSet < int > possibleValues = new HashSet < int > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . Add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Int32 . MaxValue ; foreach ( int it in possibleValues ) { bool possible = true ; foreach ( KeyValuePair < int , int > it2 in mapA ) { if ( it2 . Value != mapB [ ( it2 . Key + it ) % m ] ) { possible = false ; break ; } } if ( possible ) { ans = Math . Min ( ans , it ) ; } } return ans ; } static void Main ( ) { int n = 4 ; int m = 3 ; int [ ] A = { 0 , 0 , 2 , 1 } ; int [ ] B = { 2 , 0 , 1 , 1 } ; Console . WriteLine ( moduloEquality ( A , B , n , m ) ) ; } }


"Smallest number to be subtracted to convert given number to a palindrome | C # program for the above approach ; Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static void minSub ( int N ) { int count = 0 ; while ( N >= 0 ) { int num = N ; int rev = 0 ; while ( num != 0 ) { int digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = num / 10 ; } if ( N == rev ) { break ; } count ++ ; N -- ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int N = 3456 ; minSub ( N ) ; } }


"Smallest number to make Array sum at most K by dividing each element | C # program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinDivisor ( int [ ] arr , int n , int limit ) { int low = 0 , high = 1000000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) Math . Ceiling ( ( double ) arr [ i ] / ( double ) mid ) ; } if ( sum <= limit ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 9 } ; int N = arr . Length ; int K = 6 ; Console . WriteLine ( findMinDivisor ( arr , N , K ) ) ; } }


"Smallest number to multiply to convert floating point to natural | C # program to find the smallest number to multiply to convert a floating point number into natural number . ; Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findnum ( String str ) { int n = str . Length ; int count_after_dot = 0 ; bool dot_seen = false ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != ' . ' ) { num = num * 10 + ( str [ i ] - '0' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; int dem = ( int ) Math . Pow ( 10 , count_after_dot ) ; return ( dem / gcd ( num , dem ) ) ; } public static void Main ( ) { String str = "5.125" ; Console . Write ( findnum ( str ) ) ; } }


"Smallest number which is not coprime with any element of an array | C # program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to calculate GCD of two numbers ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Prlong the answer ; Driver Code ; Given array ; Stores the size of the array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long MAX = 50 ; static bool isPrime ( long n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void findPrime ( List < long > primes ) { for ( long i = 2 ; i <= MAX ; i ++ ) { if ( isPrime ( i ) ) primes . Add ( i ) ; } } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void findMinimumNumber ( long [ ] arr , long N ) { List < long > primes = new List < long > ( ) ; findPrime ( primes ) ; long ans = 2147483647 ; int n = primes . Count ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { long temp = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { temp *= primes [ j ] ; } } bool check = true ; for ( long k = 0 ; k < N ; k ++ ) { if ( gcd ( temp , arr [ k ] ) == 1 ) { check = false ; break ; } } if ( check == true ) ans = Math . Min ( ans , temp ) ; } Console . Write ( ans ) ; } public static void Main ( ) { long [ ] arr = { 3 , 4 , 6 , 7 , 8 , 9 , 10 } ; long N = arr . Length ; findMinimumNumber ( arr , N ) ; } }


"Smallest number whose product with N has sum of digits equal to that of N | C # program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Globalization ; class GFG { static void find_num ( string n ) { int ans = 0 ; char [ ] digitsOfN = n . ToCharArray ( ) ; int sumOfDigitsN = 0 ; foreach ( char c in digitsOfN ) { sumOfDigitsN += Int32 . Parse ( Char . ToString ( c ) ) ; } for ( int x = 11 ; x > 0 ; x ++ ) { int newNum = x * Int32 . Parse ( n ) ; int tempSumDigits = 0 ; string str = newNum . ToString ( ) ; char [ ] temp = str . ToCharArray ( ) ; foreach ( char c in temp ) { tempSumDigits += Int32 . Parse ( Char . ToString ( c ) ) ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { string N = "3029" ; find_num ( N ) ; } }


"Smallest number whose set bits are maximum in a given range | C # program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Driver code"
Solution in C#:

using System ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } static public void Main ( ) { int l = 1 ; int r = 5 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; } }


"Smallest number whose set bits are maximum in a given range | C # program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code"
Solution in C#:

using System ; class gfg { static int countMaxSetBits ( int left , int right ) { int max_count = - 1 , num = 0 ; for ( int i = left ; i <= right ; ++ i ) { int temp = i , cnt = 0 ; while ( temp > 0 ) { if ( temp % 2 == 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } public static void Main ( String [ ] args ) { int l = 1 , r = 5 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; Console . Write ( countMaxSetBits ( l , r ) ) ; } }


"Smallest number whose square has N digits | C # program for above approach ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver code"
Solution in C#:

using System ; class GFG { static int smallestNum ( int N ) { float x = ( float ) ( Math . Pow ( 10 , ( N - 1 ) / 2.0 ) ) ; return ( int ) ( Math . Ceiling ( x ) ) ; } public static void Main ( ) { int N = 4 ; Console . Write ( smallestNum ( N ) ) ; } }


"Smallest number whose sum of digits is N and every digit occurring at most K times | C # program to implement the above approach ; Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void findSmallestNumberPossible ( int N , int K ) { if ( N > 45 * K ) { Console . Write ( " - 1" ) ; return ; } string res = " " ; int count = 0 ; for ( int i = 9 ; i >= 1 ; ) { if ( count == K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res += ( char ) ( '0' + i ) ; } else { res += ( char ) ( '0' + N ) ; N = 0 ; break ; } count ++ ; } string ans = " " ; for ( int i = res . Length - 1 ; i >= 0 ; i -- ) { ans += res [ i ] + " " ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ; } }


"Smallest number whose sum of digits is square of N | C # implementation of the above approach ; Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestNum ( int n ) { return ( int ) ( ( n * n % 9 + 1 ) * Math . Pow ( 10 , n * n / 9 ) - 1 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( smallestNum ( n ) ) ; } }


"Smallest number with given sum of digits and sum of square of digits | C # program to find the Smallest number with given sum of digits and sum of square of digits ; Top down dp to find minimum number of digits with given sum of dits a and sum of square of digits as b ; Invalid condition ; Number of digits satisfied ; Memoization ; Initialize ans as maximum as we have to find the minimum number of digits ; Check for all possible combinations of digits ; recurrence call ; If the combination of digits cannot give sum as a and sum of square of digits as b ; Returns the minimum number of digits ; Function to print the digits that gives sum as a and sum of square of digits as b ; initialize the dp array as - 1 ; base condition ; function call to get the minimum number of digits ; When there does not exists any number ; Printing the digits from the most significant digit ; Trying all combinations ; checking conditions for minimum digits ; Driver Code ; Function call to print the smallest number"
Solution in C#:

using System ; public class GFG { static int [ , ] dp = new int [ 901 , 8101 ] ; static int minimumNumberOfDigits ( int a , int b ) { if ( a > b a < 0 b < 0 a > 900 b > 8100 ) { return - 1 ; } if ( a == 0 && b == 0 ) { return 0 ; } if ( dp [ a , b ] != - 1 ) { return dp [ a , b ] ; } int ans = 101 ; for ( int i = 9 ; i >= 1 ; i -- ) { int k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) ; if ( k != - 1 ) { ans = Math . Min ( ans , k + 1 ) ; } } return dp [ a , b ] = ans ; } static void printSmallestNumber ( int a , int b ) { for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) dp [ i , j ] = - 1 ; dp [ 0 , 0 ] = 0 ; int k = minimumNumberOfDigits ( a , b ) ; if ( k == - 1 k > 100 ) { Console . WriteLine ( " - 1" ) ; } else { while ( a > 0 && b > 0 ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( a >= i && b >= i * i && 1 + dp [ a - i , b - i * i ] == dp [ a , b ] ) { Console . Write ( i ) ; a -= i ; b -= i * i ; break ; } } } } } public static void Main ( ) { int a = 18 , b = 162 ; printSmallestNumber ( a , b ) ; } }


"Smallest number with sum of digits as N and divisible by 10 ^ N | C # program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so as to make it divisible by 10^N ) ; Driver Code"
Solution in C#:

using System ; class GFG { static void digitsNum ( int N ) { if ( N == 0 ) Console . Write ( "0" ) ; if ( N % 9 != 0 ) Console . Write ( ( N % 9 ) ) ; for ( int i = 1 ; i <= ( N / 9 ) ; ++ i ) Console . Write ( "9" ) ; for ( int i = 1 ; i <= N ; ++ i ) Console . Write ( "0" ) ; Console . WriteLine ( " " ) ; } public static void Main ( ) { int N = 5 ; Console . Write ( " The ▁ number ▁ is ▁ : ▁ " ) ; digitsNum ( N ) ; } }


"Smallest occurring element in each subsequence | C # program for the above approach ; Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int N ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M . Add ( i , arr [ i ] ) ; } Array . Sort ( arr ) ; Dictionary < int , int > Count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Count . Add ( arr [ i ] , ( int ) Math . Pow ( 2 , N - i - 1 ) ) ; } foreach ( KeyValuePair < int , int > m in M ) { Console . Write ( Count [ m . Value ] ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 1 } ; int N = arr . Length ; solve ( arr , N ) ; } }


"Smallest odd digits number not less than N | C # program to print the smallest integer not less than N with all odd digits ; function to check if all digits are odd of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 == 0 ) if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code"
Solution in C#:

using System ; class GFG { static int check_digits ( int n ) { while ( n != 0 ) { return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) == 1 ) return i ; } static void Main ( ) { int N = 2397 ; Console . WriteLine ( smallest_number ( N ) ) ; } }


"Smallest odd digits number not less than N | C # program to print the smallest integer not less than N with all odd digits ; function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code"
Solution in C#:

using System ; public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int digit = s [ i ] - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; } num = num * 10 + ( s [ index ] - '0' + 1 ) ; for ( int i = index + 1 ; i < s . Length ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void Main ( ) { int N = 2397 ; Console . WriteLine ( smallestNumber ( N ) ) ; } }


"Smallest odd number with N digits | C # implementation of the approach ; Function to return smallest odd with n digits ; Driver code"
Solution in C#:

using System ; class Solution { static int smallestOdd ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) + 1 ; } public static void Main ( ) { int n = 4 ; Console . Write ( smallestOdd ( n ) ) ; } }


"Smallest odd number with even sum of digits from the given number N | C # program to find the smallest odd number with even sum of digits from the given number N ; Function to find the smallest odd number whose sum of digits is even from the given string ; Converting the given string to a list of digits ; An empty array to store the digits ; For loop to iterate through each digit ; If the given digit is odd then the digit is appended to the array b ; Sorting the list of digits ; If the size of the list is greater than 1 then a 2 digit smallest odd number is returned . Since the sum of two odd digits is always even ; Else , - 1 is returned ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { public static int smallest ( string s ) { int [ ] a = new int [ s . Length ] ; for ( int i = 0 ; i < s . Length ; i ++ ) a [ i ] = ( int ) ( s [ i ] - '0' ) ; ArrayList b = new ArrayList ( ) ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] % 2 != 0 ) b . Add ( a [ i ] ) ; } b . Sort ( ) ; if ( b . Count > 1 ) return ( ( int ) b [ 0 ] * 10 + ( int ) b [ 1 ] ) ; return - 1 ; } public static void Main ( string [ ] args ) { Console . Write ( smallest ( "15470" ) ) ; } }


"Smallest of three integers without comparison operators | C # implementation of above approach ; Function to find minimum of x and y ; Function to find minimum of 3 numbers x , y and z ; Driver code"
Solution in C#:

using System ; class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . Min ( x , Math . Min ( y , z ) ) ; } static void Main ( ) { int x = 12 , y = 15 , z = 5 ; Console . WriteLine ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }


"Smallest of three integers without comparison operators | C # program of above approach ; Using division operator to find minimum of three numbers ; Same as " if ▁ ( y ▁ < ▁ x ) " ; Driver code"
Solution in C#:

using System ; public class GfG { static int smallest ( int x , int y , int z ) { if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ; return ( ( x / z ) != 1 ) ? x : z ; } public static void Main ( ) { int x = 78 , y = 88 , z = 68 ; Console . Write ( " Minimum ▁ of ▁ 3 ▁ numbers " + " ▁ is ▁ { 0 } " , smallest ( x , y , z ) ) ; } }


"Smallest of three integers without comparison operators | C # program to find Smallest of three integers without comparison operators ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallest ( int x , int y , int z ) { int c = 0 ; while ( x != 0 && y != 0 && z != 0 ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } public static void Main ( ) { int x = 12 , y = 15 , z = 5 ; Console . Write ( " Minimum ▁ of ▁ 3" + " ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }


"Smallest pair of indices with product of subarray co | C # program for the above approach ; Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod [ ] ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then print - 1 ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void findPair ( int [ ] A , int N ) { int [ ] right_prod = new int [ N ] ; int flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; int total_prod = right_prod [ 0 ] ; int product ; for ( int i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( int j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; Console . WriteLine ( " ( " + ( i - 1 ) + " , ▁ " + ( j + 1 ) + " ) " ) ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) Console . Write ( - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 3 , 7 } ; int N = arr . Length ; findPair ( arr , N ) ; } }


"Smallest pair of integers with minimum difference whose Bitwise XOR is N | C # program for the above approach ; Function to find the numbers A and B whose Bitwise XOR is N and the difference between them is minimum ; Find the MSB of the N ; Find the value of B ; Find the value of A ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void findAandB ( int N ) { int K = ( int ) ( Math . Log ( N ) / Math . Log ( 2 ) ) ; int B = ( 1 << K ) ; int A = B ^ N ; Console . Write ( A + " ▁ " + B ) ; } public static void Main ( String [ ] args ) { int N = 26 ; findAandB ( N ) ; } }


"Smallest perfect cube in an array | C # implementation of the approach ; Function that returns true if n is a perfect cube ; Takes the sqrt of the number ; Checks if it is a perfect cube number ; Function to return the smallest perfect cube from the array ; Stores the minimum of all the perfect cubes from the array ; Traverse all elements in the array ; Store the minimum if current element is a perfect cube ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkPerfectcube ( int n ) { int d = ( int ) Math . Sqrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int smallestPerfectCube ( int [ ] a , int n ) { int mini = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . Min ( a [ i ] , mini ) ; } } return mini ; } static public void Main ( ) { int [ ] a = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = a . Length ; Console . Write ( smallestPerfectCube ( a , n ) ) ; } }


"Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | C # Code for Smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program"
Solution in C#:

using System ; class GFG { static int perfectPowerOf2 ( int n ) { int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } public static void Main ( ) { int n = 128 ; Console . WriteLine ( " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " + n + " : ▁ " + perfectPowerOf2 ( n ) ) ; } }


"Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | C # program for the above approach ; Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver code ; Given array ; Given size ; Print the required smallest integer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMinKToMakeAllEqual ( int N , int [ ] A ) { var B = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { B . Add ( A [ i ] ) ; } List < int > b = new List < int > ( B ) ; int M = b . Count ; int j = 0 ; if ( M > 3 ) { Console . Write ( " - 1" ) ; } else if ( M == 3 ) { int B_1 = b [ j ++ ] ; int B_2 = b [ j ++ ] ; int B_3 = b [ j ++ ] ; if ( B_2 - B_1 == B_3 - B_2 ) { Console . Write ( B_2 - B_1 ) ; } else { Console . Write ( " - 1" ) ; } } else if ( M == 2 ) { int B_1 = b [ j ++ ] ; int B_2 = b [ j ++ ] ; if ( ( B_2 - B_1 ) % 2 == 0 ) { Console . Write ( ( B_2 - B_1 ) / 2 ) ; } else { Console . Write ( B_2 - B_1 ) ; } } else { Console . Write ( 0 ) ; } } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 3 , 5 , 1 } ; int N = A . Length ; findMinKToMakeAllEqual ( N , A ) ; } }


"Smallest positive integer X satisfying the given equation | C # program to implement the above approach ; Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinSoln ( int n , int k ) { int minSoln = int . MaxValue ; for ( int i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . Min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } public static void Main ( String [ ] args ) { int n = 4 , k = 6 ; Console . WriteLine ( findMinSoln ( n , k ) ) ; } }


"Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | C # program to implement the above approach ; Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int findSmallestInteger ( int [ ] arr , int N , int K ) { int left = 1 ; int right = arr . Max ( ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( arr [ i ] + mid - 1 ) / mid ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 9 } ; int N = arr . Length ; int K = 6 ; Console . Write ( findSmallestInteger ( arr , N , K ) ) ; } }


"Smallest positive integer that does not divide any elements of the given array | C # program for the above approach ; Function to find the smallest number which doesn 't divides any integer in  the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; public class GFG { static void smallestNumber ( int [ ] arr , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = Math . Max ( maxi , arr [ i ] ) ; } int ans = - 1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 2 , 6 , 9 , 2 } ; int N = arr . Length ; smallestNumber ( arr , N ) ; } }


"Smallest positive number made up of non | C # program for the above approach ; Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void result ( int n ) { if ( n > 45 ) { Console . Write ( - 1 ) ; return ; } string res = " " ; int digit = 9 ; while ( n > digit ) { res = ( char ) ( '0' + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = ( char ) ( '0' + n ) + res ; } Console . Write ( res ) ; } public static void Main ( ) { int N = 19 ; result ( N ) ; } }


"Smallest possible integer K such that ceil of each Array element when divided by K is at most M | C # program for the above approach ; Function to check if the sum of ceil values of the arr [ ] for the K value is at most M or not ; Stores the sum of ceil values ; Update the sum ; Return true if sum is less than or equal to M , false otherwise ; Function to find the smallest possible integer K such that ceil ( arr [ 0 ] / K ) + ceil ( arr [ 1 ] / K ) + ... . + ceil ( arr [ N - 1 ] / K ) is less than or equal to M ; Stores the low value ; Stores the high value ; Stores the middle value ; Update the mid value ; Check if the mid value is K ; Update the low value ; Update the high value ; Check if low is K or high is K and return it ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static bool isvalid ( int [ ] arr , int K , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( int ) Math . Ceiling ( arr [ i ] * 1.0 / K ) ; } return sum <= M ; } static int smallestValueForK ( int [ ] arr , int N , int M ) { int low = 1 ; int high = arr . Max ( ) ; int mid ; while ( high - low > 1 ) { mid = ( high + low ) / 2 ; if ( ! isvalid ( arr , mid , N , M ) ) low = mid + 1 ; else high = mid ; } return isvalid ( arr , low , N , M ) ? low : high ; } public static void Main ( ) { int [ ] arr = { 4 , 3 , 2 , 7 } ; int N = arr . Length ; int M = 5 ; Console . WriteLine ( smallestValueForK ( arr , N , M ) ) ; } }


"Smallest power of 2 consisting of N digits | C # program of the above approach ; Function to return smallest power of 2 consisting of N digits ; calculate log2 N indirectly using log ( ) method ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestNum ( int n ) { double power = log2 ( 10 ) ; return ( int ) Math . Ceiling ( ( n - 1 ) * power ) ; } static double log2 ( int N ) { return ( Math . Log ( N ) / Math . Log ( 2 ) ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( smallestNum ( n ) ) ; } }


"Smallest power of 2 consisting of N digits | C # program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestNum ( int n ) { for ( int i = 2 ; ; i *= 2 ) { int length = ( int ) ( Math . Log10 ( i ) ) + 1 ; if ( length == n ) return ( int ) ( Math . Log ( i ) / Math . Log ( 2 ) ) ; } } public static void Main ( ) { int n = 4 ; Console . Write ( smallestNum ( n ) ) ; } }


"Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | C # program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given [ ] arr ; Function call"
Solution in C#:

using System ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . Length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( findMinLength ( arr ) ) ; } }


"Smallest prime divisor of a number | C # program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } static public void Main ( ) { int n = 31 ; Console . WriteLine ( smallestDivisor ( n ) ) ; } }


"Smallest prime number missing in an array | C # implementation of the above approach ; this store all prime number upto 10 ^ 5 this function find all prime ; use sieve to find prime ; if number is prime then store it in prime vector ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver code ; find smallest prime which is not present"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList findPrime ( int MAX ) { bool [ ] pm = new bool [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) pm [ i ] = true ; pm [ 0 ] = pm [ 1 ] = false ; for ( int i = 2 ; i <= MAX ; i ++ ) if ( pm [ i ] ) for ( int j = 2 * i ; j <= MAX ; j += i ) pm [ j ] = false ; ArrayList prime = new ArrayList ( ) ; for ( int i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . Add ( i ) ; return prime ; } static int max_element ( int [ ] arr ) { int max = arr [ 0 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) max = Math . Max ( max , arr [ i ] ) ; return max ; } static int findSmallest ( int [ ] arr , int n ) { int MAX = max_element ( arr ) ; ArrayList prime = findPrime ( MAX ) ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) s . Add ( arr [ i ] ) ; int ans = - 1 ; for ( int i = 0 ; i < prime . Count ; i ++ ) { if ( ! s . Contains ( ( int ) prime [ i ] ) ) { ans = ( int ) ( prime [ i ] ) ; break ; } } return ( int ) ans ; } static void Main ( ) { int [ ] arr = { 3 , 0 , 1 , 2 , 7 } ; int n = arr . Length ; if ( findSmallest ( arr , n ) == - 1 ) Console . Write ( " No ▁ prime ▁ number ▁ missing " ) ; else Console . Write ( findSmallest ( arr , n ) ) ; } }


"Smallest root of the equation x ^ 2 + s ( x ) * x | C # program to find smallest value of root of an equation under given constraints . ; function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; driver program"
Solution in C#:

using System ; public class GFG { static bool check ( long a , long b ) { long c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } static long root ( long n ) { bool found = false ; long mx = ( long ) 1E18 ; for ( long i = 0 ; i <= 90 ; i ++ ) { long s = i * i + 4 * n ; long sq = ( long ) Math . Sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = true ; mx = Math . Min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return - 1 ; } public static void Main ( ) { long n = 110 ; Console . Write ( root ( n ) ) ; } }


"Smallest square formed with given rectangles | C # Program to find the area of the smallest square which can be formed with rectangles of given dimensions ; Recursive function to return gcd of a and b ; Everything divides 0 ; Base case ; a is greater ; Function to find the area of the smallest square ; the length or breadth or side cannot be negative ; LCM of length and breadth ; squaring to get the area ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int squarearea ( int l , int b ) { if ( l < 0 b < 0 ) return - 1 ; int n = ( l * b ) / gcd ( l , b ) ; return n * n ; } public static void Main ( ) { int l = 6 , b = 4 ; Console . Write ( squarearea ( l , b ) ) ; } }


"Smallest string divisible by two given strings | C # program for above approach ; Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code"
Solution in C#:

using System ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } static void findSmallestString ( string s , string t ) { int n = s . Length , m = t . Length ; int l = lcm ( n , m ) ; string s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 == t1 ) Console . WriteLine ( s1 ) ; else Console . WriteLine ( - 1 ) ; } public static void Main ( String [ ] args ) { string S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; } }


"Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2 | C # program for the above approach ; Function to find the length of the smallest string possible by removing substrings "01" and "11" ; Stores the length of the smallest string ; Traverse the string S ; If st is greater than 0 and S [ i ] is '1' ; Delete the last character and decrement st by 1 ; Otherwise ; Increment st by 1 ; Return the answer in st ; Driver code ; Input ; Function call"
Solution in C#:

using System ; public class GFG_JAVA { static int shortestString ( string S , int N ) { int st = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st > 0 && S [ i ] == '1' ) { st -- ; } else { st ++ ; } } return st ; } public static void Main ( string [ ] args ) { string S = "1010" ; int N = S . Length ; Console . WriteLine ( shortestString ( S , N ) ) ; } }


"Smallest string without any multiplication sign that represents the product of two given numbers | C # program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver Code ; Product A X B ; Function call"
Solution in C#:

using System ; class GFG { public static string len ( long A , long B ) { string res = " " ; long log = 0 ; do { log = ( long ) ( Math . Log ( B ) / Math . Log ( 2 ) ) ; if ( log != 0 ) { res += A + " < < " + log ; } else { res += A ; break ; } B = B - ( long ) Math . Pow ( 2 , log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( log != 0 ) ; return res ; } public static void minimumString ( long A , long B ) { string res1 = len ( A , B ) ; string res2 = len ( B , A ) ; if ( res1 . Length > res2 . Length ) { Console . WriteLine ( res2 ) ; } else { Console . WriteLine ( res1 ) ; } } public static void Main ( ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; } }


"Smallest subarray containing minimum and maximum values | C # implementation of above approach ; Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code"
Solution in C#:

using System ; using System . Linq ; public class GFG { static int minSubarray ( int [ ] A , int n ) { int minValue = A . Min ( ) ; int maxValue = A . Max ( ) ; int pos_min = - 1 , pos_max = - 1 , ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . Min ( ans , Math . Abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } static public void Main ( ) { int [ ] A = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = A . Length ; Console . WriteLine ( minSubarray ( A , n ) ) ; } }


"Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C # program for the above approach ; Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int smallSumSubset ( List < int > data , int target , int maxVal ) { int sum = 0 ; foreach ( int i in data ) sum += i ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . Count ; else if ( data [ 0 ] >= target ) return 1 ; else if ( data [ 0 ] < target ) { List < int > temp = new List < int > ( ) ; for ( int i = 1 ; i < data . Count ; i ++ ) temp . Add ( data [ i ] ) ; return Math . Min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data [ 0 ] , maxVal ) ) ; } return 0 ; } static void Main ( ) { List < int > data = new List < int > ( ) ; data . Add ( 3 ) ; data . Add ( 1 ) ; data . Add ( 7 ) ; data . Add ( 1 ) ; data . Add ( 2 ) ; int target = 11 ; int val = smallSumSubset ( data , target , data . Count + 1 ) ; if ( val > data . Count ) Console . Write ( - 1 ) ; else Console . Write ( val ) ; } }


"Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C # program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code"
Solution in C#:

using System ; class GFG { static int minlt ( int [ ] arr , int target , int n ) { int [ , ] dp = new int [ arr . Length + 1 , target + 1 ] ; for ( int i = 0 ; i < arr . Length + 1 ; i ++ ) { for ( int j = 0 ; j < target + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < arr . Length + 1 ; i ++ ) dp [ i , 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 , j ] = int . MaxValue ; for ( int i = 1 ; i <= arr . Length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i , j ] = dp [ i - 1 , j ] ; } else { dp [ i , j ] = Math . Min ( dp [ i - 1 , j ] , ( dp [ i , j - arr [ i - 1 ] ] ) != int . MaxValue ? ( dp [ i , j - arr [ i - 1 ] ] + 1 ) : int . MaxValue ) ; } } } if ( dp [ arr . Length , target ] == int . MaxValue ) { return - 1 ; } else { return dp [ arr . Length , target ] ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . Length ; Console . Write ( minlt ( arr , target , n ) ) ; } }


"Smallest subarray having an element with frequency greater than that of other elements | C # program for the above approach ; Function to find subarray ; If the array has only one element , then there is no answer . ; Array to store the last occurrences of the elements of the array . ; To maintain the length ; Variables to store start and end indices ; Check if element is occurring for the second time in the array ; Find distance between last and current index of the element . ; If the current distance is less then len update len and set ' start ' and ' end ' ; Set the last occurrence of current element to be ' i ' . ; If flag is equal to 0 , it means there is no answer . ; Driver code"
Solution in C#:

using System ; class GFG { public static void FindSubarray ( int [ ] arr , int n ) { if ( n == 1 ) { Console . WriteLine ( " No ▁ such ▁ subarray ! " ) ; } int [ ] vis = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = - 1 ; vis [ arr [ 0 ] ] = 0 ; int len = int . MaxValue , flag = 0 ; int start = 0 , end = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int t = arr [ i ] ; if ( vis [ t ] != - 1 ) { int distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) Console . WriteLine ( " No ▁ such ▁ subarray ! " ) ; else { for ( int i = start ; i <= end ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 2 , 4 , 5 } ; int n = arr . Length ; FindSubarray ( arr , n ) ; } }


"Smallest subarray which upon repetition gives the original array | C # program for the above approach ; Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printArray ( List < int > brr ) { foreach ( int it in brr ) { Console . Write ( it + " ▁ " ) ; } } static void RepeatingSubarray ( int [ ] arr , int N ) { if ( N < 2 ) { Console . Write ( " - 1" ) ; } List < int > brr = new List < int > ( ) ; brr . Add ( arr [ 0 ] ) ; brr . Add ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { bool a = false ; int n = brr . Count ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr [ K ] ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . Add ( arr [ i ] ) ; } Console . Write ( " - 1" ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = arr . Length ; RepeatingSubarray ( arr , N ) ; } }


"Smallest subarray whose product leaves remainder K when divided by size of the array | C # program to implement the above approach ; Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array"
Solution in C#:

using System ; class GFG { static int findsubArray ( int [ ] arr , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . Min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 } ; int N = arr . Length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) Console . WriteLine ( answer ) ; else Console . WriteLine ( " - 1" ) ; } }


"Smallest subarray with GCD as 1 | Segment Tree | C # implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Building the segment tree ; Two pointer variables ; To store the final answer ; Looping ; Incrementing j till we don 't get  a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code"
Solution in C#:

using System ; class GFG { static int maxLen = 30 ; static int [ ] seg = new int [ 3 * maxLen ] ; static int build ( int l , int r , int ind , int [ ] arr ) { if ( l == r ) return seg [ ind ] = arr [ l ] ; int mid = ( l + r ) / 2 ; return seg [ ind ] = __gcd ( build ( l , mid , 2 * ind + 1 , arr ) , build ( mid + 1 , r , 2 * ind + 2 , arr ) ) ; } static int query ( int l , int r , int l1 , int r1 , int ind ) { if ( l1 <= l && r <= r1 ) return seg [ ind ] ; if ( l > r1 r < l1 ) return 0 ; int mid = ( l + r ) / 2 ; return __gcd ( query ( l , mid , l1 , r1 , 2 * ind + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * ind + 2 ) ) ; } static int findLen ( int [ ] arr , int n ) { build ( 0 , n - 1 , 0 , arr ) ; int i = 0 , j = 0 ; int ans = int . MaxValue ; while ( i < n ) { while ( j < n && query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = Math . Min ( ( j - i + 1 ) , ans ) ; i ++ ; j = Math . Max ( j , i ) ; } if ( ans == int . MaxValue ) return - 1 ; else return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 2 } ; int n = arr . Length ; Console . WriteLine ( findLen ( arr , n ) ) ; } }


"Smallest subarray with all occurrences of a most frequent element | C # implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static void smallestSubsegment ( int [ ] a , int n ) { Dictionary < int , int > left = new Dictionary < int , int > ( ) ; Dictionary < int , int > count = new Dictionary < int , int > ( ) ; int mx = 0 ; int mn = - 1 , strindex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( ! count . ContainsKey ( x ) ) { left . Add ( x , i ) ; count . Add ( x , 1 ) ; } else count [ x ] = count [ x ] + 1 ; if ( count [ x ] > mx ) { mx = count [ x ] ; mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } else if ( ( count [ x ] == mx ) && ( i - left [ x ] + 1 < mn ) ) { mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 2 , 2 , 1 } ; int n = A . Length ; smallestSubsegment ( A , n ) ; } }


"Smallest subarray with k distinct numbers | C # program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void minRange ( int [ ] arr , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { ISet < int > s = new HashSet < int > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . Add ( arr [ j ] ) ; if ( s . Count == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) { break ; } } if ( l == 0 && r == n ) { Console . WriteLine ( " Invalid ▁ k " ) ; } else { Console . WriteLine ( l + " ▁ " + r ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 3 ; minRange ( arr , n , k ) ; } }


"Smallest subarray with k distinct numbers | C # program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; Increment right side . ; If number of distinct elements less than k . ; If distinct elements are equal to k and length is less than previous length . ; If number of distinct elements less than k , then break . ; If distinct elements equals to k then try to increment left side . ; Increment left side . ; It is same as explained in above loop . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minRange ( int [ ] arr , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . Count < k ) if ( hm . ContainsKey ( arr [ j ] ) ) hm [ arr [ j ] ] = hm [ arr [ j ] ] + 1 ; else hm . Add ( arr [ j ] , 1 ) ; if ( hm . Count == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . Count < k ) break ; while ( hm . Count == k ) { if ( hm . ContainsKey ( arr [ i ] ) && hm [ arr [ i ] ] == 1 ) hm . Remove ( arr [ i ] ) ; else { if ( hm . ContainsKey ( arr [ i ] ) ) hm [ arr [ i ] ] = hm [ arr [ i ] ] - 1 ; } i ++ ; if ( hm . Count == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . ContainsKey ( arr [ i ] ) && hm [ arr [ i ] ] == 1 ) hm . Remove ( arr [ i ] ) ; else if ( hm . ContainsKey ( arr [ i ] ) ) hm [ arr [ i ] ] = hm [ arr [ i ] ] - 1 ; } if ( l == 0 && r == n ) Console . WriteLine ( " Invalid ▁ k " ) ; else Console . WriteLine ( l + " ▁ " + r ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 3 ; minRange ( arr , n , k ) ; } }


"Smallest subarray with sum greater than a given value | C # program to find Smallest subarray with sum greater than a given value ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestSubWithSum ( int [ ] arr , int n , int x ) { int min_len = n + 1 ; for ( int start = 0 ; start < n ; start ++ ) { int curr_sum = arr [ start ] ; if ( curr_sum > x ) return 1 ; for ( int end = start + 1 ; end < n ; end ++ ) { curr_sum += arr [ end ] ; if ( curr_sum > x && ( end - start + 1 ) < min_len ) min_len = ( end - start + 1 ) ; } } return min_len ; } static public void Main ( ) { int [ ] arr1 = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . Length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res1 ) ; int [ ] arr2 = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . Length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res2 ) ; int [ ] arr3 = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . Length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res3 ) ; } }


"Smallest subarray with sum greater than a given value | O ( n ) solution for finding smallest subarray with sum greater than x ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestSubWithSum ( int [ ] arr , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } static public void Main ( ) { int [ ] arr1 = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . Length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res1 ) ; int [ ] arr2 = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . Length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res2 ) ; int [ ] arr3 = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . Length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res3 ) ; } }


"Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K | C # program to implement the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is equal to S % K ; Remainder when total_sum is divided by K ; Stores curr_remainder and the most recent index at which curr_remainder has occured ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; Update res ; If not possible ; Return the result ; Function to find the smallest submatrix rqured to be deleted to make the sum of the matrix divisible by K ; Stores the sum of element of the matrix ; Traverse the matrix [ , ] mat ; Update S ; Stores smallest area need to be deleted to get sum divisible by K ; Stores leftmost column of each matrix ; Stores rightmost column of each matrix ; Stores number of coulmns deleted of a matrix ; Store area of the deleted matrix ; prefixRowSum [ i ] : Store sum of sub matrix whose topmost left and bottommost right position is ( 0 , left ) ( i , right ) ; Iterate over all possible values of ( left , right ) ; Initialize all possible values of prefixRowSum [ ] to 0 ; Traverse each row from left to right column ; Update row_sum [ i ] ; ; Update width ; If no submatrix of the length ( right - left + 1 ) found to get the required output ; Update area ; If area is less than min_area ; Update min_area ; Driver Code ; Stores number of rows in the matrix ; Stores number of column in the matrix"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int removeSmallestSubarray ( int [ ] arr , int S , int n , int k ) { int target_remainder = S % k ; Dictionary < int , int > map1 = new Dictionary < int , int > ( ) ; map1 . Add ( 0 , - 1 ) ; int curr_remainder = 0 ; int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 [ curr_remainder ] = i ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . ContainsKey ( mod ) ) { res = Math . Min ( res , i - map1 [ mod ] ) ; } } if ( res == int . MaxValue res == n ) { res = - 1 ; } return res ; } static int smstSubmatDeleted ( int [ , ] mat , int N , int M , int K ) { int S = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) S += mat [ i , j ] ; } int min_area = N * M ; int left = 0 ; int right = 0 ; int width ; int area ; int [ ] prefixRowSum = new int [ N ] ; for ( left = 0 ; left < M ; left ++ ) { for ( int i = 0 ; i < prefixRowSum . Length ; i ++ ) prefixRowSum [ i ] = 0 ; for ( right = left ; right < M ; right ++ ) { for ( int i = 0 ; i < N ; i ++ ) { prefixRowSum [ i ] += mat [ i , right ] ; } width = removeSmallestSubarray ( prefixRowSum , S , N , K ) ; if ( width != - 1 ) { area = ( right - left + 1 ) * ( width ) ; if ( area < min_area ) { min_area = area ; } } } } return min_area ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 6 , 2 , 6 } , { 3 , 2 , 8 } , { 2 , 5 , 3 } } ; int K = 3 ; int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; Console . Write ( smstSubmatDeleted ( mat , N , M , K ) ) ; } }


"Smallest submatrix with Kth maximum XOR | C # Program for the above approach ; Function to print smallest index of Kth maximum Xor value of submatrices ; Dimensions of matrix ; Stores XOR values for every index ; Min heap to find the kth maximum XOR value ; Stores indices for corresponding XOR values ; Traversing matrix to calculate XOR values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xor [ i , j ] ; Stores the kth maximum element ; Print the required index ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void smallestPosition ( int [ , ] m , int k ) { int n = m . GetLength ( 0 ) ; int mm = m . GetLength ( 1 ) ; int [ , ] xor = new int [ n , mm ] ; List < int > minHeap = new List < int > ( ) ; Dictionary < int , int [ ] > map = new Dictionary < int , int [ ] > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xor [ i - 1 , j ] : 0 ; int b = j - 1 >= 0 ? xor [ i , j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xor [ i - 1 , j - 1 ] : 0 ; xor [ i , j ] = m [ i , j ] ^ a ^ b ^ c ; minHeap . Add ( xor [ i , j ] ) ; minHeap . Sort ( ) ; if ( minHeap . Count > k ) { minHeap . RemoveAt ( 0 ) ; } if ( ! map . ContainsKey ( xor [ i , j ] ) ) map . Add ( xor [ i , j ] , new int [ ] { i , j } ) ; } } minHeap . Sort ( ) ; int kth_max_e = minHeap [ 0 ] ; Console . WriteLine ( ( map [ kth_max_e ] [ 0 ] + 1 ) + " ▁ " + ( map [ kth_max_e ] [ 1 ] + 1 ) ) ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k ) ; } }


"Smallest subsequence having GCD equal to GCD of given array | C # program to implement the above approach ; Function to calculate gcd of two numbers ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSmallSub ( int [ ] arr , int N ) { int gcdArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { gcdArr = gcd ( gcdArr , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { Console . Write ( arr [ i ] + " ▁ " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { Console . Write ( arr [ i ] + " ▁ " + arr [ j ] ) ; return ; } } } } public static void Main ( ) { int [ ] arr = { 4 , 6 , 12 } ; int N = arr . Length ; printSmallSub ( arr , N ) ; } }


"Smallest subsequence with sum of absolute difference of consecutive elements maximized | C # program to find smallest subsequence with sum of absolute difference of consecutive elements maximized ; Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of readonly subsequence ; Print the subsequence ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { List < int > req = new List < int > ( ) ; req . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . Add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . Add ( arr [ i ] ) ; } req . Add ( arr [ n - 1 ] ) ; Console . Write ( req . Count + "
 " ) ; foreach ( int x in req ) Console . Write ( x + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . Length ; getSubsequence ( arr , n ) ; } }


"Smallest subset of maximum sum possible by splitting array into two subsets | C # program for the above approach ; Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array ; Stores the sum of the resultant set ; Stores if it is possible to split the array that satisfies the conditions ; Stores the elements of the first subseta ; Traverse the array arr [ ] ; Increment total sum ; Increment count of arr [ i ] ; Sort the array arr [ ] ; Stores the index of the last element of the array ; Traverse the array arr [ ] ; Stores the frequency of arr [ i ] ; If frq + ans . size ( ) is at most remaining size ; Append arr [ i ] to ans ; Decrement totSum by arr [ i ] ; Increment s by arr [ i ] ; Otherwise , decrement i by frq ; If s is greater than totSum ; Mark flag 1 ; If flag is equal to 1 ; Print the arrList ans ; Otherwise , print " - 1" ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findSubset ( List < int > arr ) { int N = arr . Count ; int i ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int totSum = 0 ; int s = 0 ; int flag = 0 ; List < int > ans = new List < int > ( ) ; for ( i = 0 ; i < arr . Count ; i ++ ) { totSum += arr [ i ] ; if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; else mp . Add ( arr [ i ] , 1 ) ; } arr . Sort ( ) ; i = N - 1 ; while ( i >= 0 ) { int frq = mp [ arr [ i ] ] ; if ( ( frq + ans . Count ) < ( N - ( frq + ans . Count ) ) ) { for ( int k = 0 ; k < frq ; k ++ ) { ans . Add ( arr [ i ] ) ; totSum -= arr [ i ] ; s += arr [ i ] ; i -- ; } } else { i -= frq ; } if ( s > totSum ) { flag = 1 ; break ; } } if ( flag == 1 ) { for ( i = ans . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { Console . Write ( - 1 ) ; } } public static void Main ( ) { List < int > arr = new List < int > ( ) { 5 , 3 , 2 , 4 , 1 , 2 } ; findSubset ( arr ) ; } }


"Smallest substring with each letter occurring both in uppercase and lowercase | C # program for the above approach ; Function to check if the current string is balanced or not ; For every character , check if there exists uppercase as well as lowercase characters ; Function to find smallest length substring in the given string which is balanced ; Store frequency of lowercase characters ; Stores frequency of uppercase characters ; Count frequency of characters ; Mark those characters which are not present in both lowercase and uppercase ; Initialize the frequencies back to 0 ; Marks the start and end of current substring ; Marks the start and end of required substring ; Stores the length of smallest balanced substring ; Remove all characters obtained so far ; Remove extra characters from front of the current substring ; If substring ( st , i ) is balanced ; No balanced substring ; Store answer string ; Driver Code ; Given string"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public const int MaxValue = 2147483647 ; static bool balanced ( int [ ] small , int [ ] caps ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && ( caps [ i ] == 0 ) ) return false ; else if ( ( small [ i ] == 0 ) && ( caps [ i ] != 0 ) ) return false ; } return true ; } static void smallestBalancedSubstring ( string s ) { int [ ] small = new int [ 26 ] ; int i ; int [ ] caps = new int [ 26 ] ; Array . Clear ( small , 0 , small . Length ) ; Array . Clear ( caps , 0 , caps . Length ) ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] >= 65 && s [ i ] <= 90 ) caps [ ( int ) s [ i ] - 65 ] ++ ; else small [ ( int ) s [ i ] - 97 ] ++ ; } Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && caps [ i ] == 0 ) { mp [ ( char ) ( i + 97 ) ] = 1 ; } else if ( caps [ i ] != 0 && small [ i ] == 0 ) mp [ ( char ) ( i + 65 ) ] = 1 ; } Array . Clear ( small , 0 , small . Length ) ; Array . Clear ( caps , 0 , caps . Length ) ; i = 0 ; int st = 0 ; int start = - 1 , end = - 1 ; int minm = MaxValue ; while ( i < s . Length ) { if ( mp . ContainsKey ( s [ i ] ) ) { while ( st < i ) { if ( ( int ) s [ st ] >= 65 && ( int ) s [ st ] <= 90 ) caps [ ( int ) s [ st ] - 65 ] -- ; else small [ ( int ) s [ st ] - 97 ] -- ; st ++ ; } i += 1 ; st = i ; } else { if ( ( int ) s [ i ] >= 65 && ( int ) s [ i ] <= 90 ) caps [ ( int ) s [ i ] - 65 ] ++ ; else small [ ( int ) s [ i ] - 97 ] ++ ; while ( true ) { if ( ( int ) s [ st ] >= 65 && ( int ) s [ st ] <= 90 && caps [ ( int ) s [ st ] - 65 ] > 1 ) { caps [ ( int ) s [ st ] - 65 ] -- ; st ++ ; } else if ( ( int ) s [ st ] >= 97 && ( int ) s [ st ] <= 122 && small [ ( int ) s [ st ] - 97 ] > 1 ) { small [ ( int ) s [ st ] - 97 ] -- ; st ++ ; } else break ; } if ( balanced ( small , caps ) ) { if ( minm > ( i - st + 1 ) ) { minm = i - st + 1 ; start = st ; end = i ; } } i += 1 ; } } if ( start == - 1 end == - 1 ) Console . WriteLine ( - 1 )...


"Smallest sum contiguous subarray | C # implementation to find the smallest sum contiguous subarray ; function to find the smallest sum contiguous subarray ; to store the minimum value that is ending up to the current index ; to store the minimum value encountered so far ; traverse the array elements ; if min_ending_here > 0 , then it could not possibly contribute to the minimum sum further ; else add the value arr [ i ] to min_ending_here ; update min_so_far ; required smallest sum contiguous subarray value ; Driver method"
Solution in C#:

using System ; class GFG { static int smallestSumSubarr ( int [ ] arr , int n ) { int min_ending_here = 2147483647 ; int min_so_far = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_ending_here > 0 ) min_ending_here = arr [ i ] ; else min_ending_here += arr [ i ] ; min_so_far = Math . Min ( min_so_far , min_ending_here ) ; } return min_so_far ; } public static void Main ( ) { int [ ] arr = { 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 } ; int n = arr . Length ; Console . Write ( " Smallest ▁ sum : ▁ " + smallestSumSubarr ( arr , n ) ) ; } }


"Smallest sum contiguous subarray | Set | C # program for Smallest sum contiguous subarray | Set 2 ; function to find the smallest sum contiguous subarray ; First invert the sign of the elements ; Apply the normal Kadane algorithm But on the elements Of the array having inverted sign ; Invert the answer to get minimum val ; Driver Code"
Solution in C#:

using System ; class GFG { static int smallestSumSubarr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = - arr [ i ] ; int sum_here = arr [ 0 ] , max_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum_here = Math . Max ( sum_here + arr [ i ] , arr [ i ] ) ; max_sum = Math . Max ( max_sum , sum_here ) ; } return ( - 1 ) * max_sum ; } public static void Main ( ) { int [ ] arr = { 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 } ; int n = arr . Length ; Console . WriteLine ( " Smallest ▁ sum : ▁ " + smallestSumSubarr ( arr , n ) ) ; } }


"Smallest triangular number larger than p | C # code to find the bucket to choose for picking flowers out of it ; Driver code"
Solution in C#:

using System ; class GFG { static int findBucketNo ( int p ) { return ( int ) Math . Ceiling ( ( Math . Sqrt ( 8 * p + 1 ) - 1 ) / 2 ) ; } static public void Main ( ) { int p = 10 ; Console . WriteLine ( findBucketNo ( p ) ) ; } }


"Smallest value in each level of Binary Tree | C # program to print minimum element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Iterative method to find every level minimum element of Binary Tree ; Base Case ; Create an empty queue for level order traversal ; push the root for Change the level ; for go level by level ; for check the level ; Get top of queue ; if node == null ( Means this is boundary between two levels ) ; here queue is empty represent no element in the actual queue ; increment level ; Reset min for next level minimum value ; get Minimum in every level ; Enqueue left child ; Enqueue right child ; Utility function to create a new tree node ; Driver code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root ) { if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; int min = int . MaxValue ; int level = 0 ; while ( q . Count != 0 ) { Node node = q . Peek ( ) ; q . Dequeue ( ) ; if ( node == null ) { Console . Write ( " level ▁ " + level + " ▁ min ▁ is ▁ = ▁ " + min + "
 " ) ; if ( q . Count == 0 ) break ; q . Enqueue ( null ) ; level ++ ; min = int . MaxValue ; continue ; } if ( min > node . data ) min = node . data ; if ( node . left != null ) { q . Enqueue ( node . left ) ; } if ( node . right != null ) { q . Enqueue ( node . right ) ; } } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; Console . Write ( " Every ▁ Level ▁ minimum ▁ is " + "
 " ) ; printPerLevelMinimum ( root ) ; } }


"Smallest value in each level of Binary Tree | C # program to print smallest element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Inorder Traversal Search minimum element in each level and store it into vector array . ; height of tree for the size of vector array ; vector for store all minimum of every level ; save every level minimum using inorder traversal ; print every level minimum ; Utility function to create a new tree node ; Driver Code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1"
Solution in C#:

 using System ; class GFG { static int INT_MAX = ( int ) 10e6 ; public class Node { public int data ; public Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root , int [ ] res , int level ) { if ( root != null ) { printPerLevelMinimum ( root . left , res , level + 1 ) ; if ( root . data < res [ level ] ) res [ level ] = root . data ; printPerLevelMinimum ( root . right , res , level + 1 ) ; } } static void perLevelMinimumUtility ( Node root ) { int n = heightoftree ( root ) , i ; int [ ] res = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) res [ i ] = INT_MAX ; printPerLevelMinimum ( root , res , 0 ) ; Console . Write ( " Every ▁ level ▁ minimum ▁ is
 " ) ; for ( i = 0 ; i < n ; i ++ ) { Console . Write ( " level ▁ " + i + " ▁ min ▁ is ▁ = ▁ " + res [ i ] + "
 " ) ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; perLevelMinimumUtility ( root ) ; } }


"Smallest value of N such that the sum of all natural numbers from K to N is at least X | C # program for the above approach ; Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isGreaterEqual ( int N , int K , int X ) { return ( ( N * 1L * ( N + 1 ) / 2 ) - ( ( K - 1 ) * 1L * K / 2 ) ) >= X ; } static void minimumNumber ( int K , int X ) { if ( K > X ) { Console . Write ( " - 1" ) ; return ; } int low = K , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , K , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } Console . WriteLine ( res ) ; } public static void Main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }


"Smallest value of N such that the sum of all natural numbers from K to N is at least X | C # program for the above approach ; Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumNumber ( int K , int X ) { if ( K > X ) { Console . Write ( " - 1" ) ; return ; } int ans = 0 ; int sum = 0 ; for ( int i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } Console . Write ( ans ) ; } public static void Main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }