`{"id":"fbd656a2-325c-4632-907a-ce6c6a8241e7","name":"Sparse Table","description":"<a href=\"https://www.spoj.com/problems/RMQSQ/\">https://www.spoj.com/problems/RMQSQ/</a>","inputFormat":".","outputFormat":".","constraints":".","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static InputReader in = new InputReader(System.in);\r\n static PrintWriter out = new PrintWriter(System.out);\r\n /*\r\n input functions in.nextInt(); in.nextIntArray(n); in.nextIntArray1(n);\r\n output fnctions out.println(); out.print();\r\n\r\n use in for reading input\r\n use out for printing output\r\n */\r\n\r\n\r\n public static void main(String[] args) throws IOException {\r\n\r\n // write your code here.\r\n\r\n\r\n out.close();\r\n }\r\n\r\n private static class InputReader implements AutoCloseable {\r\n\r\n private static final int DEFAULT_BUFFER_SIZE = 1 << 16;\r\n\r\n\r\n private static final InputStream DEFAULT_STREAM = System.in;\r\n\r\n private static final int MAX_DECIMAL_PRECISION = 21;\r\n\r\n private int c;\r\n\r\n private final byte[] buf;\r\n private final int bufferSize;\r\n private int bufIndex;\r\n private int numBytesRead;\r\n\r\n private final InputStream stream;\r\n\r\n private static final byte EOF = -1;\r\n\r\n private static final byte NEW_LINE = 10;\r\n\r\n private static final byte SPACE = 32;\r\n\r\n private static final byte DASH = 45;\r\n\r\n private static final byte DOT = 46;\r\n private char[] charBuffer;\r\n private static final byte[] bytes = new byte[58];\r\n private static final int[] ints = new int[58];\r\n private static final char[] chars = new char[128];\r\n\r\n static {\r\n char ch = '' '';\r\n int value = 0;\r\n byte _byte = 0;\r\n for (int i = 48; i < 58; i++)\r\n bytes[i] = _byte++;\r\n for (int i = 48; i < 58; i++)\r\n ints[i] = value++;\r\n for (int i = 32; i < 128; i++)\r\n chars[i] = ch++;\r\n }\r\n\r\n public InputReader() {\r\n this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(int bufferSize) {\r\n this(DEFAULT_STREAM, bufferSize);\r\n }\r\n public InputReader(InputStream stream) {\r\n this(stream, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(InputStream stream, int bufferSize) {\r\n if (stream == null || bufferSize <= 0)\r\n throw new IllegalArgumentException();\r\n buf = new byte[bufferSize];\r\n charBuffer = new char[128];\r\n this.bufferSize = bufferSize;\r\n this.stream = stream;\r\n }\r\n private byte read() throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n throw new IOException();\r\n\r\n if (bufIndex >= numBytesRead) {\r\n bufIndex = 0;\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n }\r\n\r\n return buf[bufIndex++];\r\n }\r\n private int readJunk(int token) throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > token)\r\n return 0;\r\n bufIndex++;\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public byte nextByte() throws IOException {\r\n return (byte) nextInt();\r\n }\r\n public int nextInt() throws IOException {\r\n\r\n if (readJunk(DASH - 1) == EOF)\r\n throw new IOException();\r\n int sgn = 1, res = 0;\r\n\r\n c = buf[bufIndex];\r\n if (c == DASH) {\r\n sgn = -1;\r\n bufIndex++;\r\n }\r\n\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > SPACE) {\r\n res = (res << 3) + (res << 1);\r\n res += ints[buf[bufIndex++]];\r\n } else {\r\n bufIndex++;\r\n return res * sgn;\r\n }\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return res * sgn;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public int[] nextIntArray(int n) throws IOException {\r\n int[] ar = new int[n];\r\n for (int i = 0; i < n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public int[] nextIntArray1(int n) throws IOException {\r\n int[] ar = new int[n + 1];\r\n for (int i = 1; i <= n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public void close() throws IOException {\r\n stream.close();\r\n }\r\n\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1 4 1\r\n2\r\n1 1\r\n1 2\r\n","sampleOutput":"4\r\n1\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":"ed294401-c1be-4c10-a3f7-3e2e4f8d9755","name":"Range Queries For Experts","slug":"range-queries-for-experts-903","type":0},{"id":"6994e031-60ea-4219-843b-2b48a9a7e132","name":"Sparse Table","slug":"sparse-table","type":1}],"next":null,"prev":{"id":"239cc04b-e6e3-4d31-b45e-7eb280c3807b","name":"Trampoline","type":1,"slug":"trampoline"}}}`

# Sparse Table

<a href="https://www.spoj.com/problems/RMQSQ/">https://www.spoj.com/problems/RMQSQ/</a>

