Find first missing positive

From Rosetta Code
Revision as of 17:35, 23 February 2021 by CalmoSoft (talk | contribs) (Created page with "{{Draft task}} Let given an unsorted integer array nums. The goal is find the smallest missing positive integer. <br> nums = [1,2,0],[3,4,-1,1],[7,8,9,11,12] =={{header|Ring}}...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Find first missing positive is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Let given an unsorted integer array nums. The goal is find the smallest missing positive integer.
nums = [1,2,0],[3,4,-1,1],[7,8,9,11,12]

Ring

<lang ring> nums = [[1,2,0],[3,4,-1,1],[7,8,9,11,12]] numnr = list(3) numnr[1] = "[1,2,0]" numnr[2] = "[3,4,-1,1]" numnr[3] = "[7,8,9,11,12]"

for n = 1 to len(nums)

   for m = 1 to len(nums[n])
       if nums[n][m] < 1
          del(nums[n],m)
       ok
   next
   sortNums = sort(nums[n])
   ln = len(sortNums)
   num = sortNums[ln]   
   for m = 1 to num + 1
       ind = find(nums[n],m)
       if ind < 1
          see "the smallest missing positive integer for " + numnr[n] + ": " + m + nl
          exit
       ok
   next

next </lang>

Output:
the smallest missing positive integer for [1,2,0]: 3
the smallest missing positive integer for [3,4,-1,1]: 2
the smallest missing positive integer for [7,8,9,11,12]: 1