fork download
  1. public class Main {
  2. public static int findUpperBound(int[] arr, int target) {
  3. for (int i = 0; i < arr.length; i++) {
  4. if (arr[i] > target) {
  5. return i;
  6. }
  7. }
  8. return arr.length;
  9. }
  10.  
  11. public static void main(String[] args) {
  12. int[] arr = {1, 3, 5, 6, 6, 7, 7, 9};
  13. int target = 6;
  14. int upperBoundIndex = findUpperBound(arr, target);
  15.  
  16. if (upperBoundIndex < arr.length) {
  17. System.out.println("Upper bound of " + target + " is at index " + upperBoundIndex);
  18. } else {
  19. System.out.println("Upper bound of " + target + " is not found in the array.");
  20. }
  21. }
  22. }
  23.  
Success #stdin #stdout 0.13s 57440KB
stdin
Standard input is empty
stdout
Upper bound of 6 is at index 5