Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to find missing elements of a given range in array of distinct elements with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
	public static void printMissingElements(int[] arr, int range_start, int range_end)
	{
		HashSet<int> hset = new HashSet<int>();
		for (int i = 0; i < arr.Length; i++) {
			hset.Add(arr[i]);
		}

		for (int i = range_start; i <= range_end; i++) {
			if (!hset.Contains(i)) {
				Console.Write(i + " ");
			}
		}
	}
	public static void Main(string[] args)
	{
		int[] arr = new int[] {2, 4, 5, 7, 9};
		int range_start = 1, range_end = 9;

		printMissingElements(arr, range_start, range_end);
	}
}




/*
run:
 
1 3 6 8 
 
*/

 



answered May 23, 2023 by avibootz
...