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; }
public boolean sameEnds(int[] nums, int len) {
ReplyDeletefor (int i=len-1; i>=0; i--)
{
if(nums[(len-1)-i]!=nums[(nums.length-1)-i]) return false;
}
return true;
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeletefor(int i = 0, j = nums.length-len; i<len; i++, j++){
if(nums[i] != nums[j])
return false;
}
return true;
}
The person above beats me... what I had
ReplyDeletepublic 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;
}
The person above wins..
ReplyDeletepublic 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;
}
With simple logic:
ReplyDeletepublic 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;
}
a bit different approach, it's not memory friendly, but it works
ReplyDeleteString 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());
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint 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 ;)
public static boolean sameEnds(int[] nums, int len) {
ReplyDeleteint[] 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);
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint[] 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));
}
With Java Stream and Collections
ReplyDeleteArrayList 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);
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint 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;
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteString 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));
}
More simplied
ReplyDeletepublic 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;
}
ReplyDeletepublic 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;
}