Java | Array, TreeMap with Explanation
\n\nThe TreeMap data structure is employed to effectively sort and store indices corresponding to their respective values. Following this, the code assigns the 
![WhatsApp Image 2024-03-18 at 01.54.55_67902158.jpg](https://assets.leetcode.com/users/images/51ef8c10-1cc8-4b41-a6de-8139c16d12ef_1710707242.400418.jpeg)\n\nThe TreeMap data structure is employed to effectively sort and store indices corresponding to their respective values. Following this, the code assigns the values in the `value` array and the indices in the `check` array. Additionally, a variable, let\'s call it `idx`, is employed to iterate through the check array, serving the purpose of marking.\n\n# Code\n```\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] queries) {\n        int n = queries.length;\n        long sum=0;\n        long[] res = new long[n];\n        Set<Integer> set = new HashSet<>(); // for unmark indices check\n        Map<Integer, List<Integer>> map = new TreeMap<>(); // sort and store their indices\n        for(int i=0; i<nums.length; i++){\n            if(!map.containsKey(nums[i])){\n                map.put(nums[i], new ArrayList<>());\n            }\n            map.get(nums[i]).add(i);\n            sum += nums[i];\n        }\n        \n        int[] check = new int[nums.length]; // to store indices of sorted value\n        int[] value = new int[nums.length]; // to store value of indices respectively to indices\n\n        int idx = 0;\n        for(Integer key: map.keySet()){\n            for(int i=0; i<map.get(key).size(); i++){\n                value[idx] = key;\n                check[idx++] = map.get(key).get(i);\n            }\n        }\n        idx = 0;\n\n        for(int i=0; i<n; i++){\n            int x = queries[i][0];\n            int y = queries[i][1];\n\n            if(!set.contains(x)){\n                set.add(x);\n                sum -= nums[x];\n            }\n\n            for(int j=0; j<y; ){\n                if(!set.contains(check[idx])){\n                    set.add(check[idx]);\n                    sum -= value[idx++];\n                    j++;\n                    if(idx == nums.length) break;\n                }\n                else{\n                    idx++;\n                    if(idx == nums.length) break;\n                }\n            }\n            \n            res[i] = sum;\n            if(idx == nums.length) break;\n        }\n        return res;\n    }\n}\n\n\n```
['Array', 'Hash Table', 'Java']