Please help me find error in this code
My thought process was- first find the subarray in which the count of prizes is max for a given k.. then remove those elements already picked and repeat the sam
My thought process was- first find the subarray in which the count of prizes is max for a given k.. then remove those elements already picked and repeat the same process for 1 more time on the new array since there should be exactly 2 segments.. the code I wrote is as follows-  (It passed 61 out of 65 test cases during the contest)\n```\nclass Solution {\n    public int maximizeWin(int[] pos, int k) {\n        int n = pos.length;\n        \n        long max1=Long.MIN_VALUE, sum=0;\n        int maxIdx=0, i=0,j=0, maxIdx2=0;\n        \n        while(i<n){\n            while(j<n && pos[j]-pos[i] <= k){\n                sum++;\n                j++;\n            }\n            if(sum >= max1){\n                maxIdx = i;\n                max1 = sum;\n                maxIdx2 = j;\n            }\n            while(i<n-1 && pos[i] == pos[i+1]){\n                i++;\n                sum--;\n            }\n            i++;\n            sum--;\n        }\n        //System.out.println(max1);\n        int[] npos = new int[n-(maxIdx2-maxIdx)];\n        int idx=0;\n        \n        for(i=0; i<maxIdx; i++) npos[idx++] = pos[i];\n        for(i=maxIdx2; i<n; i++) npos[idx++] = pos[i];\n        n = npos.length;\n        //System.out.println(npos[0]);\n        long max2=Long.MIN_VALUE;\n        i=0;\n        j=0;\n        sum=0;\n        while(i<n){\n            while(j<n && npos[j]-npos[i] <= k){\n                sum++;\n                j++;\n            }\n            if(sum >= max2){\n                max2 = sum;\n            }\n            while(i<n-1 && npos[i] == npos[i+1]){\n                i++;\n                sum--;\n            }\n            i++;\n            sum--;\n        }\n        \n        return (int)(max1+max2);\n        \n    }\n}
['Java']