What's wrong with my solution of SearchInsert? <p>puzzle:</p> <blockquote> <p>Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.</p> <p>You may assume no duplicates in the array.</p> </blockquote> <p>And my code here:</p> <pre><code>public class Solution { public static int searchInsert(int[] nums, int target) { return searchInsert(nums, target, 0, nums.length-1); } private static int searchInsert(int[] nums, int target, int start, int end) { if(start &lt;= end) { return target &lt;= nums[start] ? start : (start+1); } int m = (start + end) / 2; if(nums[m] == target) { return m; } else if(nums[m] &lt; target) { return searchInsert(nums, target, m+1, end); } else { return searchInsert(nums, target, start, m-1); } } public static void main(String[] args) { int[] nums = {1, 3}; System.out.print(searchInsert(nums, 4); } } </code></pre> <p>The result turns out like this:</p> <blockquote> <p>Input:</p> <p>[1,3]</p> <p>4</p> <p>Output:</p> <p>1</p> <p>Expected:</p> <p>2</p> </blockquote> <p>I've simulate the process of this input over and over again on paper, but just cannot figure out how my code could output <code>2</code>.</p> <p>Please help me with this, thx in advance.</p>