I'm having an issue with a simple task it seems, but cannot figure it out. I believe the solution is within the code:
n = input().split(',')
list1 = []
list2 = []
for x in n:
list1.append(int(x))
for y in range(1, len(list1 1)):
if y not in list1:
list2.append(y)
print(list2)
The task is: Given an array of integers, some elements appear twice and others appear once. Each integer is in the range of [1, N], where N is the number of elements in the array.
Find all the integers of [1, N] inclusive that do NOT appear in this array.
Constrain: N will always be in the range of [5, 1000]
Input: 1,2,3,3,5 Output: 4 Input: 1,1,1,1,1,1,1,1 Output: 2,3,4,5,6,7,8
My idea is to have two empty arrays. The first one I will write all the numbers using a for loop. Once I have them all there I will use another loop where I can look and find the missing numbers. I guess it related to some formatting that is killing my logic. Any help would be appreciated! Thanks
CodePudding user response:
You only have to change this line:
for y in range(1, len(list1 1)):
to this one:
for y in range(1, len(list1) 1):
The problem is that you added one to the list, but you want to add 1 to the length of the list.