Problem Description

Given an sorted array A[i] of N positive integers having all the numbers occuring exactly twice, except for one number which will occur only once. Find the number occuring only once.
Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
The first line of each test case contains a positive integer N, denoting the size of the array.
The second line of each test case contains a N positive integers, denoting the elements of the array.
Output
Print out the singly occurring number.
Constraints
1 <= T <= 100
0 < N <= 100
0 <= A[i] <= 100

  • Test Case 1
    Input (stdin)
    2
    5
    1 1 2 5 5
    7
    2 2 5 5 20 30 30
    
    Expected Output
    2
    20
  • Test Case 2
    Input (stdin)
    2
    5
    1 1 2 5 5
    3
    20 30 30
    
    Expected Output
    2
    20
#include <stdio.h>
int main()
{
int a,b,c[10],d[10],i,min,k;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b);
for(k=0;k<b;k++)
{
scanf("%d",&c[k]);
}
for(k=0;k<b;k++)
{

if(c[k]!=c[k+1] && c[k]!=c[k+2] && c[k]!=c[k-1] && c[k]!=c[k-2] )
{
printf("%d",c[k]);
}
}
printf("\n");
}
return 0;
}

Here's a brief explanation of how the code works:

  1. Input Handling:
    The program begins by taking the number of test cases (a). For each test case, it reads the size of the array (b) and then reads the array elements into the array c[].

  2. Finding the Single Occurrence:
    The program loops through each element in the array c[]. For each element, it checks if it is different from its neighboring elements. Specifically:

    • It compares c[k] with c[k+1], c[k+2], c[k-1], and c[k-2].
    • If c[k] is not equal to any of these neighbors, it is considered the element that occurs only once.
  3. Output:
    The element that occurs only once is printed for each test case. If multiple elements meet the condition, the last one found is printed.

  4. Edge Cases:

    • The array is assumed to be sorted, so the singly occurring element will always be found by this method.
    • The array is expected to have a valid structure according to the problem's constraints.

Note: The code logic has potential issues, particularly with the boundary checks (e.g., c[k+2], c[k-2] for small arrays). The code may not work correctly for cases where the array size is less than 4 or when the single element appears at the boundaries.