forked from USF-ISM6225/ISM6225_Fall24_Assignment_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFile1.cs
More file actions
42 lines (37 loc) · 981 Bytes
/
TextFile1.cs
File metadata and controls
42 lines (37 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
namespace Assignment_2
{
class Program
{
static void Main(string[] args)
{
// Question 1: Find Missing Numbers in Array
Console.WriteLine("Question 1:");
int[] nums1 = { 4, 3, 2, 7, 8, 2, 3, 1 };
IList<int> missingNumbers = FindMissingNumbers(nums1);
Console.WriteLine(string.Join(",", missingNumbers));
// Question 1: Find Missing Numbers in Array
public static IList<int> FindMissingNumbers(int[] nums)
{
List<int> missingNums = new List<int>();
int length = nums.Length;
// Mark the presence of numbers in the array
for (int i = 0; i < length; i++)
{
int index = Math.Abs(nums[i]) - 1;
if (nums[index] > 0)
{
nums[index] = -nums[index];
}
}
// Find the missing numbers by checking positive indices
for (int i = 0; i < length; i++)
{
if (nums[i] > 0)
{
missingNums.Add(i + 1);
}
}
return missingNums;
}