Back to course home
0% completed
Vote For New Content
More space saving solution, could be done with O(1) space
Sahil and Team
Nov 18, 2023
Could be done in more optimised way by storing total sum and calculating rsum & lsum at runtime using total sum. public int[] findDifferenceArray(int[] nums) { int n = nums.length; int[] differenceArray = new int[n]; // TODO: Write your code here int sum=0,lsum=0; for (int i : nums){ sum+=i; } for(int i=0;i<nums.length;i++){ int rsum = sum-nums[i]-lsum; differenceArray[i]=Math.abs(rsum-lsum); lsum+=nums[i]; } return differenceArray; }
4
0
Comments
Comments
L
luidymorais 2 years ago
I came up with the same solution at first place, I believe it's good to include it in Solution page to demonstrate how save more space.
M
Matthew Ibarra2 years ago
While this is better space, it's still O(N) space because your differenceArray is going to always be of size n. Technically, this solution and the answer provided by DesignGurus has the same space complexity because O(3n) (two extra arrays of size n plus result array o...
Tauseef Fayyaza year ago
It's good.
Tauseef Fayyaza year ago
It's good.
On this page