title: find predecessor of a given element from an array
body: <p>How to find the predecessor of a given number from a given array of numbers?
  For example, if the given array contains 
  <code>-2,1,0,3</code> <br>and the input number is <code>0</code>, then the predecessor is <code>-2</code>.</p>
  
  <p>I wrote the following code:</p>
  
  <pre><code>public static int getPredecessor(int[] inpArr, int key) {
      int minDiff = key&lt;=0 ? (key-inpArr[0]) : key;
      int predecessor = key;
      for(int i=0;i&lt;inpArr.length;i++) {
          if(inpArr[i] &lt; key &amp;&amp; (key - inpArr[i])&lt;=minDiff)
          {
              minDiff = key - inpArr[i];
              predecessor = inpArr[i];
          }   
      }
      return predecessor;
  }
  </code></pre>
  
  <p>What I have done is basically keep track of the minimum difference between the supplied number and each number in the array; whenever the minimum difference is encountered, that particular number in the array is stored as the predecessor. If the final return statement is going to return the same number as the input number, then it means no predecessor was found in the given array.</p>
  
  <p><strong>My question is:</strong><br>
  can the code be optimized in any way? It runs in O(n) time and O(1) space complexities.</p>