I have these two methods in java
public Int[] getFollows
public Int[] getFollowers
Where getfollows(user1)
returns an array of users
that user1
follows
and getfollowers(user1)
returns an array of users
that follow user1
My current data structure is an 2-d array so that I have
For every new follower of user1
I run followerArray[user1][followers++]=newFollower
This means that getFollowers()
will run in O(1) time as I just return followerArray[user1]
However the method getFollows()
will require me to use two for
loops to search through all of the arrays N times so will run in O(N^2).
Is there a way that I can decrease the time complexity of the method getFollows
without sacrificing the speed of getFollowers()