Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MaxSpan challenge

Posted on 2016-08-14
9
Medium Priority
?
241 Views
Last Modified: 2016-08-21
Hi,

I am working on below challenge

http://codingbat.com/prob/p189576
Array-3 > maxSpan
prev  |  next  |  chance
Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array. (Efficiency is not a priority.)

maxSpan([1, 2, 1, 1, 3]) → 4//why 4??
maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6//why 6?
maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6

i have not understood above problem description?
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 total points
ID: 41756194
First example: the number 1 has the largest span (1,2,1,1). 4 wide.

Second example: numbers 1 and 4 have the largest span.

Third example: the number 4 has the largest span.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756475
i see the point now
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756560
OK ;)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 41760273
maxSpan([1, 2, 1, 1, 3]) → 4

how above is 4 it looks 3 to me as there are 3 ones there?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41760817
The '4' span is:
1, 2, 1, 1
0
 
LVL 7

Author Comment

by:gudii9
ID: 41761861
The '4' span is:
1, 2, 1, 1
'

i got it.

Not getting idea on psedo code for this challenge?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41761866
can i write solution with simgle loop?
psedo code:

0. loop through array
1. find left most appearance of a number and note the index
2. find if that number exist within the loop again from backwards
3. if yes find the index of that.
4. find the span length which is difference of end index- start index
5. return span length
0
 
LVL 7

Author Comment

by:gudii9
ID: 41761882
public int maxSpan(int[] nums) {
		int span=1;
		int counter=0;
	    if (nums.length>0) {
	    	for (int i = 0; i < nums.length; i++) {
				for (int k = nums.length-1; k >0; k--) {
					if(nums[k]==nums[i]){
						counter=k-i+1;
						if(counter>span){
							span=counter;
						}
						
					}
				}
			}
	    }
			
		 else {
			span=0;

		}
		return span;
		
}
		

Open in new window


above pass all tests
Expected      Run            
maxSpan([1, 2, 1, 1, 3]) → 4      4      OK      
maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6      6      OK      
maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6      6      OK      
maxSpan([3, 3, 3]) → 3      3      OK      
maxSpan([3, 9, 3]) → 3      3      OK      
maxSpan([3, 9, 9]) → 2      2      OK      
maxSpan([3, 9]) → 1      1      OK      
maxSpan([3, 3]) → 2      2      OK      
maxSpan([]) → 0      0      OK      
maxSpan([1]) → 1      1      OK      
other tests
OK      

public class MaxSpan {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
 int[] ar={1, 2, 1, 1, 3};
 System.out.println("value is"+maxSpan(ar));
	}

	
	public static int maxSpan(int[] nums) {
		int span=1;
		int counter=0;
	    if (nums.length>0) {
	    	for (int i = 0; i < nums.length; i++) {
				for (int k = nums.length-1; k >0; k--) {
					if(nums[k]==nums[i]){
						counter=k-i+1;
						if(counter>span){
							span=counter;
						}
						
					}
				}
			}
	    }
			
		 else {
			span=0;

		}
		return span;
		
}
}

Open in new window

value is4



any improvement, optimization, refinement to my code?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41763586
Passes all test - great! Efficiency was not a priority for this one so I'd keep it like this  ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question