Slice an Array (with.Java)
In this article, we learned how to slice an array, and how to determine and control the index siphoning of a given array by query value.
We’ll do this by solving a coding test problem, reflecting on the problem we solved, and exploring other ways to solve it.
Let’s start with the problem
Problem
You are given an array of integers, arr, and a query, query.
Repeat the following operations while traversing query.
At even indices, truncate and discard the trailing query[i] indices in arr, except for query[i].
For odd indices, truncate and discard the portion of arr before query[i], excluding query[i] from arr.
After doing the above, complete the solution function to return a partial array of the remaining arr.
Example input and output
arr: [0, 1, 2, 3, 4, 5]
query: [4,1,2]
result: [1, 2, 3]
My solution to the problem
import java.util.*;
class Solution {
public int[] solution(int[] arr, int[] query) {
for(int i = 0; i < query.length; i++) {
if(i % 2 == 0) {
arr = Arrays.copyOfRange(arr, 0, query[i] + 1);
} else {
arr = Arrays.copyOfRange(arr, query[i], arr.length);
}
}
} return arr;
}
}
solution Description
The solution function takes two integer arrays, arr and query, as input. arr is the target array to manipulate, and query contains the index information to perform the manipulation.
At the beginning of the function, loop the length of query and perform the following operations:
For even indices: When the loop variable i is even, remove all subsequent elements from arr, including the query[i]th index. For example, if arr = [0, 1, 2, 3, 4, 5] and query[i] is 2, the result is [0, 1, 2].
For odd indices: When i is odd, leave the query[i]th index to the end of arr, and remove all elements before it. For example, if arr = [0, 1, 2, 3, 4, 5] and query[i] is 2, the result would be [2, 3, 4, 5].
If you do this for every element in query, you’ll end up with a partial array of the remaining arr.
Java’s Arrays.copyOfRange method makes it easy to copy a specific range of an array, allowing you to perform the above operations efficiently.
See
If the array in arr has a fixed size and you use Arrays.copyOfRange, the elements will be reduced. **Will the size of the array decrease as well?
The Arrays.copyOfRange method creates a new array by extracting a specified range of elements from the original array. Therefore, the size of the new array created will match the length of the specified range.
For example, calling Arrays.copyOfRange(arr, 0, 3) creates a new array by copying the elements from index 0 to index 2 of the original array arr. The size of the new array will therefore be 3.
In this way, Arrays.copyOfRange makes it easy to extract the elements of a desired range and create a new array sized to fit that range. So in this problem, the size of arr will continue to fluctuate based on the elements in the query.
Good luck.