Worst Implementation of Binary Search
\nclass Solution {\n    public int maximizeWin(int[] arr, int k) {\n        int n = arr.length;\n        int[] pre = new int[n];\n        int count = 0;\n      
```\nclass Solution {\n    public int maximizeWin(int[] arr, int k) {\n        int n = arr.length;\n        int[] pre = new int[n];\n        int count = 0;\n        for(int i = 0 ; i < arr.length ; i++){\n            pre[i] = count;\n            count++;\n        }\n        for(int i = arr.length - 1 ; i >= 0 ; i--){\n            int index = getIndex(0 , i , arr[i] - k , arr);\n            pre[i] = pre[i] +1 - pre[index];\n        }\n        // for(int i = 0 ; i < arr.length ; i++){\n        //     System.out.print(pre[i] + " ");\n        // }\n        count = 0;\n        int[] suffix = new int[n];\n        for(int i = arr.length - 1 ; i >= 0 ; i--){\n            suffix[i] = count;\n            count++;\n        }\n        for(int i = 0 ; i < arr.length ; i++){\n            int index = getIndex2(i , arr.length - 1 , arr[i] + k , arr);\n            suffix[i] =  - suffix[index] + suffix[i] + 1;\n        }\n        //System.out.println();\n        // for(int i = 0 ; i < arr.length ; i++){\n        //     System.out.print(suffix[i] + " ");\n        // }\n        int cur = 0;\n        for(int i = 0 ; i < arr.length ; i++){\n            cur = Math.max(cur , pre[i]);\n            pre[i] = cur;\n        }\n        cur = 0;\n        for(int i = arr.length - 1 ; i >= 0 ; i--){\n            cur = Math.max(cur , suffix[i]);\n            suffix[i] = cur;\n        }\n        int ans = 0;\n        for(int i = 0 ; i < arr.length - 1 ; i++){\n            ans = Math.max(ans , pre[i]+suffix[i+1]);\n        }\n        if(arr.length == 1){\n            return pre[0];\n        }\n        return ans;\n    }\n    private int getIndex2(int i , int j , int target ,int[] arr){\n        int index = -1000000;\n        while(i <= j){\n            int mid = (i+j) >> 1;\n            if(arr[mid] <= target){\n                index = Math.max(mid , index);\n                i = mid + 1;\n            }else{\n                j = mid - 1; \n            }\n        }\n        return index == -1000000 ? arr.length - 1 : index;\n    }\n    private int getIndex(int i , int j , int target ,int[] arr){\n        int index = 1000000;\n        while(i <= j){\n            int mid = (i+j) >> 1;\n            if(arr[mid] >= target){\n                index = Math.min(mid , index);\n                j = mid - 1;\n            }else{\n                i = mid + 1; \n            }\n        }\n        return index == 1000000 ? 0 : index;\n    }\n}\n```
[]