• Status: Solved
• Priority: Medium
• Security: Public
• Views: 266

sameEnds challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p134300

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
• 14
• 8
• 2
• +1
3 Solutions

Commented:
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

Author Commented:
what is difference between N and n here?  Are both same as per challenge or different? please advise
0

Commented:
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

Author Commented:
ok
0

Author 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

Author 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);
}
``````
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

Author 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);
}
``````
``````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);
}

}
``````
not sure why arr2 is always coming[0,0]
0

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.

You have to make n comparisons.
0

Author 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);
}

}
``````

0

Commented:
There are no errors shown.
I won't look at he errors until I see the pseudo code.
0

Author 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);
}

}
``````
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

Commented:
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

Author Commented:
All you have to do is make n comparisons.
let me try
You can if you want to,
but how?
0

Author 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;

}
``````

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
``````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;

}

}
``````
output is
true

passed many . need to fix few
0

Commented:
Did you notice that your code ALWAYS RETURNS TRUE?
You actually FAILED EVERY TEST!!!
0

Author 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;

}
``````

are you talking about above code. it is returning false in if loop and true in else loop?
0

Commented:
I am talking about the code in  https:#a41752575  that ALWAYS RETURNS TRUE.
0

Commented:
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

Commented:
>> 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

Commented:
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

Author Commented:
Sure
0

Author 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

Author 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;

}
``````

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

any improvements/modifications/other approaches to this challenge?
0

Commented:
That is good. But, you don't need the else block.
0

Author 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;

}
``````

above also worked fine
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.