How do you find all pairs of an integer array whose sum is equal to a given number?

I don’t know what you mean…

local numbers = {3,6,4,2,1,5,2,5,5,5}

function sumwewant(array, sum)
	local matches = {}
	local alreadyMatched = {}
	for i = 1, #array do
		local first = array[i]
		for j = 1, #array do
			local second = numbers[j]
			if i ~= j and not table.find(alreadyMatched, first) and not table.find(alreadyMatched, second) then
				if ((first + second) == sum)then
					table.insert(matches, {first, second})
					table.insert(alreadyMatched, first)
					table.insert(alreadyMatched, second)
				end
			end
		end
	end
	return matches
end

local d  = sumwewant(numbers, 10)
print(d)

image

1 Like

thats why u check if its matching the same index not the same number.

Oh actually I solved it too,

local numbers = {3,6,4,5,2,1,5,2}


for i = 1, #numbers do
	local first = numbers[i]
	for j = i, #numbers do
		local second = numbers[j]
		if ((first + second) == 10)then
			print(first, second)
			break
		end
	end

Again I changed j = i

I just needed to make for each of j = i numbers. but anyways your script is still better and yea.

But actually you script is really long I dont know if that is okey with the script speed?

I don’t think it should be a problem unless you’re comparing like 1000+ numbers constantly

1 Like

To be a little more exact, i should only count to the second to last element. j = i + 1 is so that the indices are not equal.

function sumwewant(array, sum)
	local tab = {}
	for i = 1, #array - 1 do
		local first = array[i]
		for j = i + 1, #array do
			local second = numbers[j]
			if ((first + second) == sum)then
				table.insert(tab, {first, second})
			end
		end
	end
	return tab
end

But your method will work anyway because the second for loop will not execute that extra cycle because it will be out of range.

If you want it to have no duplicates just apply a filter before inserting a pair in the result.

local numbers = {3,6,4,2,1,5,2,5,5,5}

function sumwewant(array, sum)
	local tab = {}
	for i = 1, #array - 1 do
		local first = array[i]
		for j = i + 1, #array do
			local second = numbers[j]
			if ((first + second) == sum)then
				local notInTab = true
				for k = 1, #tab do
					if  tab[k][1] == first and tab[k][2] == second then
						notInTab = false
						break
					end
				end
				if notInTab then
					table.insert(tab, {first, second})
				end
			end
		end
	end
	return tab
end

local d  = sumwewant(numbers, 10)
print(d)

Result:
{
[1] = ▼ {
[1] = 6,
[2] = 4
},
[2] = ▼ {
[1] = 5,
[2] = 5
}
}

1 Like

Actually it isnt a dublicate. It just print the Output {5,5} 2times so it doesnt mean there exist 2 dublicates because it just print the sameoutput 2 times. so there woudnt be a problem for my script and the things I want to create. Dublicate would be if it does print the output 2 times like 5,5 and then 5,5. I could easily check if it does print 1 times and the problem is solved.

This is what you actually mean!
image

This is what my script does, as you can see it print 2 times the same and it isnt a big problem,

image

I dont know why you post again something that was solved by @heII_ish but anyways my script is solved 2 times.

I got the impression that you wanted something more efficient, so I posted my answer. If you examine it closely you will notice that the changes, although subtle, are significant and, from my point of view, it is the right way to go.

Oh yes when I think about it, then thats true!

Yes then well done! :smiley: