I have written a slow solution to the Memorise Me problem on hackerearth
The first line of input will contain N, an integer, which is the total number of numbers shown to your team.
The second line of input contains N space separated integers.
The third line of input contains an integer Q, denoting the total number of integers.
The Next Q lines will contain an integer denoting an integer, Bi, for which you have to print the number of occurrences of that number (Bi) in those N numbers on a new line.
If the number Bi isn’t present then print
NOT PRESENT
on a new line.Constraints
$$1 \le N \le 10^5$$
$$0 \le B_i \le 1000$$
$$1 \le Q \le 10^{55}$$
#include <stdio.h>
int check(int z,int c[1001][2],int q,int arr[])
{
int i =0;
while(c[i][0]!=-1)
{
if (z==c[i][0])
{
return c[i][1];
break;
}
else
{
c[i][0]=z;
c[i][1]=count(z,arr,q);
return c[i][1];
}
i++;
}
}
int count(int z,int arr[],int q)
{
int occurence=0;
for(int k=0;k<q;k++)
{
if(z==arr[k])
{
occurence++;
}
}
return occurence;
}
int main()
{
int c[1001][2];
for(int i=0;i<=1001;i++)
{
c[i][0]=-1;
c[i][1]=0;
}
c[0][0]=9999;
int n;
scanf("%d",&n);
int arr[100000];
int i;
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int q;
scanf("%d",&q);
int j;
int B[100000];
int occurence1=0;
for(j=0;j<q;j++)
{
scanf("%d",&B[j]);
occurence1=check(B[j],c,q,arr);
if(occurence1==0)
{
printf("NOT PRESENT\n");
}
else
{
printf("%d\n",occurence1);
}
}
}
Code taking 0.9 seconds for a set of input where \$n=10^5\$ else it's taking 0.1 seconds. Can you suggest a shorter way to reduce time complexity! And suggest improvements in the code itself.