`{"id":"fbd656a2-325c-4632-907a-ce6c6a8241e7","name":"Sparse Table","description":"<a href=\"https://www.spoj.com/problems/RMQSQ/\">https://www.spoj.com/problems/RMQSQ/</a>","inputFormat":".","outputFormat":".","constraints":".","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static InputReader in = new InputReader(System.in);\r\n static PrintWriter out = new PrintWriter(System.out);\r\n /*\r\n input functions in.nextInt(); in.nextIntArray(n); in.nextIntArray1(n);\r\n output fnctions out.println(); out.print();\r\n\r\n use in for reading input\r\n use out for printing output\r\n */\r\n\r\n\r\n public static void main(String[] args) throws IOException {\r\n\r\n // write your code here.\r\n\r\n\r\n out.close();\r\n }\r\n\r\n private static class InputReader implements AutoCloseable {\r\n\r\n private static final int DEFAULT_BUFFER_SIZE = 1 << 16;\r\n\r\n\r\n private static final InputStream DEFAULT_STREAM = System.in;\r\n\r\n private static final int MAX_DECIMAL_PRECISION = 21;\r\n\r\n private int c;\r\n\r\n private final byte[] buf;\r\n private final int bufferSize;\r\n private int bufIndex;\r\n private int numBytesRead;\r\n\r\n private final InputStream stream;\r\n\r\n private static final byte EOF = -1;\r\n\r\n private static final byte NEW_LINE = 10;\r\n\r\n private static final byte SPACE = 32;\r\n\r\n private static final byte DASH = 45;\r\n\r\n private static final byte DOT = 46;\r\n private char[] charBuffer;\r\n private static final byte[] bytes = new byte[58];\r\n private static final int[] ints = new int[58];\r\n private static final char[] chars = new char[128];\r\n\r\n static {\r\n char ch = '' '';\r\n int value = 0;\r\n byte _byte = 0;\r\n for (int i = 48; i < 58; i++)\r\n bytes[i] = _byte++;\r\n for (int i = 48; i < 58; i++)\r\n ints[i] = value++;\r\n for (int i = 32; i < 128; i++)\r\n chars[i] = ch++;\r\n }\r\n\r\n public InputReader() {\r\n this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(int bufferSize) {\r\n this(DEFAULT_STREAM, bufferSize);\r\n }\r\n public InputReader(InputStream stream) {\r\n this(stream, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(InputStream stream, int bufferSize) {\r\n if (stream == null || bufferSize <= 0)\r\n throw new IllegalArgumentException();\r\n buf = new byte[bufferSize];\r\n charBuffer = new char[128];\r\n this.bufferSize = bufferSize;\r\n this.stream = stream;\r\n }\r\n private byte read() throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n throw new IOException();\r\n\r\n if (bufIndex >= numBytesRead) {\r\n bufIndex = 0;\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n }\r\n\r\n return buf[bufIndex++];\r\n }\r\n private int readJunk(int token) throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > token)\r\n return 0;\r\n bufIndex++;\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public byte nextByte() throws IOException {\r\n return (byte) nextInt();\r\n }\r\n public int nextInt() throws IOException {\r\n\r\n if (readJunk(DASH - 1) == EOF)\r\n throw new IOException();\r\n int sgn = 1, res = 0;\r\n\r\n c = buf[bufIndex];\r\n if (c == DASH) {\r\n sgn = -1;\r\n bufIndex++;\r\n }\r\n\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > SPACE) {\r\n res = (res << 3) + (res << 1);\r\n res += ints[buf[bufIndex++]];\r\n } else {\r\n bufIndex++;\r\n return res * sgn;\r\n }\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return res * sgn;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public int[] nextIntArray(int n) throws IOException {\r\n int[] ar = new int[n];\r\n for (int i = 0; i < n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public int[] nextIntArray1(int n) throws IOException {\r\n int[] ar = new int[n + 1];\r\n for (int i = 1; i <= n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public void close() throws IOException {\r\n stream.close();\r\n }\r\n\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1 4 1\r\n2\r\n1 1\r\n1 2\r\n","sampleOutput":"4\r\n1\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":"ed294401-c1be-4c10-a3f7-3e2e4f8d9755","name":"Range Queries For Experts","slug":"range-queries-for-experts-903","type":0},{"id":"6994e031-60ea-4219-843b-2b48a9a7e132","name":"Sparse Table","slug":"sparse-table","type":1}],"next":null,"prev":{"id":"239cc04b-e6e3-4d31-b45e-7eb280c3807b","name":"Trampoline","type":1,"slug":"trampoline"}}}`

Editor

# Sparse Table

medium

https://www.spoj.com/problems/RMQSQ/

.

.

.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 1 4 1 2 1 1 1 2 ```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4 1 ```

Discussions

Show Discussion

Related Resources