• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

sameEnds challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p134300

i read description as below
Array-2 > sameEnds
prev  |  next  |  chance
Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.

sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false
Go...Save, Compile, Run



public boolean sameEnds(int[] nums, int len) {
  return true;
}
Go
Shorter output
Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      true      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      true      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 1) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 2) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 0) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 5) → true      true      OK      
sameEnds([1, 1, 1], 0) → true      true      OK      
sameEnds([1, 1, 1], 1) → true      true      OK      
sameEnds([1, 1, 1], 2) → true      true      OK      
sameEnds([1, 1, 1], 3) → true      true      OK      
sameEnds([1], 1) → true      true      OK      
sameEnds([], 0) → true      true      OK      
sameEnds([4, 2, 4, 5], 1) → false      true      X      
other tests
X      


i have not understood the description of the challenge clearly. Please advise how ends are same and what is n there?
0
gudii9
Asked:
gudii9
  • 14
  • 8
  • 2
  • +1
3 Solutions
 
d-glitchCommented:
If you don't understand the Challenge, you must work on the pseudo code until you do.  
This is much more difficult than any of the other challenges you have posted.  I'm not sure you are ready for it.
Do not post any more code until an expert has passed on your pseudo code.
0
 
gudii9Author Commented:
what is difference between N and n here?  Are both same as per challenge or different? please advise
0
 
d-glitchCommented:
n or N is the number of elements that much match at the beginning and end of the array.
The challenge uses them interchangeably.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gudii9Author Commented:
ok
0
 
gudii9Author Commented:
For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3

what it means by ends are same for n=0 and n=2.(are they saying beginning two characters at index 0, index 1 are 5,6 which are same as, last but one(penultimate), last ultimate character 5,6 are same so true??

i have not got meaning or clue out of it?
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
  int lengt=nums.length;
  int[] arr1=new int[len];
  int[] arr2=new int[len];
  
  for(int i=0;i<len;i++){
    
    arr1[i]=nums[i];
   
  }
  for(int i=(lengt-len);i<len;i++){
   arr2[i]=nums[i];
  }
  return arr1.equals(arr2);
}

Open in new window

Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      false      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      false      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      false      OK      
sameEnds([1, 2, 5, 2, 1], 1) → true      false      X      
sameEnds([1, 2, 5, 2, 1], 2) → false      false      OK      
sameEnds([1, 2, 5, 2, 1], 0) → true      false      X      
sameEnds([1, 2, 5, 2, 1], 5) → true      false      X      
sameEnds([1, 1, 1], 0) → true      false      X      
sameEnds([1, 1, 1], 1) → true      false      X      
sameEnds([1, 1, 1], 2) → true      false      X      
sameEnds([1, 1, 1], 3) → true      false      X      
sameEnds([1], 1) → true      false      X      
sameEnds([], 0) → true      false      X      
sameEnds([4, 2, 4, 5], 1) → false      false      OK      
other tests
X      
i think got meaning of the challenge

psedo code:
1. loop through given array
2. create two new arrays
3. assign each element of given array until len to first newly created array
4. similary assign each element of given array back ward elements upto len to second newly created array
5. return equality of the both arrays
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
  int lengt=nums.length;
  int[] arr1=new int[len];
  int[] arr2=new int[len];
  
  for(int i=0;i<len;i++){
    
    arr1[i]=nums[i];
   
  }
  for(int j=(lengt-len);j<len;j++){
   arr2[j]=nums[j];
  }
  return arr1.equals(arr2);
}

Open in new window

public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2;j<len;j++){
		   arr2[j]=nums[j];
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window

not sure why arr2 is always coming[0,0]
0
 
d-glitchCommented:
Your pseudo code is not correct.
1. You don't have to loop through the array.
2. You don't have to make any new arrays.
3. There is nothing in the challenge about backwards.

You have to make n comparisons.
0
 
gudii9Author Commented:
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2, k=0;j<lengt-1;j++){
		   arr2[j]=nums[k];
		   k++;
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window


above gives below error. please advise
0
 
d-glitchCommented:
There are no errors shown.
I won't look at he errors until I see the pseudo code.
0
 
gudii9Author Commented:
Your pseudo code is not correct.
1. You don't have to loop through the array.
2. You don't have to make any new arrays.
3. There is nothing in the challenge about backwards.

corrected psedo code of the logic


1. loop through given array
2. create two new arrays one representing front part array {5,6}--->{5, 6, 45, 99, 13, 5, 6}
;other arr2 representing back part array({5,6}--->{5, 6, 45, 99, 13, 5, 6};

3. assign each element of given array until len to first newly created array to build first array ie arr1
4. similary assign each element of given array back ward elements upto len to second newly created array.
5. return equality of the both arrays arr1 and arr2 to make sure it returns true if front and back portions are same

sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true

i am getting illegal array while debugging for left had side array at line 24  as attached when i run my code
for(int j=len2;j<lengt;j++){
              arr2[j]=nums[j];
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len2];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2;j<lengt;j++){
		   arr2[j]=nums[j];
		   //k++;
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window

and then below error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at SameEnds.sameEnds(SameEnds.java:24)
      at SameEnds.main(SameEnds.java:8)
illegalArray.png
0
 
d-glitchCommented:
Your pseudo code is not correct.

1. You don't have to loop through the array.
     In fact, you should not loop through the array.
2. You don't have to make any new arrays.
    You can if you want to, but it is very wasteful.
3. There is nothing in the challenge about backwards.

All you have to do is make n comparisons.

There is no reason to look at your code because YOUR PSUEDO CODE IS WRONG.
0
 
gudii9Author Commented:
All you have to do is make n comparisons.
let me try
You can if you want to,
but how?
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((len-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}

Open in new window


Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      true      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      true      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 1) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 2) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 0) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 5) → true      true      OK      
sameEnds([1, 1, 1], 0) → true      true      OK      
sameEnds([1, 1, 1], 1) → true      true      OK      

Open in new window

sameEnds([1, 1, 1], 2) → true      true      OK      
sameEnds([1, 1, 1], 3) → true      true      OK      
sameEnds([1], 1) → true      true      OK      
sameEnds([], 0) → true      true      OK      
sameEnds([4, 2, 4, 5], 1) → false      true      X      
other tests
X      
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
public static boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((len-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}


}

Open in new window

output is
 true

passed many . need to fix few
0
 
d-glitchCommented:
WHERE IS YOUR PSUEDO CODE?
Did you notice that your code ALWAYS RETURNS TRUE?
You actually FAILED EVERY TEST!!!
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
  
boolean result=true;

for(int i=0;i<len;i++){
  
  if(nums[i]!=nums[(len-n)+i)]{
    result=false;
  }
  else {
    result=true;
  }
}
return result;
  
}

Open in new window


are you talking about above code. it is returning false in if loop and true in else loop?
0
 
d-glitchCommented:
I am talking about the code in  https:#a41752575  that ALWAYS RETURNS TRUE.
0
 
awking00Commented:
The code may be a little challenging but the task is relatively easy to understand. Whatever len is find that many ints at the beginning of the array, then see if they are the same and in the same order as the last n ints of the array.
0
 
d-glitchCommented:
>> awking00

I agree.  The challenge test does not say anything about order:
     Return true if the group of N numbers at the start and end of the array are the same.

Test output results do indicate that the same order interpretation is correct, making the challenge fairly straight forward.  It would be trickier if order did not matter.
0
 
rrzCommented:
Your last code is almost there.  You should write the array  on a piece of paper.  The graphic layout will help you. Step through your code by hand and point to each element as it being accessed.  Also, ask yourself; What is the last element of an array?  
nums[ ? ] == last element
1
 
gudii9Author Commented:
Sure
0
 
gudii9Author Commented:
You should write the array  on a piece of paper.  The graphic layout will help you. Step through your code by hand and point to each element as it being accessed.  Also, ask yourself; What is the last element of an array?  
nums[ ? ] == last element

above suggestion is very useful. I am esp. using erasable board and marker. If it get more complicated opening eclipse debugger same time
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;
		int totalLen=nums.length;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((totalLen-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}

Open in new window


above passed all tests. there is type i used len-len by mistake earlier.

any improvements/modifications/other approaches to this challenge?
0
 
rrzCommented:
That is good. But, you don't need the else block.
0
 
gudii9Author Commented:
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;
		int totalLen=nums.length;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((totalLen-len)+i)] ){
		    result=false;
		  }
		
		}
		return result;
		  
		
}

Open in new window


above also worked fine
0

Featured Post

Technology Partners: 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!

  • 14
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now