Simple Solution using Arrays without sets 🌐✨
Intuition\nSince the constraints guarantee a range from -1000 to 1000, I decided to use a vector with an offset of 2001 to cleverly represent these values.\n\n#
# Intuition\nSince the constraints guarantee a range from -1000 to 1000, I decided to use a vector with an offset of 2001 to cleverly represent these values.\n\n# Approach\nTo tackle the problem, I decided to use this vector as a mark to indicate the presence of a particular value. I traversed both `nums1` and `nums2` to mark the values encountered by setting the corresponding index in the vector to 1. After marking, I checked for unique values in each array. If a value was found to be unique in either array, I added it to the answer vector and marked it as used.\n\nI also threw in a bit of humor by mentioning the "people using each other" metaphor when marking the values as -1 after they were added to the result vectors.\n\n# Complexity\n- Time complexity: $$O(n)$$, where $$n$$ is the size of the larger array among `nums1` and `nums2`. The loops to mark and find unique values contribute to this linear time complexity.\n- Space complexity: $$O(1)$$, considering the vector size is constant (2001). The extra space used is reasonable and does not scale with the input size, thanks to the clever indexing.\n# Contribution\n\n- I only wrote the code in c++ all other language codes are generated by `chatgpt`.\n- It also helped me in generating the explanation from my broken english.\n- Thanks ...................................................\n# Code\n``` cpp []\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n       nums1.reserve(1e4);  // just to avoid overflow\n       nums2.reserve(1e4);  // you can also reserve like 1000 + 2001 = 3001\n        \n        // By seeing constraints we can confirm the least value that we will get is -1000 .\n        // and the max we will get is 1000.\n        // so to reprent -1000 on a vector index we have to add an offset of 2001\n        // mark nums[nums[0]] =1 for value we have encountered.\n        // after that in both arrays index check if the value if present only in one array\n        // then push to the answer vector.\n\n       for(int  i =0;i<nums1.size();i++){\n           nums1[nums1[i]+2001] = 1; \n       }\n\n       \n       for(int  i =0;i<nums2.size();i++){\n           nums2[nums2[i]+2001] = 1; \n       }\n\n       vector<int> first;\n       vector<int> sec;\n       for(int  i =0;i<nums1.size();i++){\n           if(nums1[nums1[i]+2001] == 1 and nums2[nums1[i]+2001] !=1) {\n               first.push_back(nums1[i]);\n               nums1[nums1[i]+2001] = -1; // indicating we have used this value like people use each other \n           }\n       }\n       for(int  i =0;i<nums2.size();i++){\n           if(nums2[nums2[i]+2001] == 1 and nums1[nums2[i]+2001] !=1) {\n               sec.push_back(nums2[i]);\n               nums2[nums2[i] +2001] =-1; \n           }\n       }\n       return {first,sec};\n    }\n};\n```\n``` java []\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Solution {\n    public List<List<Integer>> findDifference(List<Integer> nums1, List<Integer> nums2) {\n        List<List<Integer>> result = new ArrayList<>();\n        List<Integer> first = new ArrayList<>();\n        List<Integer> sec = new ArrayList<>();\n        \n        for (int i = 0; i < 1e4; i++) {\n            nums1.add(0);\n            nums2.add(0);\n        }\n\n        for (int i = 0; i < nums1.size(); i++) {\n            nums1.set(nums1.get(i) + 2001, 1);\n        }\n\n        for (int i = 0; i < nums2.size(); i++) {\n            nums2.set(nums2.get(i) + 2001, 1);\n        }\n\n        for (int i = 0; i < nums1.size(); i++) {\n            if (nums1.get(nums1.get(i) + 2001) == 1 && nums2.get(nums1.get(i) + 2001) != 1) {\n                first.add(nums1.get(i));\n                nums1.set(nums1.get(i) + 2001, -1);\n            }\n        }\n\n        for (int i = 0; i < nums2.size(); i++) {\n            if (nums2.get(nums2.get(i) + 2001) == 1 && nums1.get(nums2.get(i) + 2001) != 1) {\n                sec.add(nums2.get(i));\n                nums2.set(nums2.get(i) + 2001, -1);\n            }\n        }\n\n        result.add(first);\n        result.add(sec);\n        return result;\n    }\n}\n\n\n```\n``` c []\n#include <stdio.h>\n#include <stdlib.h>\n\nstruct Solution {\n    int* findDifference(int* nums1, int* nums2, int nums1Size, int nums2Size) {\n        int* result = (int*)malloc(2 * sizeof(int*));\n        int* first = (int*)malloc(nums1Size * sizeof(int*));\n        int* sec = (int*)malloc(nums2Size * sizeof(int*));\n\n        for (int i = 0; i < 1e4; i++) {\n            nums1 = realloc(nums1, (nums1Size + i) * sizeof(int));\n            nums2 = realloc(nums2, (nums2Size + i) * sizeof(int));\n        }\n\n        for (int i = 0; i < nums1Size; i++) {\n            nums1[nums1[i] + 2001] = 1;\n        }\n\n        for (int i = 0; i < nums2Size; i++) {\n            nums2[nums2[i] + 2001] = 1;\n        }\n\n        int firstIndex = 0;\n        int secIndex = 0;\n\n        for (int i = 0; i < nums1Size; i++) {\n            if (nums1[nums1[i] + 2001] == 1 && nums2[nums1[i] + 2001] != 1) {\n                first[firstIndex++] = nums1[i];\n                nums1[nums1[i] + 2001] = -1;\n            }\n        }\n\n        for (int i = 0; i < nums2Size; i++) {\n            if (nums2[nums2[i] + 2001] == 1 && nums1[nums2[i] + 2001] != 1) {\n                sec[secIndex++] = nums2[i];\n                nums2[nums2[i] + 2001] = -1;\n            }\n        }\n\n        result[0] = first;\n        result[1] = sec;\n        return result;\n    }\n};\n\n```\n``` javascript []\nclass Solution {\n    findDifference(nums1, nums2) {\n        nums1.length = 1e4;\n        nums2.length = 1e4;\n        const result = [[], []];\n\n        for (let i = 0; i < nums1.length; i++) {\n            nums1[nums1[i] + 2001] = 1;\n        }\n\n        for (let i = 0; i < nums2.length; i++) {\n            nums2[nums2[i] + 2001] = 1;\n        }\n\n        for (let i = 0; i < nums1.length; i++) {\n            if (nums1[nums1[i] + 2001] === 1 && nums2[nums1[i] + 2001] !== 1) {\n                result[0].push(nums1[i]);\n                nums1[nums1[i] + 2001] = -1;\n            }\n        }\n\n        for (let i = 0; i < nums2.length; i++) {\n            if (nums2[nums2[i] + 2001] === 1 && nums1[nums2[i] + 2001] !== 1) {\n                result[1].push(nums2[i]);\n                nums2[nums2[i] + 2001] = -1;\n            }\n        }\n\n        return result;\n    }\n}\n\n```\n\n``` python []\nclass Solution:\n    def findDifference(self, nums1, nums2):\n        nums1 += [0] * int(1e4)\n        nums2 += [0] * int(1e4)\n        result = [[], []]\n\n        for i in range(len(nums1)):\n            nums1[nums1[i] + 2001] = 1\n\n        for i in range(len(nums2)):\n            nums2[nums2[i] + 2001] = 1\n\n        for i in range(len(nums1)):\n            if nums1[nums1[i] + 2001] == 1 and nums2[nums1[i] + 2001] != 1:\n                result[0].append(nums1[i])\n                nums1[nums1[i] + 2001] = -1\n\n        for i in range(len(nums2)):\n            if nums2[nums2[i] + 2001] == 1 and nums1[nums2[i] + 2001] != 1:\n                result[1].append(nums2[i])\n                nums2[nums2[i] + 2001] = -1\n\n        return result\n\n```\n``` Go []\npackage main\n\nimport "fmt"\n\ntype Solution struct{}\n\nfunc (sol *Solution) findDifference(nums1 []int, nums2 []int) [][]int {\n\tnums1 = append(nums1, make([]int, int(1e4))...)\n\tnums2 = append(nums2, make([]int, int(1e4))...)\n\tresult := [][]int{[]int{}, []int{}}\n\n\tfor i := 0; i < len(nums1); i++ {\n\t\tnums1[nums1[i]+2001] = 1\n\t}\n\n\tfor i := 0; i < len(nums2); i++ {\n\t\tnums2[nums2[i]+2001] = 1\n\t}\n\n\tfor i := 0; i < len(nums1); i++ {\n\t\tif nums1[nums1[i]+2001] == 1 && nums2[nums1[i]+2001] != 1 {\n\t\t\tresult[0] = append(result[0], nums1[i])\n\t\t\tnums1[nums1[i]+2001] = -1\n\t\t}\n\t}\n\n\tfor i := 0; i < len(nums2); i++ {\n\t\tif nums2[nums2[i]+2001] == 1 && nums1[nums2[i]+2001] != 1 {\n\t\t\tresult[1] = append(result[1], nums2[i])\n\t\t\tnums2[nums2[i]+2001] = -1\n\t\t}\n\t}\n\n\treturn result\n}\n\nfunc main() {\n\tsol := Solution{}\n\tnums1 := []int{1, 2, 3}\n\tnums2 := []int{2, 3, 4}\n\tresult := sol.findDifference(nums1, nums2)\n\tfmt.Println(result)\n}\n\n```\n\n![e9a5leu8hmhb1.jpg](https://assets.leetcode.com/users/images/d7c7cc4a-d50e-4955-99f7-29f948189864_1706613733.347077.jpeg)\n![nn5o2llpphha1.webp](https://assets.leetcode.com/users/images/2b8cc7b1-766e-438b-b55d-f9826cf2003b_1706613756.2279892.webp)\n
['Array', 'C', 'Python', 'C++', 'Java', 'Go', 'JavaScript']