Find missing elements from an array

Here given code implementation process.

/*
  C++ Program
  Find missing elements from an array
*/
#include<iostream>

using namespace std;


class MyArray {
	public:

		//Find all missing element in given sorted array
		void find_missing(int arr[], int size) {
			if (size <= 1) {
				return;
			} else {
				for (int i = 0; i < size - 1; ++i) {
					//Find the all missing elements
					for (int j = arr[i] + 1; j < arr[i + 1]; ++j) {
						//display missing element
						cout << " " << j;
					}
				}
			}
		}
};
int main() {
	MyArray obj ;
	int arr[] = {
		1,
		2,
		3,
		4,
		4,
		7,
		7
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	obj.find_missing(arr, size);
	return 0;
}

Output

 5 6
/*
  Java Program
  Find missing elements from an array
*/

public class MyArray 
{
  //Find all missing element in given sorted array
  public void find_missing(int []arr,int size)
  {
    if(size<=1)
    {
      return;
    }
    else
    {
      for (int i = 0; i < size-1; ++i)
      {
        //Find the all missing elements
        for (int j = arr[i]+1; j < arr[i+1]; ++j)
        {
          //display missing element
          System.out.print("  "+j);
        }
      }
    }
  }
  public static void main(String[] args) {
    MyArray obj = new MyArray();
    //Define the value of array elements
    int []arr = {1, 2, 3, 4, 4, 7, 7} ;
    //Get the size of array
    int size = arr.length;

    obj.find_missing(arr,size);
  }
}

Output

 5 6
/*
  C# Program
  Find missing elements from an array
*/
using System;

public class MyArray {
	//Find all missing element in given sorted array
	public void find_missing(int[] arr, int size) {
		if (size <= 1) {
			return;
		} else {
			for (int i = 0; i < size - 1; ++i) {
				//Find the all missing elements
				for (int j = arr[i] + 1; j < arr[i + 1]; ++j) {
					Console.Write(" " + j);
				}
			}
		}
	}
	public static void Main(String[] args) {
		MyArray obj = new MyArray();
		
		//Define the value of array elements
		int[] arr = {
			1,
			2,
			3,
			4,
			4,
			7,
			7
		};
		//Get the size of array
		int size = arr.Length;
		obj.find_missing(arr, size);
	}
}

Output

 5 6
<?php
/*
  Php Program
  Find missing elements from an array
*/
class MyArray {
	//Find all missing element in given sorted array

	public 	function find_missing($arr, $size) {
		if ($size <= 1) {
			return;
		} else {
			for ($i = 0; $i < $size - 1; ++$i) {
				//Find the all missing elements
				for ($j = $arr[$i] + 1; $j < $arr[$i + 1]; ++$j) {
					//display missing element

					echo(" ". $j);
				}
			}
		}
	}
}

function main() {
	$obj = new MyArray();
	//Define the value of array elements
	$arr = array(1, 2, 3, 4, 4, 7, 7);
	//Get the size of array
	$size = count($arr);
	$obj->find_missing($arr, $size);

}
main();

Output

 5 6
/*
  Node Js Program
  Find missing elements from an array
*/
class MyArray {
	//Find all missing element in given sorted array
	find_missing(arr, size) {
		if (size <= 1) {
			return;
		} else {
			for (var i = 0; i < size - 1; ++i) {
				//Find the all missing elements

				for (var j = arr[i] + 1; j < arr[i + 1]; ++j) {
					//display missing element

					process.stdout.write(" " + j);
				}
			}
		}
	}
}

function main(args) {
	var obj = new MyArray();
	//Define the value of array elements
	var arr = [1, 2, 3, 4, 4, 7, 7];
	//Get the size of array
	var size = arr.length;
	obj.find_missing(arr, size);
}

main();

Output

 5 6
# Python 3 Program
# Find missing elements from an array
class MyArray :
	# Find all missing element in given sorted array
	def find_missing(self, arr, size) :
		if (size <= 1) :
			return
		else :
			i = 0
			while (i < size - 1) :
				# Find the all missing elements
				j = arr[i] + 1
				while (j < arr[i + 1]) :
					print(" ", j, end = "")
					j += 1
				
				i += 1


def main() :
	obj = MyArray()
	arr = [1, 2, 3, 4, 4, 7, 7]
	size = len(arr)
	obj.find_missing(arr, size)


if __name__ == "__main__":
	main()

Output

  5  6
# Ruby Program
# Find missing elements from an array
class MyArray 
	# Find all missing element in given sorted array
	def find_missing(arr, size) 
		if (size <= 1) 
			return
		else 
			i = 0
			while (i < size - 1) 
				# Find the all missing elements
				j = arr[i] + 1
				while (j < arr[i + 1]) 
					print(" ", j)
					j += 1
				end
				i += 1
			end
		end
	end
end
def main() 
	obj = MyArray.new()
	arr = [1, 2, 3, 4, 4, 7, 7]
	size = arr.length
	obj.find_missing(arr, size)
end
main()

Output

 5 6
/*
  Scala Program
  Find missing elements from an array
*/
class MyArray {
	//Find all missing element in given sorted array
	def find_missing(arr: Array[Int], size: Int): Unit = {
		if (size <= 1) {
			return;
		} else {
			var i: Int = 0;
			while (i < size - 1) {
				//Find the all missing elements
				var j: Int = arr(i) + 1;
				while (j < arr(i + 1)) {
					print(" " + j);
					j += 1;
				}
				i += 1;
			}
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyArray = new MyArray();
		val arr: Array[Int] = Array(1, 2, 3, 4, 4, 7, 7);
		val size: Int = arr.length;
		obj.find_missing(arr, size);
	}
}

Output

 5 6
/*
  Swift Program
  Find missing elements from an array
*/
class MyArray {
	//Find all missing element in given sorted array
	func find_missing(_ arr: [Int], _ size: Int) {
		if (size <= 1) {
			return;
		} else {
			var i: Int = 0;
			while (i < size - 1) {
				//Find the all missing elements
				var j: Int = arr[i] + 1;
				while (j < arr[i + 1]) {
					print(" ", j, terminator: "");
					j += 1;
				}
				i += 1;
			}
		}
	}
}
func main() {
	let obj: MyArray = MyArray();
	let arr: [Int] = [1, 2, 3, 4, 4, 7, 7];
	let size: Int = arr.count;
	obj.find_missing(arr, size);
}
main();

Output

  5  6
//C Program 
//Find missing elements from an array
#include <stdio.h>

//Find all missing element in given sorted array
void find_missing(int arr[],int size)
{
  if(size<=1)
  {
    return;
  }
  else
  {
    for (int i = 0; i < size-1; ++i)
    {
      //Find the all missing elements
      for (int j = arr[i]+1; j < arr[i+1]; ++j)
      {
        //display missing element
        printf("  %d\n",j);
      }
    }
  }
}
int main()
{

  //Define the value of array elements
  int arr[] ={1, 2, 3, 4, 4, 7, 7} ;
  //Get the size of array
  int size=sizeof(arr) / sizeof(arr[0]);

  find_missing(arr,size);

  return 0;
  
}

Output

  5
  6

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







© 2021, kalkicode.com, All rights reserved