Created
May 9, 2024 10:44
-
-
Save GReturn/8a37f7c8e922b756de6ad99f9e4b7cd8 to your computer and use it in GitHub Desktop.
From 1...n in an array, find missing integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
int size; | |
printf("size: "); | |
scanf("%d", &size); | |
int arr[size]; | |
for(int i = 0; i < size; i++) | |
{ | |
printf("[%d]: ", i); | |
scanf("%d", (arr+i)); | |
} | |
// Get largest to look for n in [1..n] | |
int largest = *arr; | |
for(int i = 0; i < size; i++) | |
{ | |
if(*(arr+i) > largest) largest = *(arr+i); | |
} | |
// Look for missing | |
int miss[size]; | |
int missCtr = 0; | |
for(int ctr = 1; ctr <= largest; ctr++) | |
{ | |
int found = 0; | |
for(int iter = 0; iter < size; iter++) | |
{ | |
if(*(arr+iter) == ctr) | |
{ | |
found = 1; | |
break; | |
} | |
} | |
if(!found) { | |
*(miss+missCtr) = ctr; | |
missCtr++; | |
} | |
} | |
printf("Missing: "); | |
for(int i = 0; i < missCtr; i++) | |
{ | |
printf("%d ", *(miss+i)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment