✅✅Range Sum Query - Mutable [JAVA Solution] ✅✅ || All test cases passed ✅✅
\u2714\uFE0F Approach Used : Sqrt Decomposition\n\nThe idea is to split the array in blocks with length of sqrt{n} \n Then we calculate the sum of each block an
\u2714\uFE0F **Approach Used : Sqrt Decomposition**\n\nThe idea is to split the array in blocks with length of sqrt{n} \n Then we calculate the sum of each block and store it in auxiliary memory b. To query RSQ(i, j), we will add the sums of all the blocks lying inside and those that partially overlap with range [i\u2026j].\n\n\u2714\uFE0F **Algorithm**\n![image](https://assets.leetcode.com/users/images/bd122204-abb5-4bc3-81ba-1df7637b694c_1624030085.8661282.png)\n\n\tIn the example above, the array nums\'s length is 9, which is split into blocks of size sqrt{9} \nTo get RSQ(1, 7) we add b[1]. It stores the sum of range [3, 5] and partially sums from block 0 and block 2, which are overlapping boundary blocks.\n\n\u2714\uFE0F **Java Solution :**\n```\nclass NumArray {\nprivate int[] b;\nprivate int len;\nprivate int[] nums;\n\npublic NumArray(int[] nums) {\n    this.nums = nums;\n    double l = Math.sqrt(nums.length);\n    len = (int) Math.ceil(nums.length/l);\n    b = new int [len];\n    for (int i = 0; i < nums.length; i++)\n        b[i / len] += nums[i];\n}\n\npublic int sumRange(int i, int j) {\n    int sum = 0;\n    int startBlock = i / len;\n    int endBlock = j / len;\n    if (startBlock == endBlock) {\n        for (int k = i; k <= j; k++)\n            sum += nums[k];\n    } else {\n        for (int k = i; k <= (startBlock + 1) * len - 1; k++)\n            sum += nums[k];\n        for (int k = startBlock + 1; k <= endBlock - 1; k++)\n            sum += b[k];\n        for (int k = endBlock * len; k <= j; k++)\n            sum += nums[k];\n    }\n    return sum;\n}\n\npublic void update(int i, int val) {\n    int b_l = i / len;\n    b[b_l] = b[b_l] - nums[i] + val;\n    nums[i] = val;\n}\n\n}\n\n```\n**Please Upvote if you like this! Have a nice day!**
['Java']