Skip to main content

Find all missing elements of a range

Here given code implementation process.

// C++ Program 
// Find all missing elements of a range
#include <iostream>
#include <set>
using namespace std;
// This function are displaying given array elements
void display(int arr[], int size)
{
    cout << " Array : ";
    for (int i = 0; i < size; ++i)
    {
        cout << " " << arr[i];
    }
    cout << endl;
}
// Find the missing elements of an array of within of given range
void missing_element(int arr[], int size, int low, int high)
{
    if (low > high)
    {
        // When given range are not valid 
        missing_element(arr,size,high,low);
    }
    else
    {
        // Define a empty set
        set <int> st;

        int count = 0;

        display(arr, size);

        // Execute loop through by size
        for (int i = 0; i < size; ++i)
        {
            // Check that whether get array [i] element is part of given range
            if (arr[i] >= low && arr[i] <= high)
            {
                // Add element which is exist within given range in an array
                st.insert(arr[i]);
            }
        }
        cout<<" Missing element in range of ("<<low <<","<<high<<") is "<<endl;
        // Display missing element between the range
        for (int i = low; i <= high; ++i)
        {
            if (st.find(i) == st.end())
            {
                // When elements are missing
                cout << "  " << i;
                // Increase the count value by one
                count ++;
            }
        }
        cout <<"\n Total Missing : ";
        if(count==0)
        {
            cout<<" None "<<endl;
        }
        else
        {
            cout << count <<endl;  
        }
        cout <<endl;
           
    }

}
int main()
{
    // Array element
    int arr[] = {
        1 , 9 , 2 , 4 , 5 , 7 , 10
    };
    // Get the size of array
    int size = sizeof(arr) / sizeof(arr[0]);
    missing_element(arr, size, 1, 10);
    // Array element
    int arr2[] = {
        1 , 31 , 74 , 12 , 4 , 71
    };
    // Get the size of array
    size = sizeof(arr2) / sizeof(arr2[0]);
    missing_element(arr2, size, 70, 75);
    return 0;
}

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
  3  6  8
 Total Missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
  70  72  73  75
 Total Missing : 4
/*
   Java program
   Find all missing elements of a range
*/
import java.util.HashSet;
public class Detection
{

