Find Missing And Repeating

QUESTION

Given an unsorted array of size n. Array elements are in range from 1 to n. One number ‘A’ from set {1, 2, n} is missing and one number ‘B’ occurs twice in array. Find these two numbers.\nNote: If you find multiple answers then print the Smallest number found.\n\nInput:\n\nThe first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.\nThe first line of each test case contains a single integer N denoting the size of array.\nThe second line contains N space-separated integers A1, A2, …, AN denoting the elements of the array.\n\n\nOutput:\n\nPrint B, the repeating number followed by A which is missing in a single line.\n\n\nConstraints:\n\n1 T 40\n1 N 100\n1 A[i] N.

“TESTCASE_1”: “2\n2\n2 2\n3 \n1 3 3\n###—###SEPERATOR—###—\n2 1\n3 2”, “TESTCASE_2”: “2\n2\n1 1\n2\n4 4\n###—###SEPERATOR—###—\n1 2\n1 2”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

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

int main() {
	//code
	int t;
	scanf("%d",&t);
	while(t--)
	{
	    int n,k,on,tw,i;
	    scanf("%d",&n);
	    int a[n+1];
	    memset(a,0,(n+1)*sizeof(int));
	    for(i=0;i<n;i++)
	    {
	        scanf("%d",&k);
	        a[k]++;
	    }
	    for(i=1;i<=n;i++)
	    {
	        if(a[i]==0)
	        {
	            on=i;
	        }
	        else if(a[i]==2)
	        {
	            tw=i;
	        }
	    }
	    printf("%d %d\n",tw,on);
	}
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro