text: Find smallest number formed by inserting given digit | C # program for the above approach ; Function to insert X in N and return the minimum value string ; Variable to store length of string N ; Variable to denote the position where X must be added ; If the given string N represent a negative value ; X must be place at the last index where is greater than N [ i ] ; For positive numbers , X must be placed at the last index where it is smaller than N [ i ] ; Insert X at that position ; return string ; Driver code ; given input ; function call
code: using System ; class GFG { static String MinValue ( string number , int x ) { int length = number . Length ; int position = length + 1 ; if ( number [ 0 ] == ' - ' ) { for ( int i = number . Length - 1 ; i >= 1 ; -- i ) { if ( ( number [ i ] - 48 ) < x ) { position = i ; } } } else { for ( int i = number . Length - 1 ; i >= 0 ; -- i ) { if ( ( number [ i ] - 48 ) > x ) { position = i ; } } } number = number . Substring ( 0 , position ) + x + number . Substring ( position , number . Length ) ; return number . ToString ( ) ; } public static void Main ( ) { string number = "89" ; int x = 1 ; Console . WriteLine ( MinValue ( number , x ) ) ; } }