private static int findLastSum(Pair[] sortedSums, long key, int i) {
        int l = 0;
        int r = sortedSums.length - 1;

        while (l <= r) {
            int m = l + (r - l) / 2;
            Pair mp = sortedSums[m];
            if (mp.indx < i - 1 && mp.sum == key && (m == sortedSums.length - 1 || sortedSums[m + 1].indx >= i - 1 || sortedSums[m + 1].sum != key)) {
                return m;
            }

            if (mp.sum > key || (mp.sum == key && mp.indx >= i - 1)) {
                r = m - 1;
            } else {
                l = m + 1;
            }
        }

        return -1;
    }