    // This function are displaying given array elements
    public void display(int []arr, int size)
    {
        System.out.print(" Array : ");
        for (int i = 0; i < size; ++i)
        {
             System.out.print(" " + arr[i]);
        }
        System.out.print("\n");
    }
    // Find the missing elements of an array of within of given range
    public void missingElement(int[] arr, int size, int low, int high)
    {
        if (low > high)
        {
            // When given range are not valid 
            this.missingElement(arr, size, high, low);
        }
        else
        {
            // Create a empty Set
            HashSet < Integer > data = new HashSet < > ();
            int count = 0;
            this.display(arr, size);
            // Execute loop through by size
            for (int i = 0; i < size; ++i)
            {
                // Check that whether get array [i] element is part of given range
                if (arr[i] >= low && arr[i] <= high)
                {
                    if (data.contains(arr[i]) == false)
                    {
                        // Add element which is exist within given range in an array
                        data.add(arr[i]);
                    }
                }
            }
            // Display the given range
            System.out.print(" Missing element in range of (" + low + "," + high + ") is \n");
            // Display missing element between the range
            for (int i = low; i <= high; ++i)
            {
                if (data.contains(i) == false)
                {
                    // When elements are missing
                    System.out.print(" " + i);
                    // Increase the count value by one
                    count++;
                }
            }
            System.out.print("\n Total Missing : ");
            if (count == 0)
            {
                System.out.print(" None \n\n");
            }
            else
            {
                System.out.print(count + "\n\n");
            }
        }
    }
    public static void main(String[] args)
    {
        Detection task = new Detection();
        // Define array of integer element
        int[] arr1 = {
            1 , 9 , 2 , 4 , 5 , 7 , 10
        };
        // Define another Array element
        int[] arr2 = {
            1 , 31 , 74 , 12 , 4 , 71
        };
        // Get the size of arr1
        int size = arr1.length;
        task.missingElement(arr1, size, 1, 10);
        // Get the size of arr2
        size = arr2.length;
        task.missingElement(arr2, size, 70, 75);
    }
}

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total Missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total Missing : 4
// Include namespace system
using System;
using System.Collections.Generic;
public class Detection
{
	// This function are displaying given array elements
	public void display(int[] arr, int size)
	{
		Console.Write(" Array : ");
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	// Find the missing elements of an array of within of given range
	public void missingElement(int[] arr, int size, int low, int high)
	{
		if (low > high)
		{
			// When given range are not valid
			this.missingElement(arr, size, high, low);
		}
		else
		{
			// Create a empty Set
			HashSet <int> data = new HashSet <int> ();
			int count = 0;
			this.display(arr, size);
			// Execute loop through by size
			for (int i = 0; i < size; ++i)
			{
				// Check that whether get array [i] element is part of given range
				if (arr[i] >= low && arr[i] <= high)
				{
					if (data.Contains(arr[i]) == false)
					{
						// Add element which is exist within given range in an array
						data.Add(arr[i]);
					}
				}
			}
			// Display the given range
			Console.Write(" Missing element in range of (" + low + "," + high + ") is \n");
			// Display missing element between the range
			for (int i = low; i <= high; ++i)
			{
				if (data.Contains(i) == false)
				{
					// Increase the count value by one
					// When elements are missing
					Console.Write(" " + i);
					count++;
				}
			}
			Console.Write("\n Total missing : ");
			if (count == 0)
			{
				Console.Write(" None \n\n");
			}
			else
			{
				Console.Write(count + "\n\n");
			}
		}
	}
	public static void Main(String[] args)
	{
		Detection task = new Detection();
		// Define array of integer element
		int[] arr1 = {
			1 , 9 , 2 , 4 , 5 , 7 , 10
		};
		// Define another Array element
		int[] arr2 = {
			1 , 31 , 74 , 12 , 4 , 71
		};
		// Get the size of arr1
		int size = arr1.Length;
		task.missingElement(arr1, size, 1, 10);
		// Get the size of arr2
		size = arr2.Length;
		task.missingElement(arr2, size, 70, 75);
	}
}

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total missing : 4
<?php
class Detection
{
	// This function are displaying given array elements
	public	function display( & $arr, $size)
	{
		echo " Array : ";
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	// Find the missing elements of an array of within of given range
	public	function missingElement( & $arr, $size, $low, $high)
	{
		if ($low > $high)
		{
			// When given range are not valid
			$this->missingElement($arr, $size, $high, $low);
		}
		else
		{
			// Create a empty array
			$data = array();
			$count = 0;
			$this->display($arr, $size);
			// Execute loop through by size
			for ($i = 0; $i < $size; ++$i)
			{
				// Check that whether get array [i] element is part of given range
				if ($arr[$i] >= $low && $arr[$i] <= $high)
				{
					if (array_key_exists($arr[$i],$data) == false)
					{
						// Add element which is exist within given range in an array
						array_push($data,$arr[$i]);
					}
				}
			}
			// Display the given range
			echo " Missing element in range of (". $low .",". $high .") is \n";
			// Display missing element between the range
			for ($i = $low; $i <= $high; ++$i)
			{
				if (in_array($i,$data) == false)
				{
					// Increase the count value by one
					// When elements are missing
					echo " ". $i;
					$count++;
				}
			}
			echo "\n Total missing : ";
			if ($count == 0)
			{
				echo " None \n\n";
			}
			else
			{
				echo $count ."\n\n";
			}
		}
	}
}

function main()
{
	$task = new Detection();
	// Define array of integer element
	$arr1 = array(1, 9, 2, 4, 5, 7, 10);
	// Define another Array element
	$arr2 = array(1, 31, 74, 12, 4, 71);
	// Get the size of arr1
	$size = count($arr1);
	$task->missingElement($arr1, $size, 1, 10);
	// Get the size of arr2
	$size = count($arr2);
	$task->missingElement($arr2, $size, 70, 75);
}
main();

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total missing : 4
// Node js Program 
// Find all missing elements of a range

class Detection
{
	// This function are displaying given array elements
	display(arr, size)
	{
		process.stdout.write(" Array : ");
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Find the missing elements of an array of within of given range
	missingElement(arr, size, low, high)
	{
		if (low > high)
		{
			// When given range are not valid
			this.missingElement(arr, size, high, low);
		}
		else
		{
			// Create a empty Set
			var data = new Set();
			var count = 0;
			this.display(arr, size);
			// Execute loop through by size
			for (var i = 0; i < size; ++i)
			{
				// Check that whether get array [i] element is part of given range
				if (arr[i] >= low && arr[i] <= high)
				{
					if (data.has(arr[i]) == false)
					{
						// Add element which is exist within given range in an array
						data.add(arr[i]);
					}
				}
			}
			// Display the given range
			process.stdout.write(" Missing element in range of (" + low + "," + high + ") is \n");
			// Display missing element between the range
			for (var i = low; i <= high; ++i)
			{
				if (data.has(i) == false)
				{
					// Increase the count value by one
					// When elements are missing
					process.stdout.write(" " + i);
					count++;
				}
			}
			process.stdout.write("\n Total missing : ");
			if (count == 0)
			{
				process.stdout.write(" None \n\n");
			}
			else
			{
				process.stdout.write(count + "\n\n");
			}
		}
	}
}

function main()
{
	var task = new Detection();
	// Define array of integer element
	var arr1 = [1, 9, 2, 4, 5, 7, 10];
	// Define another Array element
	var arr2 = [1, 31, 74, 12, 4, 71];
	// Get the size of arr1
	var size = arr1.length;
	task.missingElement(arr1, size, 1, 10);
	// Get the size of arr2
	size = arr2.length;
	task.missingElement(arr2, size, 70, 75);
}
main();

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total missing : 4
#    Python 3 program
#    Find all missing elements of a range

class Detection :
	#  This function are displaying given list elements
	def display(self, arr, size) :
		print(" Array : ", end = "")
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Find the missing elements of an list of within of given range
	def missingElement(self, arr, size, low, high) :
		if (low > high) :
			#  When given range are not valid
			self.missingElement(arr, size, high, low)
		else :
			#  Create a empty Set
			data = set()
			count = 0
			self.display(arr, size)
			#  Execute loop through by size
			i = 0
			while (i < size) :
				#  Check that whether get list [i] element is part of given range
				if (arr[i] >= low and arr[i] <= high) :
					if ((arr[i] not in data)) :
						#  Add element which is exist within given range in an list
						data.add(arr[i])
					
				
				i += 1
			
