1

https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/range-minimum-query/description/

I am trying to solve this question. I am making the vector size of Math.ceil(Math.sqrt(arrSize)). I have used the following methods - For constructing sqrt vector

I am taking square root chunks and finding the smallest index in the block and storing them in the vect array.

How can I improve my update query complexity from Sqrt(n).

static void update(int[] arr, int[] vect, int index, int elem) {
    arr[index] = elem;
    int len = vect.length;
    int inIndex = ((index / len) * len);
    int finalIndex = inIndex+len;
    int min = Integer.MAX_VALUE;
    int in = -1;
    for(int i = inIndex; i < finalIndex && i < arr.length; ++i) {
        if(arr[i] < min) {
            min = arr[i];
            in = i;
        }
    }
    vect[index/len] = in;

}

used this tutorial -: http://www.geeksforgeeks.org/sqrt-square-root-decomposition-technique-set-1-introduction/

Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
  • 2
    You have posted way to much code in your question, which makes it unclear to us (and to future readers) exactly where the problem is. Please reduce your problem code to 10 lines or less. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Sep 10 '17 at 16:34
  • I am not getting in which part the problem is. I have tested on small arrays and it is working fine but I am unable to figure out where the problem is – Brij Raj Kishore Sep 10 '17 at 16:37
  • Then you need to spend some time in a debugger. When you're done that, spend a bit more time. And then after that, even more time. Until you've narrowed your question down to an MCVE, then community is not going to help you. – Joe C Sep 10 '17 at 16:39
  • @JoeC What is MCVE? – Brij Raj Kishore Sep 10 '17 at 16:41
  • Minimal, Complete and Verifiable Example. In your case, you need to focus on the Minimal bit. – Joe C Sep 10 '17 at 16:42
  • @JoeC Can You Provide me some test case to see where I am wrong? – Brij Raj Kishore Sep 10 '17 at 16:45
  • No. You already have one. It's the wrong answer you're getting on the site. – Joe C Sep 10 '17 at 16:46
  • @JoeC I am sorry, there are two test cases in which one I am getting wrong ans. And the one which they have given the example is my correct ans. The one which is wrong. I cannot see the test cases. – Brij Raj Kishore Sep 10 '17 at 16:49
  • Then I cannot help you. – Joe C Sep 10 '17 at 16:50
  • @JoeC I am getting nowhere. I have already tried many test cases of my own. I am failing to understand where it is going wrong. – Brij Raj Kishore Sep 10 '17 at 18:01
  • @JoeC Now I have corrected my mistake. Now Please help. – Brij Raj Kishore Sep 10 '17 at 19:06
  • If you want to improve the complexity then you should use a segment tree. That's why the problem is in the segment tree section. The description on that site is already excellent: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/tutorial/ – Matt Timmermans Sep 11 '17 at 02:25
  • I have already done this question using segment tree. I want to find out whether my query can be improved or not. I think not. – Brij Raj Kishore Sep 11 '17 at 03:10

1 Answers1

1

If you have to improve complexity you have to use Segment Trees. In this case you cannot directly update the index of the vect array like in case of range sum query. You have to find again the minimum of the block.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gaurav
  • 117
  • 9