Home > Backend Development > C++ > body text

Print out missing elements in the range 0-99

王林
Release: 2023-09-06 08:13:19
forward
645 people have browsed it

Print out missing elements in the range 0-99

It will display the missing values ​​in the given set entered by the user

Given : array = {88, 105, 3, 2, 200, 0, 10};
Output : 1 4-9 11-87 89-99
Copy after login

Algorithm

START
STEP 1-> Take an array with elements, bool flag[MAX] to Fale, int i, j, n to size of array
Step 2-> Loop For from I to 0 and i<n and i++
   IF array[i] < 100 && array[i]>=0
      Set flag[array[i]]=true
   End IF
Step 3 -> End For Loop
Step 4 -> Loop For from i to 0 and i<MAX and ++i
   IF flag[i] == false
      Set j=i+1
      Loop While j<MAX && flag[j] == false
         Set j++
      End While
      If j=i+1
         Print i
      End IF
   Else
      Print i and j-1
   End Else
   Set i=j
End IF
Step 5 -> End For Loop
STOP
Copy after login

Example

#include <stdio.h>
#define MAX 100
int main(int argc, char const *argv[]) {
   int array[] = {88, 105, 3, 2, 200, 0, 10};
   bool flag[MAX] = { false }; //Initializing all the values of flag as false
   int i, j, n;
   n = sizeof(array)/sizeof(array[0]);
   for (i = 0; i < n; i++) {
      if (array[i] < 100 && array[i]>=0) {
         flag[array[i]] = true; //Making the value of the elements present in an array as true, So missing will remain false
      }
   }
   for (i = 0; i < MAX; ++i) {
      if(flag[i] == false) { //Checking for false values
         j = i+1; //Giving the value of the next iteration
         while(j<MAX && flag[j] == false) //Checking the value of flag[j] is false
         j++;
         if (j==i+1) //For printing the missing number
            printf("%d</p><p>", i);
         else //For printing the missing range
            printf("%d-%d</p><p>", i, j-1);
         i = j; //Initializing the range&#39;s last value to start from that number
      }
   }
   return 0;
}
Copy after login

Output

If we run the above program, it will generate the following output

1
4-9
11-87
89-99
Copy after login

The above is the detailed content of Print out missing elements in the range 0-99. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template