{"id":"3fc2f6b6-edd8-42f0-831a-d176d79802bd","name":"Max In An Interval - Range Query","description":"You are given an array(of integers) of length n.\r\nYou are required to answer q queries.\r\nIn each query u are given an interval l, r both inclusive and u have to find the maximum element in this interval.\r\n\r\nTo do the above task u have to create a datastructure as follows :-\r\n\r\nImplement the SegmentTree class:\r\n1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array,\r\n2. int query(int l, int r): return max in interval [l, r].\r\n\r\nCan u do it in O(log(n)) or better Time Complexity.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number q\r\nfollowing q lines contains queries of format\r\nl r","outputFormat":"for each query print a single integer in seperate line","constraints":"1. 1 &lt;= n, q &lt;= 10^5\r\n2. 0 &lt;= l &lt;= r &lt; n\r\n3. 10^9 &lt;= arr[i] &lt;= 10^9\r\n4. all input and output will fit in 32bit signed integer","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class SegmentTree {\r\n\r\n SegmentTree(int arr[]) {\r\n\r\n }\r\n\r\n int query(int l, int r) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n SegmentTree obj = new SegmentTree(arr);\r\n\r\n int q = Integer.parseInt(read.readLine());\r\n\r\n StringBuilder out = new StringBuilder();\r\n while (q-- > 0) {\r\n String[]inp = read.readLine().split(\" \");\r\n\r\n int l = Integer.parseInt(inp[0]);\r\n int r = Integer.parseInt(inp[1]);\r\n\r\n int ans = obj.query(l, r);\r\n out.append(ans);\r\n out.append(\"\\n\");\r\n }\r\n\r\n System.out.println(out);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n8\r\n7\r\n4\r\n2\r\n5\r\n3\r\n1\r\n10\r\n4\r\n0 7\r\n0 3\r\n2 7\r\n1 6\r\n","sampleOutput":"10\r\n8\r\n10\r\n7\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9b3d7d76-b2ca-458f-80a8-4b37d548024a","name":"Segment Tree For Experts","slug":"segment-tree-for-experts-953","type":0},{"id":"6a2a82cc-7b41-487b-8d34-a25bc9250b10","name":"Max In An Interval - Range Query","slug":"max-in-an-interval-range-query","type":1}],"next":{"id":"1761e131-94fb-4631-8e27-e7bf0e70621a","name":"Sum Of Interval","type":1,"slug":"sum-of-interval"},"prev":{"id":"374e28dd-65a3-41fe-9210-4a0478313b00","name":"Complex Queries","type":1,"slug":"complex-queries-18"}}}

Max In An Interval - Range Query

You are given an array(of integers) of length n. You are required to answer q queries. In each query u are given an interval l, r both inclusive and u have to find the maximum element in this interval. To do the above task u have to create a datastructure as follows :- Implement the SegmentTree class: 1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array, 2. int query(int l, int r): return max in interval [l, r]. Can u do it in O(log(n)) or better Time Complexity.

{"id":"3fc2f6b6-edd8-42f0-831a-d176d79802bd","name":"Max In An Interval - Range Query","description":"You are given an array(of integers) of length n.\r\nYou are required to answer q queries.\r\nIn each query u are given an interval l, r both inclusive and u have to find the maximum element in this interval.\r\n\r\nTo do the above task u have to create a datastructure as follows :-\r\n\r\nImplement the SegmentTree class:\r\n1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array,\r\n2. int query(int l, int r): return max in interval [l, r].\r\n\r\nCan u do it in O(log(n)) or better Time Complexity.","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number q\r\nfollowing q lines contains queries of format\r\nl r","outputFormat":"for each query print a single integer in seperate line","constraints":"1. 1 &lt;= n, q &lt;= 10^5\r\n2. 0 &lt;= l &lt;= r &lt; n\r\n3. 10^9 &lt;= arr[i] &lt;= 10^9\r\n4. all input and output will fit in 32bit signed integer","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class SegmentTree {\r\n\r\n SegmentTree(int arr[]) {\r\n\r\n }\r\n\r\n int query(int l, int r) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n SegmentTree obj = new SegmentTree(arr);\r\n\r\n int q = Integer.parseInt(read.readLine());\r\n\r\n StringBuilder out = new StringBuilder();\r\n while (q-- > 0) {\r\n String[]inp = read.readLine().split(\" \");\r\n\r\n int l = Integer.parseInt(inp[0]);\r\n int r = Integer.parseInt(inp[1]);\r\n\r\n int ans = obj.query(l, r);\r\n out.append(ans);\r\n out.append(\"\\n\");\r\n }\r\n\r\n System.out.println(out);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n8\r\n7\r\n4\r\n2\r\n5\r\n3\r\n1\r\n10\r\n4\r\n0 7\r\n0 3\r\n2 7\r\n1 6\r\n","sampleOutput":"10\r\n8\r\n10\r\n7\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9b3d7d76-b2ca-458f-80a8-4b37d548024a","name":"Segment Tree For Experts","slug":"segment-tree-for-experts-953","type":0},{"id":"6a2a82cc-7b41-487b-8d34-a25bc9250b10","name":"Max In An Interval - Range Query","slug":"max-in-an-interval-range-query","type":1}],"next":{"id":"1761e131-94fb-4631-8e27-e7bf0e70621a","name":"Sum Of Interval","type":1,"slug":"sum-of-interval"},"prev":{"id":"374e28dd-65a3-41fe-9210-4a0478313b00","name":"Complex Queries","type":1,"slug":"complex-queries-18"}}}
plane

Editor


Loading...

Max In An Interval - Range Query

medium

You are given an array(of integers) of length n. You are required to answer q queries. In each query u are given an interval l, r both inclusive and u have to find the maximum element in this interval. To do the above task u have to create a datastructure as follows :- Implement the SegmentTree class: 1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array, 2. int query(int l, int r): return max in interval [l, r]. Can u do it in O(log(n)) or better Time Complexity.

Constraints

1. 1 <= n, q <= 10^5 2. 0 <= l <= r < n 3. 10^9 <= arr[i] <= 10^9 4. all input and output will fit in 32bit signed integer

Format

Input

A number n n1 n2 .. n number of elements A number q following q lines contains queries of format l r

Output

for each query print a single integer in seperate line

Example

Sample Input

8 8 7 4 2 5 3 1 10 4 0 7 0 3 2 7 1 6

Sample Output

10 8 10 7

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode