Dont flag this post there is not much to say about:
This is what I created untill now but I cant find the missing number how can I find the missing number of the Table?
local numbers = {1,2,3,4,5,6,7,9}
function getMissingNumber(a, b)
local total = (b +1) * (b + 2)/2;
for i = 1, #a do
total -= a[i]
end
return total
end
local miss = getMissingNumber(numbers, 5)
print(miss)
So we want find the number 8 that is missing in our table and this is just possible with the formula n (n+1)/2*
I don’t know if this is what you are looking for, do you want to find all the missing numbers in a table?
local numbers = {1,2,3,4,5,6,7,9}
function getMissingNumber(theTable, minNumber, maxNumber)
local MissingTable = {}
for i = minNumber, maxNumber do
if not theTable[i] then
table.insert(MissingTable, i)
end
end
return MissingTable
end
local miss = getMissingNumber(numbers, 1, 10)
print(miss)
--first parameter Table, second is minimum, third is maximum
but when I change the table, the script should figure out how much is missing each time when I change the table and thats just working with the formula:
btw I didnt solved it just understand a forum about it.
so for the table we need everytime the length of the array and that is n-1 so we do actually everytime with the second number under the max table value.
local numbers = {1,2,3,5,6,7,8,9}
function getMissingNumber(a, b)
local total = (b +1) * (b + 2)/2;
for i = 1, #a do
total -= a[i]
end
return total
end
local miss = getMissingNumber(numbers, 8)
print(miss)
----output = 4
I’ve just found out they added these operators in roblox lua, but it is not a standard. If you try it in another lua interpreter, -= and += won’t work.
@Soflowsen please think before you write. And please delete your text above because of your text people get just a misconception about roblox lua operators. And nobody talked about other languages we are still in this devforum so dont switch over languages, Thanks!!
I know you’ve found a solution, but personally I wouldn’t use an algorithm. I just feel it’s easier and more convenient to work with some scenario logic given the way you’re currently doing it is a bit hacky and may not always evaluate correctly. There’s also factors such as negative numbers where this method may or may not give the appropriate results.
Assuming that each missing number is exactly 1 whole number away, you can take advantage of this by adding 1 to each number, and seeing if it returns the next number. If it doesn’t you have a missing number. By no means am I saying it’s bad to work with a formula or that you’re incorrectly solving the problem, just wanted to show how you can take advantage of a given scenario, by finding something common
local numbers = {1,2,3,5,6,7,8,9}
local max, min = math.max(unpack(numbers)), math.min(unpack(numbers))
function getMissingNumber(theTable, minNumber, maxNumber)
for i, v in ipairs(numbers) do
if i < #numbers and v > 0 and numbers[i + 1] ~= v + 1 then -- Positive integers
return v + 1
elseif i < #numbers and v < 0 and numbers[i + 1] ~= v + 1 then -- Negative integers
return v - 1
end
end
end
getMissingNumber(numbers, min, max)