• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get the subarray with same entries  RSS feed

 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to solve a problem in a book. Here it is:

Write a program that takes an array/arraylist of integer and finds the length of a longest sub array/arraylist whose entries are equal.



I need help to solve it. Thanks
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Hasan wrote:Write a program that takes an array/arraylist of integer and finds the length of a longest sub array/arraylist whose entries are equal.

Could you give one or two examples of an array and its subarray that would satisfy this?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hasan,

1.)you have not mention about where are you having problem or anything else about your code.
2.)you didn't add comments which may describe us the code.
3.)you didn't use the good name for the variable for ex-at line 16,i really find it hard to parse,"m=i-1-m",what you meant here?use something which can be well intended.
4.)variety of work is done by a subArr.it should be limited to what it is intending.in your case it is sorting a list,printing a list,checking a list for whether it is empty or not and then printing its result and etc..,you can make use of refactoring.the way you are coding is a procedural style,you should avoid it as you are dealing with a oops based language.

try to correct these things as if you didn't then you will become habitual of it which may be not good for your coding style in concern of oops paradigm.

now for your problem,i tried but failed to parse your code..first thing i will advice you to write a pseudo code for it.
[Hint]:you are not required to sort the list.you can use a set having the same element as list is having(however set will not contain the duplicate element and we can make use of this) now try to analyze the scenario,below:
now its your work how to generalize it and implement a set.and do use of the helper methods for the sake of refactoring and readability.

Hope it helps!

Kind Regards,
Praveen.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
I know this is supposed to be pseudo code'ish but you may be sending someone down the wrong path with
"rank = rank++"
Output
before: 3
after: 3
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carey,
i just miss it during focusing on logic.
i think rank=++rank would work.
anyway thanks for notifying..

Kind regards,
Praveen.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an example if the arraylist = [0, 0, 0, 1, 1, 1, 1, 2]
The output has to be = [1, 1, 1, 1]

I found this question in Elements of Programming Interviews in Java book.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm..try coding with the steps as i have mentioned in my previous post.when their is only one element left in the list then don't increment the rank and add that 1 element(which is present in the list) *rank* times to the list.you will get your sub matrix.

did you tried it after i suggest?show me if you have.

Praveen
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!