Finding minimum index of element in array range that gives maximum xor with given value









up vote
-3
down vote

favorite












We have an array(elements/numbers are given in binary form/string format) and there are multiple queries of the form:




Q(L,R,X) = smallest index i (L ≤ i ≤ R) such that
the value Arr[i]⊕X is maximum possible,
equal to max(Arr[L]⊕X,Arr[L+1]⊕X,…,Arr[R]⊕X)




See below example for clarification:



arr[5] = 11,100,101,101,1

Q(2,4,10) = 2
since arr[2]⊕10 = arr[3]⊕10
is maximum possible xor and index 2 < 3


In my approach, I have used trie data structure. While querying I do a binary search if the current node of trie has index in range [L,R] while iterating over the bits of number X from most significant bit to least significant bit(look for inverse of bits, since that only maximises the xor).



This results in answering of queries in O(length(X) * log(N)) where N is the size of array.




The difficult part is numbers in array are of different length(size of bits) and same for X in the queries. You can handle this by pre-processing(appending 0s in front of all numbers) but this increases time complexity of the solution.




Is there any faster way to do this?










share|improve this question























  • I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
    – jwimberley
    Nov 9 at 21:52






  • 1




    I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
    – jwimberley
    Nov 9 at 22:07










  • Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
    – jwimberley
    Nov 9 at 22:09










  • You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
    – nightfury1204
    Nov 10 at 6:45














up vote
-3
down vote

favorite












We have an array(elements/numbers are given in binary form/string format) and there are multiple queries of the form:




Q(L,R,X) = smallest index i (L ≤ i ≤ R) such that
the value Arr[i]⊕X is maximum possible,
equal to max(Arr[L]⊕X,Arr[L+1]⊕X,…,Arr[R]⊕X)




See below example for clarification:



arr[5] = 11,100,101,101,1

Q(2,4,10) = 2
since arr[2]⊕10 = arr[3]⊕10
is maximum possible xor and index 2 < 3


In my approach, I have used trie data structure. While querying I do a binary search if the current node of trie has index in range [L,R] while iterating over the bits of number X from most significant bit to least significant bit(look for inverse of bits, since that only maximises the xor).



This results in answering of queries in O(length(X) * log(N)) where N is the size of array.




The difficult part is numbers in array are of different length(size of bits) and same for X in the queries. You can handle this by pre-processing(appending 0s in front of all numbers) but this increases time complexity of the solution.




Is there any faster way to do this?










share|improve this question























  • I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
    – jwimberley
    Nov 9 at 21:52






  • 1




    I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
    – jwimberley
    Nov 9 at 22:07










  • Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
    – jwimberley
    Nov 9 at 22:09










  • You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
    – nightfury1204
    Nov 10 at 6:45












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











We have an array(elements/numbers are given in binary form/string format) and there are multiple queries of the form:




Q(L,R,X) = smallest index i (L ≤ i ≤ R) such that
the value Arr[i]⊕X is maximum possible,
equal to max(Arr[L]⊕X,Arr[L+1]⊕X,…,Arr[R]⊕X)




See below example for clarification:



arr[5] = 11,100,101,101,1

Q(2,4,10) = 2
since arr[2]⊕10 = arr[3]⊕10
is maximum possible xor and index 2 < 3


In my approach, I have used trie data structure. While querying I do a binary search if the current node of trie has index in range [L,R] while iterating over the bits of number X from most significant bit to least significant bit(look for inverse of bits, since that only maximises the xor).



This results in answering of queries in O(length(X) * log(N)) where N is the size of array.




The difficult part is numbers in array are of different length(size of bits) and same for X in the queries. You can handle this by pre-processing(appending 0s in front of all numbers) but this increases time complexity of the solution.




Is there any faster way to do this?










share|improve this question















We have an array(elements/numbers are given in binary form/string format) and there are multiple queries of the form:




Q(L,R,X) = smallest index i (L ≤ i ≤ R) such that
the value Arr[i]⊕X is maximum possible,
equal to max(Arr[L]⊕X,Arr[L+1]⊕X,…,Arr[R]⊕X)




See below example for clarification:



arr[5] = 11,100,101,101,1

Q(2,4,10) = 2
since arr[2]⊕10 = arr[3]⊕10
is maximum possible xor and index 2 < 3


In my approach, I have used trie data structure. While querying I do a binary search if the current node of trie has index in range [L,R] while iterating over the bits of number X from most significant bit to least significant bit(look for inverse of bits, since that only maximises the xor).



This results in answering of queries in O(length(X) * log(N)) where N is the size of array.




The difficult part is numbers in array are of different length(size of bits) and same for X in the queries. You can handle this by pre-processing(appending 0s in front of all numbers) but this increases time complexity of the solution.




Is there any faster way to do this?







algorithm optimization trie dynamic-queries






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 6:11

























asked Nov 9 at 18:49









DEV KUMAR

63




63











  • I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
    – jwimberley
    Nov 9 at 21:52






  • 1




    I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
    – jwimberley
    Nov 9 at 22:07










  • Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
    – jwimberley
    Nov 9 at 22:09










  • You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
    – nightfury1204
    Nov 10 at 6:45
















  • I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
    – jwimberley
    Nov 9 at 21:52






  • 1




    I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
    – jwimberley
    Nov 9 at 22:07










  • Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
    – jwimberley
    Nov 9 at 22:09










  • You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
    – nightfury1204
    Nov 10 at 6:45















I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
– jwimberley
Nov 9 at 21:52




I'm interested in the problem, but can't follow the details of your approach. Could you provide a pseudo-code description of the algorithm?
– jwimberley
Nov 9 at 21:52




1




1




I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
– jwimberley
Nov 9 at 22:07




I'm assuming that the complexity you cite does not include the building of the trie (which would have to be at least O(N) times constant factor related to number of bits in each element of array), but reflects a scenario where you build a trie once and then repeatedly reuse for multiple queries Q, correct?
– jwimberley
Nov 9 at 22:07












Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
– jwimberley
Nov 9 at 22:09




Third and last comment for now: Does "if the current node of trie has index in range [L,R]" mean that each node in the trie includes a table of all (L,R) pairs for which there is an entry L<=idx<=R in the current node or any of its descendants?
– jwimberley
Nov 9 at 22:09












You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
– nightfury1204
Nov 10 at 6:45




You keeping all the indexes of array in trie node, right? doen't it will require lots of memoty?
– nightfury1204
Nov 10 at 6:45

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231702%2ffinding-minimum-index-of-element-in-array-range-that-gives-maximum-xor-with-give%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231702%2ffinding-minimum-index-of-element-in-array-range-that-gives-maximum-xor-with-give%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo