# Print the largest number below ‘n’ which does not contain the digit ‘k’ in java

In this tutorial, we are going to understand another string utilization problem. Learn how to print the largest number below ‘n’ which does not contain the digit ‘k’ in Java.

**Problem statement: **Print the largest number below a given integer ‘n’ which does not contain the digit ‘k’.

**Input Format:**

The first line of the input contains an integer ‘N’. The second line of the input contains an integer ‘k’.

**Output Format:**

Print the largest number below ‘N’ which does not contain ‘k’.

**Sample Input:**

1234

2

**Sample Output:**

1199

**Explanation: **This problem can be solved easily with the help of simple string functions. Parse the given integer as a string and check whether the string contains ‘k’ using contains() function. Decrement ‘n’ if it contains ‘k’ and continue the same process for each and every integer up to which the integer ‘k’ does not contain.

## Java program to print the largest number below ‘n’ which does not contain the digit ‘k’

Source Code:

import java.util.*; import java.lang.*; class codespeedy { public static void main(String args[]){ int n=1234; String k="2"; while(n!=0){ String s = Integer.toString(n); if(s.contains(k) == false){ System.out.println(s); break; } n--; } } }

Output:

1199

**Code Explanation: **A static input for the integer ‘n’ is taken as”1234″ and ‘2’ is taken as a string so as to utilize the contains() method effectively. The while loop starts where the integer is parsed into a string and checks if it contains ‘2’. If it does not contain ‘2’ decrement n and continue while loop. If the decremented number contains ‘2’ then print that number and break the loop.

1234 contains ‘2’ so decrement to 1233

1233 contains ‘2’ so decrement to 1232

1232 contains ‘2’ so decrement to 1231 so on…

.

.

.

1200 contains ‘2’ so decrement to 1199

1199 does not contain ‘2’ so print that number and break.

**Time Complexity: **O(n)

Also read:

## Leave a Reply