Java > Array-2 >sameEnds (CodingBat Solution)

Problem:

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


Solution:

public boolean sameEnds(int[] nums, int len) {
/* This is how I thought for the problem, you should also:
for len = 1 //Conditions
nums[0] == nums[nums.length -1]
for len = 2 //Conditions
nums[1] == nums[nums.length -1]
nums[0] == nums[nums.length -2]

for len == 3 //Conditions
nums[2] == nums[nums.length -1]
nums[1] == nums[nums.length -2]
nums[0] == nums[nums.length -3] */
boolean result = true;
int range = len;
//Now translate the conditions, 
//little bit tricky, but you can manage it!
for (int i =0; i <range;i++)
if (!(nums[i] == nums[nums.length - range + i]))
result = false;

return result;
}


14 comments :

  1. public boolean sameEnds(int[] nums, int len) {
    for (int i=len-1; i>=0; i--)
    {
    if(nums[(len-1)-i]!=nums[(nums.length-1)-i]) return false;
    }
    return true;
    }

    ReplyDelete
  2. public boolean sameEnds(int[] nums, int len) {
    for(int i = 0, j = nums.length-len; i<len; i++, j++){
    if(nums[i] != nums[j])
    return false;
    }
    return true;
    }

    ReplyDelete
  3. The person above beats me... what I had

    public boolean sameEnds(int[] nums, int len) {
    for (int i = 0; i < len; i++) {
    if ((nums[i] != nums[nums.length - len + i])) {
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  4. The person above wins..

    public boolean sameEnds(int[] nums, int len) {
    for (int i = 0; i < len; i++) {
    if ((nums[i] != nums[nums.length - len + i])) {
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  5. With simple logic:

    public boolean sameEnds(int[] nums, int len) {
    int[] arr = new int[len];
    if(len == 0) {
    return true;
    }
    if(len == nums.length) {
    return true;
    }
    for(int i = 0; i < nums.length; i++) {
    if(i == len) {
    for(int j = 0; j < i; j++) {
    arr[j] = nums[j];
    }
    }
    }
    if(arr[arr.length - 1] == nums[nums.length - 1])
    return true;
    else return false;
    }

    ReplyDelete
  6. a bit different approach, it's not memory friendly, but it works

    String ff="";
    String bb="";

    for (int i = 0; i < len; i++) {


    ff+=nums[i];
    bb+=nums[nums.length - i - 1];

    }

    StringBuilder back= new StringBuilder();
    back.append(bb);
    back=back.reverse();


    return ff.equals(back.toString());

    ReplyDelete
  7. public boolean sameEnds(int[] nums, int len) {
    int a[]=new int[len];
    int b[]=new int[len];
    for(int i=0;i<len;i++){
    a[i]=nums[i];
    b[i]=nums[(nums.length-len)+i];
    }
    return Arrays.equals(a,b);
    }




    MAKE IT EASY,DONT MAKE IT LARGE ;)

    ReplyDelete
  8. public static boolean sameEnds(int[] nums, int len) {

    int[] firstPart = new int[len];
    int[] lastPart = new int[len];
    if(nums.length==len || len==0){
    return true;
    }
    for(int i=0; i<len; i++){
    firstPart[i] = nums[i];
    }
    for(int i=0; i<len; i++){
    lastPart[i] = nums[nums.length-len+i];
    }

    return Arrays.equals(firstPart,lastPart);

    }

    ReplyDelete
  9. public boolean sameEnds(int[] nums, int len) {
    int[] a = new int[len];
    int[] b = new int[len];
    for (int i = 0, j = nums.length - len; i < len; i++, j++) {
    a[i] = nums[i];
    b[i] = nums[j];
    }
    return (Arrays.equals(a, b));
    }

    ReplyDelete
  10. With Java Stream and Collections

    ArrayList numbers = (ArrayList) Arrays.stream(nums).boxed().collect(Collectors.toList());
    ArrayList first = new ArrayList(numbers.subList(0, len));
    ArrayList last = new ArrayList(numbers.subList(numbers.size() - len, numbers.size()));
    return first.equals(last);

    ReplyDelete
  11. public boolean sameEnds(int[] nums, int len) {
    int countA = 0;
    int counta = 0;
    int countb = 0;
    int countB = 0;
    int num = nums.length - len;

    for(int i = 0; i < len; i++){
    countA += nums[i];
    counta = nums[0];
    }
    for(int i = num; i < nums.length; i++){
    countB += nums[i];
    countb = nums[num];
    }

    if(countA == countB && counta == countb){
    return true;
    }
    return false;
    }

    ReplyDelete
  12. public boolean sameEnds(int[] nums, int len) {
    String str="";
    String str2="";
    for(int i=0; i<len;i++) {
    str+=Integer.toString(nums[i]);
    }
    for(int i=nums.length-len; i<nums.length;i++) {
    str2+=Integer.toString(nums[i]);
    }
    return (str.equals(str2));
    }

    ReplyDelete
  13. More simplied
    public boolean sameEnds(int[] nums, int len) {
    for(int i = 0; i < len; i++) {
    if(!(nums[i] == nums[nums.length - len + i])) return false;
    }
    return true;
    }

    ReplyDelete

  14. public boolean sameEnds(int[] nums, int len) {
    int count=0;
    for(int j=0;j<len;j++){
    if(nums[j]==nums[nums.length-len+j]){
    count++;
    }
    }
    return count==len;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact