public class Submission {
public static int[] solve(int[]array, int k){
int[] result = new int[k];
int counter = k;
for(int i = array.length - k; i < array.length; i++){
result[k-counter--] = array[i];
}
return result;
}
}
Arthur Lovea year ago
public class Submission { public static int[] solve(int[]array, int k){ int[] result = new int[k]; int counter = k; for(int i = array.length - k; i < array.length; i++){ result[k-counter--] = array[i]; } return result; } }
this is my code with for loop
`const solve = (intArray, k) => {
let result = [];
for (let i = 0 ; i < k;i++){
let n = intArray.length -1;
result.push(intArray[n-i]);
}
return result.reverse();
};
`
Mohamed El Ghannay3 years ago
my solution using unshift Array method:
``const solve = (intArray, k) => {
let result = [];
let index = intArray.length - 1;
while (k > 0) {
result.unshift(intArray[index]);
k--;
index--;
}
return result;
};
Mohamed El Ghannay3 years ago
``const solve = (intArray, k) => {
let result = [];
let index = intArray.length - 1;
while (k > 0) {
result.unshift(intArray[index]);
k--;
index--;
}
return result;
};
Mohamed El Ghannay3 years ago
const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };
const solve = (intArray, k) => {
let index = 2;
while (index < intArray.length) {
return results = intArray.slice(index, index + k);
index += k;
}
};
I am being told its not quite right because its returning [3,4] instead of [1,2,3,4]. Now I am confused, is that not what Example # 1 asks us to return?
Bacon Chair Palladium4 years ago
I'm not sure why slice(k) does not work because if I try it on console [1,2,3,4].slice(2), it returns [3,4]
Bacon Chair Palladium4 years ago
nevermind, I figured it out
benchmark Turkmenistan Granite4 years ago
my solution with [array.prototype.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) :
const solve = (intArray, k) => intArray.slice(-k)
Michael Morgan4 years ago
Here is my simple solution:
const solve = (intArray, k) => {
return intArray.splice(intArray.length - k);
};
Applications cyan4 years ago
Interesting I used slice and they both work the same. The only difference appears to be that slice creates an new array and splice mutates the original array.
DennisLoska4 years ago
Here's my solution. The tricky part was to not forget, that you ALWAYS need to return something in the callback of your reduce() function, otherwise you will wonder like me, why your accumulator stays undefined.
Comments
Arthur Lovea year ago
Java implementation
public class Submission { public static int[] solve(int[]array, int k){ int[] result = new int[k]; int counter = k; for(int i = array.length - k; i < array.length; i++){ result[k-counter--] = array[i]; } return result; } }
Arthur Lovea year ago
public class Submission { public static int[] solve(int[]array, int k){ int[] result = new int[k]; int counter = k; for(int i = array.length - k; i < array.length; i++){ result[k-counter--] = array[i]; } return result; } }
Corporate Qualitya year ago
const solve = (intArray, k) => { const endOfArray = []; for (let i = intArray.length -1 ; i >= 0; i--) { k--; endOfArray.unshift(intArray[i]); if(k === 0){ break; } } return endOfArray; };
Berkshire Senior SCSIa year ago
const solve = (intArray, k) => { return intArray.slice(- k) ; };
e v ma year ago
Salah El-Shennawya year ago
first solution =>
const solve = (intArray, k) => { return intArray.reverse().splice(0, k).reverse() };
second solution =>
const solve = (intArray, k) => { return intArray.splice(intArray.length - k, k) };
calculating feed2 years ago
const solve = (intArray, k) => { return intArray.splice(intArray.length-k,k); };
Berkshire revolutionary South Carolina2 years ago
const solve = (intArray, k) => { let newArray = []; for (i = intArray.length - k; i <= intArray.length - 1; i++) { newArray.push(intArray[i]); } return newArray; };
Human Mouse ivory2 years ago
const solve = (intArray, k) => { let r = intArray.length-k; let res; for(let i=r;i<=intArray.length-1;i++){ res = console.log(intArray[i]); } return res; };
plum invoice2 years ago
return intArray.slice(k - intArray.length);
Fatih KAYA2 years ago
const solve = (intArray, k) => { let newArr = intArray.reverse().slice(0,k).reverse(); return newArr; };
Richard O'Brien2 years ago
This one-liner does the trick
const solve = (intArray, k) => intArray.slice(intArray.length - k);
Arvind Pal2 years ago
Shekhar Patil2 years ago
const solve = (intArray, k) => intArray.slice(-k);
Unbranded online Borders2 years ago
const solve = (intArray, k) => intArray.slice(k * -1);
Unbranded online Borders2 years ago
const solve = (intArray, k) => { const arrLen = intArray.length; for(let i = 0; i < arrLen - k; i++) { intArray.shift(); } return intArray; };
Qamar Khurshid2 years ago
tayebe kavousi3 years ago
this is my code with for loop `const solve = (intArray, k) => { let result = []; for (let i = 0 ; i < k;i++){ let n = intArray.length -1; result.push(intArray[n-i]); } return result.reverse(); };
`
Mohamed El Ghannay3 years ago
my solution using unshift Array method: ``const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };
Mohamed El Ghannay3 years ago
``const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };
Mohamed El Ghannay3 years ago
const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };
Narek Musakhanyan3 years ago
tayebe kavousi3 years ago
this is much better
invoice Avon3 years ago
this worked..
Mochammad Andrian Maulana3 years ago
const solve = (intArray, k) => intArray.splice(intArray.length-k,k);
Daniel Cortes3 years ago
Here is my solution:
I am being told its not quite right because its returning
[3,4]
instead of[1,2,3,4]
. Now I am confused, is that not what Example # 1 asks us to return?Bacon Chair Palladium4 years ago
I'm not sure why slice(k) does not work because if I try it on console [1,2,3,4].slice(2), it returns [3,4]
Bacon Chair Palladium4 years ago
nevermind, I figured it out
benchmark Turkmenistan Granite4 years ago
my solution with [array.prototype.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) :
Michael Morgan4 years ago
Here is my simple solution: const solve = (intArray, k) => { return intArray.splice(intArray.length - k); };
Applications cyan4 years ago
Interesting I used slice and they both work the same. The only difference appears to be that slice creates an new array and splice mutates the original array.
DennisLoska4 years ago
Here's my solution. The tricky part was to not forget, that you ALWAYS need to return something in the callback of your reduce() function, otherwise you will wonder like me, why your accumulator stays undefined.
Abdulhamid Abdo4 years ago
This is too complicated. I think.