			#  Display the given range
			print(" Missing element in range of (", low ,",", high ,") is ")
			#  Display missing element between the range
			i = low
			while (i <= high) :
				if ( (i not in data)) :
					#  Increase the count value by one
					#  When elements are missing
					print(" ", i, end = "")
					count += 1
				
				i += 1
			
			print("\n Total missing : ", end = "")
			if (count == 0) :
				print(" None \n")
			else :
				print(count ,"\n")
			
		
	

def main() :
	task = Detection()
	#  Define list of integer element
	arr1 = [1, 9, 2, 4, 5, 7, 10]
	#  Define another Array element
	arr2 = [1, 31, 74, 12, 4, 71]
	#  Get the size of arr1
	size = len(arr1)
	task.missingElement(arr1, size, 1, 10)
	#  Get the size of arr2
	size = len(arr2)
	task.missingElement(arr2, size, 70, 75)

if __name__ == "__main__": main()

Output

 Array :   1  9  2  4  5  7  10
 Missing element in range of ( 1 , 10 ) is
  3  6  8
 Total missing : 3

 Array :   1  31  74  12  4  71
 Missing element in range of ( 70 , 75 ) is
  70  72  73  75
 Total missing : 4
#    Ruby program
#    Find all missing elements of a range

class Detection 
	#  This function are displaying given array elements
	def display(arr, size) 
		print(" Array : ")
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Find the missing elements of an array of within of given range
	def missingElement(arr, size, low, high) 
		if (low > high) 
			#  When given range are not valid
			self.missingElement(arr, size, high, low)
		else 
			#  Create a empty Set
			data  = []
			count = 0
			self.display(arr, size)
			#  Execute loop through by size
			i = 0
			while (i < size) 
				#  Check that whether get array [i] element is part of given range
				if (arr[i] >= low && arr[i] <= high) 
					if (data.include?(arr[i]) == false) 
						#  Add element which is exist within given range in an array
						data.push(arr[i])
					end

				end

				i += 1
			end

			#  Display the given range
			print(" Missing element in range of (", low ,",", high ,") is \n")
			#  Display missing element between the range
			i = low
			while (i <= high) 
				if (data.include?(i) == false) 
					#  Increase the count value by one
					#  When elements are missing
					print(" ", i)
					count += 1
				end

				i += 1
			end

			print("\n Total missing : ")
			if (count == 0) 
				print(" None \n\n")
			else 
				print(count ,"\n\n")
			end

		end

	end

end

def main() 
	task = Detection.new()
	#  Define array of integer element
	arr1 = [1, 9, 2, 4, 5, 7, 10]
	#  Define another Array element
	arr2 = [1, 31, 74, 12, 4, 71]
	#  Get the size of arr1
	size = arr1.length
	task.missingElement(arr1, size, 1, 10)
	#  Get the size of arr2
	size = arr2.length
	task.missingElement(arr2, size, 70, 75)
end

main()

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is 
 3 6 8
 Total missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is 
 70 72 73 75
 Total missing : 4

import scala.collection.mutable._ ;
/*
   Scala program
   Find all missing elements of a range
*/
class Detection
{
	// This function are displaying given array elements
	def display(arr: Array[Int], size: Int): Unit = {
		print(" Array : ");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Find the missing elements of an array of within of given range
	def missingElement(arr: Array[Int], size: Int, low: Int, high: Int): Unit = {
		if (low > high)
		{
			// When given range are not valid
			this.missingElement(arr, size, high, low);
		}
		else
		{
			// Create a empty Set
			var data: Set[Int] = Set();
			var count: Int = 0;
			this.display(arr, size);
			// Execute loop through by size
			var i: Int = 0;
			while (i < size)
			{
				// Check that whether get array [i] element is part of given range
				if (arr(i) >= low && arr(i) <= high)
				{
					if (data.contains(arr(i)) == false)
					{
						// Add element which is exist within given range in an array
						data.add(arr(i));
					}
				}
				i += 1;
			}
			// Display the given range
			print(" Missing element in range of (" + low + "," + high + ") is \n");
			// Display missing element between the range
			i = low;
			while (i <= high)
			{
				if (data.contains(i) == false)
				{
					// Increase the count value by one
					// When elements are missing
					print(" " + i);
					count += 1;
				}
				i += 1;
			}
			print("\n Total missing : ");
			if (count == 0)
			{
				print(" None \n\n");
			}
			else
			{
				print(" "+count + "\n\n");
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Detection = new Detection();
		// Define array of integer element
		var arr1: Array[Int] = Array(1, 9, 2, 4, 5, 7, 10);
		// Define another Array element
		var arr2: Array[Int] = Array(1, 31, 74, 12, 4, 71);
		// Get the size of arr1
		var size: Int = arr1.length;
		task.missingElement(arr1, size, 1, 10);
		// Get the size of arr2
		size = arr2.length;
		task.missingElement(arr2, size, 70, 75);
	}
}

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total missing :  3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total missing :  4
/*
   Swift 4 program
   Find all missing elements of a range
*/
class Detection
{
	// This function are displaying given array elements
	func display(_ arr: [Int], _ size: Int)
	{
		print(" Array : ", terminator: "");
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Find the missing elements of an array of within of given range
	func missingElement(_ arr: [Int], _ size: Int, _ low: Int, _ high: Int)
	{
		if (low > high)
		{
			// When given range are not valid
			self.missingElement(arr, size, high, low);
		}
		else
		{
			// Create a empty Set
			var data = Set<Int>()
			var count: Int = 0;
			self.display(arr, size);
			// Execute loop through by size
			var i: Int = 0;
			while (i < size)
			{
				// Check that whether get array [i] element is part of given range
				if (arr[i] >= low && arr[i] <= high)
				{
					if (data.contains(arr[i]) == false)
					{
						// Add element which is exist within given range in an array
						data.insert(arr[i]);
					}
				}
				i += 1;
			}
			// Display the given range
			print(" Missing element in range of (", low ,",", high ,") is ");
			// Display missing element between the range
			i = low;
			while (i <= high)
			{
				if (data.contains(i) == false)
				{
					// Increase the count value by one
					// When elements are missing
					print(" ", i, terminator: "");
					count += 1;
				}
				i += 1;
			}
			print("\n Total missing : ", terminator: "");
			if (count == 0)
			{
				print(" None \n");
			}
			else
			{
				print(count ,"\n");
			}
		}
	}
}
func main()
{
	let task: Detection = Detection();
	// Define array of integer element
	let arr1: [Int] = [1, 9, 2, 4, 5, 7, 10];
	// Define another Array element
	let arr2: [Int] = [1, 31, 74, 12, 4, 71];
	// Get the size of arr1
	var size: Int = arr1.count;
	task.missingElement(arr1, size, 1, 10);
	// Get the size of arr2
	size = arr2.count;
	task.missingElement(arr2, size, 70, 75);
}
main();

Output

 Array :   1  9  2  4  5  7  10
 Missing element in range of ( 1 , 10 ) is
  3  6  8
 Total missing : 3

 Array :   1  31  74  12  4  71
 Missing element in range of ( 70 , 75 ) is
  70  72  73  75
 Total missing : 4
/*
   Kotlin program
   Find all missing elements of a range
*/
class Detection
{
	// This function are displaying given array elements
	fun display(arr: Array < Int > , size: Int): Unit
	{
		print(" Array : ");
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Find the missing elements of an array of within of given range
	fun missingElement(arr: Array < Int > , size: Int, low: Int, high: Int): Unit
	{
		if (low > high)
		{
			// When given range are not valid
			this.missingElement(arr, size, high, low);
		}
		else
		{
			// Create a empty Set
			var data: MutableSet < Int > = mutableSetOf < Int > ();
			var count: Int = 0;
			this.display(arr, size);
			// Execute loop through by size
			var i: Int = 0;
			while (i < size)
			{
				// Check that whether get array [i] element is part of given range
				if (arr[i] >= low && arr[i] <= high)
				{
					if (data.contains(arr[i]) == false)
					{
						// Add element which is exist within given range in an array
						data.add(arr[i]);
					}
				}
				i += 1;
			}
			// Display the given range
			print(" Missing element in range of (" + low + "," + high + ") is \n");
			// Display missing element between the range
			i = low;
			while (i <= high)
			{
				if (data.contains(i) == false)
				{
					// Increase the count value by one
					// When elements are missing
					print(" " + i);
					count += 1;
				}
				i += 1;
			}
			print("\n Total missing : ");
			if (count == 0)
			{
				print(" None \n\n");
			}
			else
			{
				print(""+count + "\n\n");
			}
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Detection = Detection();
	// Define array of integer element
	var arr1: Array < Int > = arrayOf(1, 9, 2, 4, 5, 7, 10);
	// Define another Array element
	var arr2: Array < Int > = arrayOf(1, 31, 74, 12, 4, 71);
	// Get the size of arr1
	var size: Int = arr1.count();
	task.missingElement(arr1, size, 1, 10);
	// Get the size of arr2
	size = arr2.count();
	task.missingElement(arr2, size, 70, 75);
}

Output

 Array :  1 9 2 4 5 7 10
 Missing element in range of (1,10) is
 3 6 8
 Total missing : 3

 Array :  1 31 74 12 4 71
 Missing element in range of (70,75) is
 70 72 73 75
 Total missing : 4




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment