classSolution { funfourSum(nums: IntArray, target: Int): List<List<Int>> { val ans=ArrayList<List<Int>>() nums.sort() for(i in nums.indices){ if(i>0&&nums[i]==nums[i-1])continue for (j in i+1 until nums.size){ if(j>0&&j-1!=i&&nums[j]==nums[j-1])continue if(j==i)continue var left=j+1 var right=nums.size-1 var sum:Long while (left<right){ sum= nums[i].toLong()+nums[j].toLong()+nums[left].toLong()+nums[right].toLong() if(sum<target||left==i||left==j){ left++ }elseif(sum>target||right==i||right==j){ right-- }else{ ans.add(listOf(nums[i],nums[j],nums[left],nums[right])) left++ while (left<right&&nums[left]==nums[left-1]){ left++ } right-- while (left<right&&nums[right]==nums[right+1]){ right-- } } } } } return ans } }