Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to find the missing values in a sorted range (x to y) array with C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

int *FindMissingValuesInSortedRangeArray(int arr[], int size, int x, int y, int *totalMissingValues) {
    int *missingValues = (int *)malloc(y - x + 1 * sizeof(int));

    for (int i = x; i <= y; i++) {
        int found = 0;
        for (int j = 0; j < size; j++) {
            if (arr[j] == i) {
                found = 1;
                break;
            }
        }
        if (!found) {
            missingValues[(*totalMissingValues)++] = i;
        }
    }
    
    return missingValues;
}

int main() {
    int x = 4, y = 15;
    int arr[] = {5, 5, 5, 5, 6, 7, 9, 10, 10, 10, 11, 13};
    int size = sizeof(arr) / sizeof(arr[0]);
    int totalMissingValues = 0;
    
    int *missingValues = FindMissingValuesInSortedRangeArray(arr, size, x, y, &totalMissingValues);

    printf("missingValues: ");
    for (int i = 0; i < totalMissingValues; i++) {
        printf("%d ", missingValues[i]);
    }
    printf("\n");

    free(missingValues);
    
    return 0;
}



/*
run:
   
missingValues: 4 8 12 14 15 
            
*/

 



answered Oct 26, 2024 by avibootz
edited Oct 26, 2024 by avibootz
0 votes
#include <stdio.h>

int NumberExist(int arr[], int size, int x) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == x) {
            return 1;
        }
    }
    
    return 0;
}

void FindMissingValuesInSortedRangeArray(int arr[], int size, int x, int y) {
    int index = 0;

    printf("missingValues: ");
    while (x <= y) {
        if (index < size && NumberExist(arr, size, x)) {
            index++;
        } else {
            printf("%d ", x);
        }
        x++;
    }
    printf("\n");
}

int main() {
    int x = 4, y = 15;
    int arr[] = {5, 5, 5, 5, 6, 7, 9, 10, 10, 10, 11, 13};
    int size = sizeof(arr) / sizeof(arr[0]);

    FindMissingValuesInSortedRangeArray(arr, size, x, y);

    return 0;
}


/*
run:
   
missingValues: 4 8 12 14 15 

*/

 



answered Oct 26, 2024 by avibootz
...