Given an array A of length N, my task is to find the element which repeats in A maximum number of times as well as the corresponding count. In case of ties,I choose the smaller element first.
Input
First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases. Each case begins with a single integer N, the length of A. Then follow N space separated integers in next line. Assume that 1 <= T <= 100, 1 <= N <= 100 and for all i in [1..N] : 1 <= A <= 10000
Output

For each test case, output two space separated integers V & C. V is the value which occurs maximum number of times and C is its count.

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

int l(int *a,int n)
{int large=a[0];
int i;
for( i=0;i<n;i++)
  {if(large<a[i])
        {
            large=a[i];
        }
  }
  return large;
}
int findMode(int *array, int size, int large)
{   if(size==1)
    {printf ("%d %d\n",array[0],1);
    return 0;
    }
	int *array2;
	int count;
	int x;
	int mode = 0;
	int highest = 0;

	array2 =(int *)malloc(sizeof(int)*large);

	for (count = 0; count < large; count++)
		array2[count] = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x]++;
	}

	for (count = 0; count < large; count++)
	{
		if (array2[count] > highest)
		{
			highest = array2[count];
			mode = count;
		}
		else if(array2[count]==highest&&mode>count)
        {
            highest = array2[count];
			mode = count;
        }
	}
		printf("%d %d\n",mode,highest);
		return 1;
}
int main()
{
    int t,n,j,k,i;
    int a[100];
    scanf("%d",&t);
    j=0;

    while(j<t)
    {
    scanf("%d",&n);
    k=0;
    for(i=0;i<n;i++)
    m[i]=0;
    i=0;
    while(k<n)
    {
        scanf("%d%*c",&a[k]);
       
        k++;
    }
    findMode(a,n,10000);
    j++;
    }
    return 0;
}
Input:
2
5
1 2 3 2 5
6
1 2 2 1 1 2

Output:
2 2
1 3

The thing is that although my code is working for the above test cases,the online compiler I am using shows my answer as the wrong answer.
Can anyone tell for which test case is my code not working?
Please assume that input need not be validated

Dani AI

Generated

A few concrete problems explain the WA you see (and how to reproduce/fix them).

First: the counting array is allocated for large entries but your values can be as large as large itself. Passing 10000 and doing malloc(sizeof(int) * 10000) gives valid indices 0..9999; any input value equal to 10000 indexes past the end. That out‑of‑bounds write is undefined behavior and will produce a runtime error or wrong answer on some test files. A minimal failing case: one test with N>1 that includes a value 10000 (for example: T=1, N=2, A = 10000 10000).

Fixes to apply:

  • Compute the actual maximum value in the array and allocate the counts array with size maxValue + 1 (or, if you insist on a constant bound, pass 10001). Zero the buffer with calloc or memset so counts start at 0. Check the return of malloc/calloc when using large sizes. Free the counts array after each test case.
  • Remove the "%d%*c" scanf pattern; use scanf("%d", &a[k]) or safer line-based parsing. "%d%*c" can fail at EOF or consume characters you did not intend.
  • Fix the obvious typo m[i] (undefined in the posted code) so you do not write to an undeclared array.

Tie-breaking: iterating counts in ascending order already yields the smaller value on ties, so keep that pattern. As an alternative approach (safer for large/sparse values), use a hash map (C++ unordered_map or a map) or sort the array and count runs; both avoid large temporary arrays.

Notes for and : ’s observation about the conditional used for ties is sensible to inspect, but the primary cause of WA here is the off‑by‑one/allocation issue and the scanf/typo problems.

Nothing jumps out at me for why it doesn't work. The algorithm isn't an efficient one, but at first glance it seems like it ought to give the desired results. You haven't listed the data set that DID NOT work.

Line 44 -- Can mode ever be higher than count? I don't see how. If not, this if statement will never be true.

Line 4 -- Is this function called?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.