Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Given an array a of n elements, print any value that appears at least three times or print -1 if there is no such value

here is my code and it always output -1 and I didn’t know why. any help?
Input
The first line contains an integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It is guaranteed that the sum of n over all test cases does not exceed 2â‹…105.

Output
For each test case, print any value that appears at least three times or print -1 if there is no such value.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, size,*arr, *frr,count,*ptr,g,s;
    scanf("%d", &n);
    ptr = (int*)malloc(n * sizeof(int));
    for (int i = 0;i < n; i++)
    {
        scanf("%d",&size);
        arr = (int*)malloc(size * sizeof(int));
        frr = (int*)malloc(size * sizeof(int));
        for(int j = 0; j < size; j++)
            {
                scanf("%d",arr+j);
                *(frr + j) = -1;
            }
        if(size >= 3)
        {
            for (g = 0; g < size ; g++)
            {
                count=1;
                for(s = g + 1; s < size;s++)
                {
                    if(*(arr + g) == *(arr + s))
                    {
                        count++;
                        *(frr+s) = 0;
                    }             
                }
                if(*(frr+g) != 0 )
                {
                    *(frr+g) = count;
                }

                if(*(frr+g) >= 3)
                {
                    *(ptr+i) = *(arr + g);
                }else
                {
                    *(ptr+i) = -1;
                }
                
            }  
        }else
        {
            *(ptr+i) = -1;
        }
        
        free(arr);
        free(frr);

    }
    for(int j = 0;j<n;j++)
    {
        printf("%d\n",*(ptr+j));
    }
}

>Solution :

The problem is that you set *(ptr+i) to -1 for each element of the array. This means that a later element of the array that is not repeated three times will reset *(ptr+i) to -1.

Change this

            if(*(frr+g) >= 3)
            {
                *(ptr+i) = *(arr + g);
            }
            else
            {
                *(ptr+i) = -1;
            }

to this

            if(*(ptr+i) == -1 && *(frr+g) >= 3)
            {
                *(ptr+i) = *(arr + g);
            }

and at the beginning add this

ptr = (int*)malloc(n * sizeof(int));
for (int i = 0;i < n; i++)
{
    *(ptr + i) = -1;

But as has already been said in the comments you do not need either the ptr array or the frr array. You only run one test at a time so there is no need to keep all the test results before you print any of them out. And you only need to save the frequency of the current element you are testing, so you don’t need an array for that either.

And make your code readable, change *(arr + g) to arr[g]. They both work exactly the same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading