We’re rewarding the question askers & reputations are being recalculated! Read more.

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

Filter by
Sorted by
Tagged with
16
votes
4answers
2k views

Leetcode 15. 3 Sum

Problem statement Given an array S of n integers, are there elements a, ...
14
votes
1answer
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 ...
13
votes
3answers
2k 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 ...
10
votes
3answers
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 ...
9
votes
6answers
3k 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 ...
9
votes
2answers
526 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 ...
8
votes
4answers
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 ...
8
votes
2answers
547 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. ...
7
votes
3answers
4k 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 ...
6
votes
2answers
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 ...
6
votes
1answer
342 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 ...
5
votes
1answer
290 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 ...
5
votes
2answers
462 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 ...
5
votes
2answers
103 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 ...
5
votes
2answers
994 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. ...
4
votes
2answers
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. ...
3
votes
1answer
398 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 ...
3
votes
2answers
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 ...
3
votes
1answer
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: ...
3
votes
2answers
1k 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 ...
3
votes
2answers
361 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 ...
2
votes
2answers
82 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 ...
2
votes
3answers
153 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, ...
2
votes
1answer
559 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 ...
2
votes
1answer
170 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, ...
2
votes
1answer
340 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 ...
2
votes
1answer
101 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. ...
2
votes
3answers
179 views

Return all pairs from a list of integers that contributes to a given sum

Given a list of integers return all the pairs that makes up to a given target sum. input: {2, 7, 11, 15, -2} target sum: ...
2
votes
0answers
57 views

Solution to 3Sum problem on Leetcode using HashTable

The problem of 3sum is described as: 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 the sum of zero....
2
votes
1answer
42 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] ...
1
vote
3answers
95 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 ...
1
vote
2answers
59 views

Leetcode 15 - 3 sum

This is a Leetcode problem - Given an array nums of n integers, are there elements \$a, b, c\$ in ...
1
vote
1answer
136 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. ...
1
vote
1answer
105 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 ...
1
vote
1answer
290 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 ...
1
vote
1answer
255 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 ...
1
vote
1answer
664 views

Algorithm that finds two numbers that sum to a target

Is this the optimal algorithm to satisfy this problem? It does work. I get the: 1 and 5 (or 5 and 1) as 1 + 9 = 10. Here's a function that, when passed a list and a target sum, returns two distinct ...
1
vote
5answers
530 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: ...
1
vote
1answer
124 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 ...
1
vote
1answer
55 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 - <...
1
vote
0answers
56 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 ...
1
vote
0answers
77 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) ): ...
-1
votes
2answers
260 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 ...