We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] A = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
A[i] = Integer.parseInt(st.nextToken());
}
// Process queries
for (int i = 0; i < q; i++) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
Arrays.sort(A, l, r + 1);
}
The code first reads the input values: the size of the array (n), the number of queries (q), and the target index (k).
It reads the array elements into an array A[].
Then, it iterates through each query, sorting the subsegment of array A[] specified by the query's range.
After processing all queries, it prints the value at index k in the array A[].
The program follows a straightforward flow, reading input, processing queries, and finally outputting the desired value, adhering to the problem's constraints and requirements.
// Print the value at index k
System.out.println(A[k]);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sorted Subsegments
You are viewing a single comment's thread. Return to all comments →
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer;
public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken());
The code first reads the input values: the size of the array (n), the number of queries (q), and the target index (k). It reads the array elements into an array A[]. Then, it iterates through each query, sorting the subsegment of array A[] specified by the query's range. After processing all queries, it prints the value at index k in the array A[]. The program follows a straightforward flow, reading input, processing queries, and finally outputting the desired value, adhering to the problem's constraints and requirements.
}