#hi
9 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @thorn kernel! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class frequency{
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int arr[] = new int[n];
int count = 0;
int freq = 0;
int max = 0;
for(int i=0; i<n; i++)
arr[i] = scan.nextInt();
for(int i=0; i < (n / 2) +1; i++){
for(int j=i ; j<n ; j++){
if( arr[i] == arr[j])
count++;
}
if(count > freq){
max = arr[i];
freq = count;
}
if(count == freq){
if(arr[i] < max)
max = arr[i];
}
count = 0 ;
}
System.out.print(max);
}
}```
how do i solve this
i've been racking my brain over it for atleast half an hour now but can't seem to get it
here's the question
well ...... ur using an n^2 (n-squared) algorythm
and ur probably recounting the same bird ID's over and over
i mean if u have
1,3,1,1,3,1,2 then for i=0 u count the frequency of id=1 and at i=2 u count the same id again and at i=3 u count the same id AGAIN
....also, if the highest seen ID only appears in the 2nd half of the array u wont ever count it so thats ....... not good, ur lucky the tests all passed
u can count them in O(n) (all of them) instead of O(n^2)
then u just need to find the highest frequency