Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Program to print missing elements that lie in range 0 99

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 325
    Comment on it

    The logic of the program is to use a boolean array named presentseen of size 100, the size is defined by macro LIMIT. The boolean array is used to keep track of array elements that lie in range 0 to 99, which is initially initialized to false. An input array passed to printMissing function is traversed and  elements present in passed array are marked in the boolean array as true. Once all elements in passed array are marked as true in boolean array, the boolean array is then used to print missing elements using ternary operator.

    // C program for print missing elements
    #include<stdio.h>
    #define LIMIT 100
    
    void printMissing(int arr[], int n)
    {
    	// Initialize all number from 0 to 99 as false
    	bool presentseen[LIMIT] = {false};
    
    	// Mark present elements in range [0-99] as true
    	for (int i=0; i<n; i++)
    	if (arr[i] < LIMIT)
    	presentseen[arr[i]] = true;
    
    	// Print missing element
    	int i = 0;
    	while (i < LIMIT)
    	{
    		// If i is missing
    		if (presentseen[i] == false)
    		{
    			// Find if there are more missing elements after i
    			int j = i+1;
    			while (j < LIMIT && presentseen[j] == false)
    				j++;
    
    			// Print missing single or range
    			(i+1 == j)? printf("%d\n", i): printf("%d-%d\n", i, j-1);
    
    			// Update i
    			i = j;
    		}
    		else
    			i++;
    	}
    }
    
    int main()
    {
    	int arr[] = {88, 105, 3, 2, 200, 0, 10};
    	int n = sizeof(arr)/sizeof(arr[0]);
    	printMissing(arr, n);
    	return 0;
    }
    

    Output:

    1
    4-9
    11-87
    89-99

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: