Saturday, September 27, 2014

Array Examles same Ends

Comment: The below mentioned example uses some very basic concept of arrays all the while testing the logic. The question is in response to a question posed in battery of questions in codingbat.com

Ques: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.

Ans:
public boolean sameEnds(int[] nums, int len) {
 int[] hnums = new int[len== 0 ? 2 * len + 1 : 2 * len];
        int[] hrnums = new int[len == 0 ? 2 * len + 1 : 2 * len];
        int count = 0;
              for (int k = 0; k < len; k++) {
            if (nums[k] == nums[k + nums.length - len]) {
                 }
            }

        return count == len ? true : false;
}

No comments:

Post a Comment