Write a program to display the Repeating and Missing numbers.

Repeating and Missing numbers: You are given an array of size N. It contains numbers in the range 1-N(Both inclusive). One of these numbers is repeating and one is missing in the array.

Write a program to display these 2 numbers. The output should be the missing number followed by the repeating number separated by a space.

Repeating and Missing numbers

Input:

5
1,3,3,4,5

Output:

3 2

Solution

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

void repeatingMissing(int arr[], int n)
{

    //write your code here
    int i;

    for (i = 0; i < n; i++) {
        int abs_val = abs(arr[i]);
        if (arr[abs_val - 1] > 0)
            arr[abs_val - 1] = -arr[abs_val - 1];
        else
            printf("%d ", abs_val);
    }
    for (i = 0; i < n; i++) {
        if (arr[i] > 0)
            printf("%d", i + 1);
    }

}
int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    repeatingMissing(arr,n);
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *