java - Given a natural number N, find the largest number not greater than N with monotone nondecreasing digits

Java - Given a natural number N, find the largest number not greater than N with monotone nondecreasing digits

To find the largest number not greater than N with monotone nondecreasing digits, you can convert the number to a string, iterate through its digits from left to right, and modify the digits if necessary to maintain the nondecreasing order. Here's how you can implement this in Java:

public class MonotoneDigits { public static void main(String[] args) { int N = 23458; int result = largestNonIncreasing(N); System.out.println("Largest number not greater than " + N + " with monotone nondecreasing digits: " + result); } public static int largestNonIncreasing(int N) { char[] digits = String.valueOf(N).toCharArray(); int n = digits.length; // Find the index where the digits are no longer nondecreasing int i = 1; while (i < n && digits[i - 1] <= digits[i]) { i++; } // If the digits are already nondecreasing, return N if (i == n) { return N; } // Decrease the digit at index i - 1 and replace all subsequent digits with 9 while (i > 0 && digits[i - 1] > digits[i]) { digits[--i]--; } // Convert the modified digits back to an integer int result = Integer.parseInt(String.valueOf(digits)); return result; } } 

In this code:

  • We convert the number N to a character array of digits.
  • We iterate through the digits from left to right to find the index where the digits are no longer nondecreasing.
  • If the digits are already nondecreasing, we return N.
  • Otherwise, we decrease the digit at the index found earlier by 1 and replace all subsequent digits with 9 to maintain the nondecreasing order.
  • Finally, we convert the modified digits back to an integer and return the result.

Examples

  1. "Java code for finding the largest monotone nondecreasing number not greater than N"

    • Description: This query seeks Java code to solve the problem of finding the largest monotone nondecreasing number not exceeding a given number N. The code will ensure that the digits in the number are in nondecreasing order.
    public class LargestMonotoneNumber { public static int findLargestMonotone(int N) { String numString = Integer.toString(N); char[] digits = numString.toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 456723; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  2. "Java algorithm for monotone nondecreasing number search"

    • Description: This query looks for an algorithm implemented in Java to search for the largest monotone nondecreasing number that is not greater than a given number N.
    public class MonotoneNumberSearch { public static int findMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 6789012; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findMonotone(N)); } } 
  3. "Java program for finding monotone nondecreasing number"

    • Description: This query seeks a Java program that can find the largest monotone nondecreasing number not greater than a given number N.
    public class MonotoneNumberFinder { public static int findMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 789; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findMonotone(N)); } } 
  4. "Java code to find largest monotone nondecreasing integer"

    • Description: This query is about finding Java code to determine the largest monotone nondecreasing integer not exceeding a given integer N.
    public class LargestMonotoneInteger { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 123456; System.out.println("Largest monotone nondecreasing integer not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  5. "Java code to find largest monotone nondecreasing number less than or equal to N"

    • Description: This query seeks Java code to find the largest monotone nondecreasing number less than or equal to a given number N.
    public class LargestMonotoneNumberFinder { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 999876; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  6. "Java code for largest monotone nondecreasing number less than N"

    • Description: This query looks for Java code to find the largest monotone nondecreasing number less than a given number N.
    public class LargestMonotoneNumberLessThanN { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 321; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  7. "Java code implementation for largest monotone nondecreasing number"

    • Description: This query is about finding a Java code implementation to determine the largest monotone nondecreasing number not exceeding a given number N.
    public class MonotoneNumber { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 987654; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  8. "Java code for largest monotone nondecreasing integer not greater than N"

    • Description: This query seeks Java code to find the largest monotone nondecreasing integer not greater than a given integer N.
    public class MonotoneIntegerFinder { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 987123; System.out.println("Largest monotone nondecreasing integer not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  9. "Java code to find largest monotone nondecreasing number less than or equal to given N"

    • Description: This query is about finding Java code to find the largest monotone nondecreasing number less than or equal to a given number N.
    public class MonotoneNumberLessThanN { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 54321; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 
  10. "Java code to find largest monotone nondecreasing number not greater than N"

    • Description: This query seeks Java code to find the largest monotone nondecreasing number not greater than a given number N.
    public class MonotoneNondecreasingNumber { public static int findLargestMonotone(int N) { char[] digits = Integer.toString(N).toCharArray(); int pivot = -1; for (int i = digits.length - 1; i > 0; i--) { if (digits[i] < digits[i - 1]) { pivot = i - 1; break; } } if (pivot == -1) return N; for (int i = pivot + 1; i < digits.length; i++) { digits[i] = '9'; // Change digits to '9' to maintain nondecreasing order } digits[pivot]--; return Integer.parseInt(String.valueOf(digits)); } public static void main(String[] args) { int N = 1234567; System.out.println("Largest monotone nondecreasing number not greater than " + N + " is: " + findLargestMonotone(N)); } } 

More Tags

stock sharedpreferences sas python-packaging windows-ce android-safe-args angular-google-maps formatexception hbase onclicklistener

More Programming Questions

More Chemical thermodynamics Calculators

More Biochemistry Calculators

More Retirement Calculators

More Genetics Calculators