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.
- Prepare
- Algorithms
- Search
- Sorted Subsegments
- Discussions
Sorted Subsegments
Sorted Subsegments
Sort by
recency
|
37 Discussions
|
Please Login in order to post a comment
The below code fails in 11 test cases only it pases 22 cases successfully. I think there is small issue in it if anyone can resolve it resolve. A simple approach but better results.....
def sortedSubsegments(k, a, queries): s = [] for st, end in queries:
if st <= k <= end:
s.append(st) s.append(end) idx=max(s) a[:idx]=sorted(a[:idx+1]) return a[k]
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.
}
Here is my solution in java, Python, C, C++, Csharp HackerRank Sorted Subsegments Problem Solution
Here is the solution of Sorted Subsegments Click Here
https://zeroplusfour.com/sorted-subsegments-hackerrank-solution/
Here's how I did in all languages Java 8 , C++ , C , Python 3, Python 2.