Skip to main content
Question Protected by Jamal
added 23 characters in body; edited tags
Source Link
JavaDeveloper
  • 8.5k
  • 29
  • 93
  • 162

Code review requested to make this code simpler, cleaner, and better. Input array is sorted.

Code review requested to make this code simpler, cleaner, and better.

Code review requested to make this code simpler, cleaner, and better. Input array is sorted.

Tweeted twitter.com/#!/StackCodeReview/status/392357646126555136
added 15 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Find Finding greatest value in array lessersmaller than x, code review request

Request for codeCode review, requested to make this code simpler, cleaner, and better. 

This code will findprogram finds the greatest number smaller than "x" sox. So, in an array [10 20 30 40][10 20 30 40] and x = 25x = 25, the output should be 20.

Find greatest value in array lesser than x, code review request

Request for code review, to make this code simpler, cleaner better. This code will find greatest number smaller than "x" so in an array [10 20 30 40] and x = 25, the output should be 20.

Finding greatest value in array smaller than x

Code review requested to make this code simpler, cleaner, and better. 

This program finds the greatest number smaller than x. So, in an array [10 20 30 40] and x = 25, the output should be 20.

Source Link
JavaDeveloper
  • 8.5k
  • 29
  • 93
  • 162

Find greatest value in array lesser than x, code review request

Request for code review, to make this code simpler, cleaner better. This code will find greatest number smaller than "x" so in an array [10 20 30 40] and x = 25, the output should be 20.

public class GreatestValueLesserThanEqualToX {

    public static Integer findGreatestValueLesserThanOrEqualToX (int[] arr, int x) {
        if (arr == null) {
            throw new NullPointerException(" The input array is null. ");
        }
        if (arr.length >= 1) {
            return findGreatestValueLesserThanOrEqualToX (arr, x, 0, arr.length - 1);
        } else {
            return null;  // note the difference wrt greatest value lesser than X.
        }
    }

    private static Integer findGreatestValueLesserThanOrEqualToX (int[] arr, int x, int lb, int ub) {

        assert arr != null;

        final int mid = (lb + ub) / 2;

        /**
         *  Testing boundaries.
         */
        // testing lower boundary.
        if (mid == 0) {
            if (arr[mid] > x) {
                return null;
            }

            // single element array with value lesser than input value.
            if (arr.length == 1) {
                return arr[0];
            }
        }

        //testing higher boundary.
        if (lb == (arr.length - 1) && arr[mid] < x) {
            return arr[mid]; 
        }

        /**
         *  Testing equalities and duplicates.
         */
        // testing equality and duplicates. eg: consider input like: 1, 2, 2, 5
        if (arr[mid] == x || arr[mid + 1] == x) {
            return x;
        }

        /**
         *  Testing when element is in the range of array elements.
         */
        // input x in range of array elements.
        if (arr[mid] < x && arr[mid + 1] > x) {
             return arr[mid]; // note the difference wrt greatest value lesser than X.
         }

         if (arr[mid] < x) {
             return findGreatestValueLesserThanOrEqualToX (arr, x, mid + 1, ub);
         } else {
             return findGreatestValueLesserThanOrEqualToX (arr, x, lb, mid);
         }
     }
}