# Questions tagged [k-sum]

Given a set of numbers, a target sum, and some k, the problem of finding a subset of size k that has the target sum

42 questions
Filter by
Sorted by
Tagged with
83 views

### Two solutions to 2-sum

I have written a solution for two sum problem in variation. first is simple with two loops and another one is one loop and hash map. But I wonder why the simple solution is faster than the efficent ...
2k views

### 3Sum implementation

I have recently started to work through the problems on Leetcode, for the sake of bettering my own skills as well as preparing for interviews. I was faced with the 3Sum problem which is: Given an ...
277 views

### Given a vector and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum

I am trying to solve 4. Two Sum from this set of C++ interview questions. As far as I can think this solution has a time complexity of $O(n \log n)$ can it be further optimized? Write a function ...
46 views

### Leetcode 15 - 3 sum

This is a Leetcode problem - Given an array nums of n integers, are there elements $a, b, c$ in ...
36 views

### Printing all the combination of indexes for 2 sum exact outcome

Can somebody review the code and provide suggestions on where I can improve on the code like use of better data structure etc. Input: {1,7,2,8,3,6}, 9 Output: [0, 3] [1, 2] [4, 5] ...
102 views

### Given a list and a number k return whether any two numbers from the list add up to k

The task: Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. ...
98 views

### In an set of integers, find three elements summing to zero (3-sum, leetcode variant)

I use a dummy solution to solve the leetcode 3Sum problem. I employ the set data type to handle duplicates, then transform back to list. Given an array nums ...
356 views

### A binary search solution to 3Sum

I tried a binary solution to 3Sum problem in LeetCode: Given an array nums of $n$ integers, are there elements $a$, $b$, $c$ in ...
436 views

### Hash table solution to twoSum

I try the most to solve a twoSum problem in leetcode Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input ...
113 views

### Finding pairs that add up to a given sum, using recursion

I'm currently tasked with using recursion to find pairs that add up to a given sum. How would I make the function recursiveFixedSumPairs function more efficient and truncate this code? Also, what is ...
148 views

### Leetcode 3Sum problem using hashmap in Python

For practice, I solved Leetcode 15. 3Sum question: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives ...
103 views

### Contiguous elements of the list sum to K

The task: Given a list of integers and a number K, return which contiguous elements of the list sum to K. For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, ...
79 views

### Is the sum of 2 numbers in list k

I got a coding problem to solve and was wondering if I got a commonly acceptable and efficient/clean solution. If there is a better way I'd gladly hear it. given a list with numbers return if the ...
261 views

### Count pairs in list of integers such that their addition is equal to the input value

Given a List<int>, the problem I am trying to solve is: Find the number of unique pairs in List<int> such that their ...
155 views

### Two-sum solution in JavaScript [closed]

I was given this question during an interview. It is similar to two sum problems and I would like to ask for feedback for my solution. Merging 2 Packages Given a package with a weight limit ...
807 views

### Leetcode Two Sum code in Python

Here's my solution for the Leet Code's Two Sum problem -- would love feedback on (1) code efficiency and (2) style/formatting. Problem: Given an array of integers, return indices of the two ...
248 views

### Leetcode Three Sum in Python

Here's my solution for the Leet Code's Three Sum problem -- would love feedback on (1) code efficiency and (2) style/formatting. This is Python 3. Problem: Given an array nums of n integers, are ...
46 views

### Closest 3Sum in Scala

This question is taken from Leet code. Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three ...
110 views

### Find all combinations of 4 elements whose sum equals a target in Python

I wrote a solution to find all combinations of 4 elements from a list whose sum of elements equals some target Given an array nums of n integers and an integer target, are there elements a, b, c, ...
2k views

### If any two items in a list is equal to a given number

I just subscribed to the daily coding problems and received my first today. The problem is: Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For ...
2k views

### Find every possible pair of numbers that sum up to n number

I have this following code which find every possible pair of numbers that sum up to n number ...
54 views

### Check if list contains a pair which adds up to a given sum

Kindly review this scala code for given problem and suggest improvements. Problem - Given an array of integers and a target sum, check if array contains a pair which adds up to sum. Example - <...
75 views

### Check if list contains 3 elements with predefined sum

I have 2 solutions about puzzle I mentioned in title: solution 1( O(n^3) ): ...
92 views

### Determine a list contains two elements with a given sum

On interview I was asked to write method with following contract: boolean checkList(List<Long> list, long sum){...} for example it has to return true for ...
1k views

### Find pairs in an integer array whose sum is equal to 10

I came up with 2 different solutions in Java. First one is: ...
95 views

### Finding pairs of numbers with a given difference

Without changing the outputs or the inputs of the method, how would you make this code more readable ? I am interested in any general improvement it can be made in terms of readability without WORSEN ...
511 views

### Find all pairs in an array that sum to a given number without using HashMap

Find all pairs in an array that sum to a given number without using HashMap. Duplicate pairs are not allowed. Input array cannot be modified. input: ...
319 views

### Pairwise adding tuples in a list

I have two list of tuples: [[1,8],[2,7],[3,14]] [[1,5],[2,10],[3,14]] and a desired sum 20. I need to find two tuples from two list whose second element either ...
246 views

### Find the pairs of integers that sum to N

Can it be made faster, and I am a newbie to complexity but I'm pretty sure the average case for this would be O(n). Am I wrong? Output for the first list should be: (1,9), (2,8), (5,5), (-11,21) The ...
765 views

### Counting pairs that have a given difference in Java

You will be given an integer k and a list of integers. Count the number of distinct valid pair of integers (a,b) in the list for which a+k=b. For example, the array [1,1,1,2] has two different ...
412 views

### Python 3 two-sum performance

I just submitted a Python solution to the 'Two Sum' problem on LeetCode. The Problem Given an array of integers, return indices of the two numbers such that they add up to a specific target. ...
3k views

### Two Sum Leetcode

Looking for some feedback on the Two Sum LeetCode problem. Looking for feedback on code style in general, use of var, variable naming and initialization, return placement, and any other feedback or ...
761 views

### Leetcode 4sum problem using hashmaps

4sum problem Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. ...
2k views

### 3-Sum Problem in Python

I attempted the 3-Sum problem on Leetcode, where the problem asks to find all possible triplets of numbers in a given list such that their sum is 0. My code worked, but it exceeded the time limit for ...
2k views

### Leetcode 15. 3 Sum

Problem statement Given an array S of n integers, are there elements a, ...
5k views

### Finding indices of numbers that sum to a target

I've taken this algorithm question, which is to find the indexes of any two numbers in an array that sum to a given number. It is quite a simple problem to solve. But it is the execution time of my ...
2k views

### FindTwoSums using Tuple

I am writing a function that, given a list and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. If there are no such elements, the function ...
424 views

### Checking the sum of pairs to match a target value

I'm new to Python and curious what looks good and what doesn't. The function I have defined looks for pairs in a list of integers and tells the user whether or not any possible pair equals the target ...
1k views

### Find two values that add up to the sum

I was working on a practice Java exam online: Write a function that, given a list and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. ...
100 views

### Find sum of 3 that total a target from a List

A related Java question got me curios. All unique combinations (not permutations) of 3 values that sum to a target from a list of integers. Values can duplicate in the list but are only used once. ...