I Have an array that values in it is :
int[] elements = {4 , 7 , 3 , 6 ,2 ,5 ,1};
The users input is (k,i,j) That K means the Kth index in range (i,j)
If The user's input is (3,0,6) The Answer Must be 3 Cause The 3rd Index in range (0,6) is 3.
How Can I retrieve the Kth index and print The index's value ?
One approach could be creating your own function which takes one subarray from the original one using begin and end indexes.
import java.util.Arrays;
public static int[] subArray(int[] array, int beg, int end) {
return Arrays.copyOfRange(array, beg, end + 1);
}
Then just use below function to access your k - 1 element.
int[] subarray = subArray(arr, beg, end);
System.out.println(subarray[k - 1]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With