Is there a way to mathematically locate the correct node?What is an efficient way to implement a singleton pattern in Java?Fastest way to determine if an integer's square root is an integerWhat's the simplest way to print a Java array?How do I write a correct micro-benchmark in Java?Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA projectEfficient way to find Frequency of a character in a String in java : O(n)Return value in atoi functionHow to directly initialize a HashMap (in a literal way)?Missing Return Statement Error w/ For Loop - JAVAIs there a way to have a single for loop return different values for different user inputs?

Should you avoid redundant information after dialogue?

Do native speakers use ZVE or CPU?

Crab Nebula short story from 1960s or '70s

Will it hurt my career to work as a graphic designer in a startup for beauty and skin care?

Won 50K! Now what should I do with it

As a DM, how to avoid unconscious metagaming when dealing with a high AC character?

Doing research in academia and not liking competition

How are "soeben" and "eben" different from one another?

Construct a pentagon avoiding compass use

What is the German equivalent of 干物女 (dried fish woman)?

How can an advanced civilization forget how to manufacture its technology?

What does a red v with a dot above mean in the Misal rico de Cisneros (Spain, 1518)?

Would letting a multiclass character rebuild their character to be single-classed be game-breaking?

What's the difference between soft PWM and PWM

Possible isometry groups of open manifolds

Why does Hellboy file down his horns?

Is killing off one of my queer characters homophobic?

Are L-functions uniquely determined by their values at negative integers?

Is a public company able to check out who owns its shares in very detailed format?

Equivalent definitions of total angular momentum

Integral clarification

Redox reactions redefined

What are some symbols representing peasants/oppressed persons fighting back?

Project Euler, problem # 9, Pythagorean triplet



Is there a way to mathematically locate the correct node?


What is an efficient way to implement a singleton pattern in Java?Fastest way to determine if an integer's square root is an integerWhat's the simplest way to print a Java array?How do I write a correct micro-benchmark in Java?Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA projectEfficient way to find Frequency of a character in a String in java : O(n)Return value in atoi functionHow to directly initialize a HashMap (in a literal way)?Missing Return Statement Error w/ For Loop - JAVAIs there a way to have a single for loop return different values for different user inputs?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Question from: https://open.kattis.com/problems/numbertree, sourceed from KTH Challenge 2014



This is how the number tree looks like



I used a for loop to iterate through the input string consisting of 'L' and 'R', and curr to store the current variable being evaluated and previous to store the one before this iteration.



if (i == 0) //first 'L' or 'R'
if (curr == 'L') count++;
if (curr == 'R') count +=2 ;



Here, if the current character is the same as the previous one, I increase count by 2 to the power of loops passed, and subtract one from that if the current character is not the same as the previous one.



else if (curr == prev) count += (int) Math.pow(2, h); 
else if (curr != prev) { count += (int) Math.pow(2, h)-1;


At the end of every loop, I'll equate prev to curr and increment h.



This logic worked for the 3 given inputs, 3 LR, 3 RRL, and 2, which returns 11, 2, and 7 respectively.



However it did not pass the hidden test cases. One input I've tried is 3 LLL
which should return 8, but my algorithm returned 3. 3 LRR should return 5, but I got 3.



Is there a solution which uses a data structure to obtain the solution, since the problem quotes "perfect binary trees". Appreciate any help in sorting out the kinks in my logic, as well as other solutions. Thank you!










share|improve this question
























  • Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

    – hyde
    Mar 26 at 6:32











  • Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

    – hyde
    Mar 26 at 6:33












  • Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

    – Teo Jun Xiong
    Mar 26 at 7:11











  • Why would 2 return 7?

    – Mark
    Mar 26 at 8:32











  • When there's no path. it returns the root, 7

    – Teo Jun Xiong
    Mar 26 at 8:35

















0















Question from: https://open.kattis.com/problems/numbertree, sourceed from KTH Challenge 2014



This is how the number tree looks like



I used a for loop to iterate through the input string consisting of 'L' and 'R', and curr to store the current variable being evaluated and previous to store the one before this iteration.



if (i == 0) //first 'L' or 'R'
if (curr == 'L') count++;
if (curr == 'R') count +=2 ;



Here, if the current character is the same as the previous one, I increase count by 2 to the power of loops passed, and subtract one from that if the current character is not the same as the previous one.



else if (curr == prev) count += (int) Math.pow(2, h); 
else if (curr != prev) { count += (int) Math.pow(2, h)-1;


At the end of every loop, I'll equate prev to curr and increment h.



This logic worked for the 3 given inputs, 3 LR, 3 RRL, and 2, which returns 11, 2, and 7 respectively.



However it did not pass the hidden test cases. One input I've tried is 3 LLL
which should return 8, but my algorithm returned 3. 3 LRR should return 5, but I got 3.



Is there a solution which uses a data structure to obtain the solution, since the problem quotes "perfect binary trees". Appreciate any help in sorting out the kinks in my logic, as well as other solutions. Thank you!










share|improve this question
























  • Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

    – hyde
    Mar 26 at 6:32











  • Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

    – hyde
    Mar 26 at 6:33












  • Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

    – Teo Jun Xiong
    Mar 26 at 7:11











  • Why would 2 return 7?

    – Mark
    Mar 26 at 8:32











  • When there's no path. it returns the root, 7

    – Teo Jun Xiong
    Mar 26 at 8:35













0












0








0


1






Question from: https://open.kattis.com/problems/numbertree, sourceed from KTH Challenge 2014



This is how the number tree looks like



I used a for loop to iterate through the input string consisting of 'L' and 'R', and curr to store the current variable being evaluated and previous to store the one before this iteration.



if (i == 0) //first 'L' or 'R'
if (curr == 'L') count++;
if (curr == 'R') count +=2 ;



Here, if the current character is the same as the previous one, I increase count by 2 to the power of loops passed, and subtract one from that if the current character is not the same as the previous one.



else if (curr == prev) count += (int) Math.pow(2, h); 
else if (curr != prev) { count += (int) Math.pow(2, h)-1;


At the end of every loop, I'll equate prev to curr and increment h.



This logic worked for the 3 given inputs, 3 LR, 3 RRL, and 2, which returns 11, 2, and 7 respectively.



However it did not pass the hidden test cases. One input I've tried is 3 LLL
which should return 8, but my algorithm returned 3. 3 LRR should return 5, but I got 3.



Is there a solution which uses a data structure to obtain the solution, since the problem quotes "perfect binary trees". Appreciate any help in sorting out the kinks in my logic, as well as other solutions. Thank you!










share|improve this question
















Question from: https://open.kattis.com/problems/numbertree, sourceed from KTH Challenge 2014



This is how the number tree looks like



I used a for loop to iterate through the input string consisting of 'L' and 'R', and curr to store the current variable being evaluated and previous to store the one before this iteration.



if (i == 0) //first 'L' or 'R'
if (curr == 'L') count++;
if (curr == 'R') count +=2 ;



Here, if the current character is the same as the previous one, I increase count by 2 to the power of loops passed, and subtract one from that if the current character is not the same as the previous one.



else if (curr == prev) count += (int) Math.pow(2, h); 
else if (curr != prev) { count += (int) Math.pow(2, h)-1;


At the end of every loop, I'll equate prev to curr and increment h.



This logic worked for the 3 given inputs, 3 LR, 3 RRL, and 2, which returns 11, 2, and 7 respectively.



However it did not pass the hidden test cases. One input I've tried is 3 LLL
which should return 8, but my algorithm returned 3. 3 LRR should return 5, but I got 3.



Is there a solution which uses a data structure to obtain the solution, since the problem quotes "perfect binary trees". Appreciate any help in sorting out the kinks in my logic, as well as other solutions. Thank you!







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 7:10







Teo Jun Xiong

















asked Mar 26 at 6:28









Teo Jun XiongTeo Jun Xiong

84 bronze badges




84 bronze badges












  • Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

    – hyde
    Mar 26 at 6:32











  • Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

    – hyde
    Mar 26 at 6:33












  • Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

    – Teo Jun Xiong
    Mar 26 at 7:11











  • Why would 2 return 7?

    – Mark
    Mar 26 at 8:32











  • When there's no path. it returns the root, 7

    – Teo Jun Xiong
    Mar 26 at 8:35

















  • Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

    – hyde
    Mar 26 at 6:32











  • Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

    – hyde
    Mar 26 at 6:33












  • Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

    – Teo Jun Xiong
    Mar 26 at 7:11











  • Why would 2 return 7?

    – Mark
    Mar 26 at 8:32











  • When there's no path. it returns the root, 7

    – Teo Jun Xiong
    Mar 26 at 8:35
















Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

– hyde
Mar 26 at 6:32





Please make the code into a MCVE (see help section), and provide example input, and expected and actual outputs.

– hyde
Mar 26 at 6:32













Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

– hyde
Mar 26 at 6:33






Also, at least to me it is unclear what you mean either by "solve it mathematically" or by "is data structure required". But generally speaking, you don't handle text (your input string) "mathematically", at least...

– hyde
Mar 26 at 6:33














Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

– Teo Jun Xiong
Mar 26 at 7:11





Thanks for your suggestion! Sorry, first post here on stackoverflow :-)

– Teo Jun Xiong
Mar 26 at 7:11













Why would 2 return 7?

– Mark
Mar 26 at 8:32





Why would 2 return 7?

– Mark
Mar 26 at 8:32













When there's no path. it returns the root, 7

– Teo Jun Xiong
Mar 26 at 8:35





When there's no path. it returns the root, 7

– Teo Jun Xiong
Mar 26 at 8:35












1 Answer
1






active

oldest

votes


















0














Being a perfect binary tree, the lowest node at a level x is greater then the sum of 2^k, where k here is the number of nodes from the level x+1 to H. A path 5 RL tells you that you have a node in level 2 on a 5-levels tree, so that node is greater than 2^3 + 2^4 + 2^5 = 56. Now that we have reduced the tree to H = h(x), we can observe that the path to the node is nothing more than a binary code where L = 0, R =1. We convert this binary code to integer, sum it to the number obtained before and we have the label of the node. A path RL is 10 = 2, and then 5 RL has label 58.



Now, converting this algorithm is very simple as you just have to split the array in two parts, convert the R-L path in binary code and do the math.






share|improve this answer






















    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',
    autoActivateHeartbeat: false,
    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%2f55351011%2fis-there-a-way-to-mathematically-locate-the-correct-node%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Being a perfect binary tree, the lowest node at a level x is greater then the sum of 2^k, where k here is the number of nodes from the level x+1 to H. A path 5 RL tells you that you have a node in level 2 on a 5-levels tree, so that node is greater than 2^3 + 2^4 + 2^5 = 56. Now that we have reduced the tree to H = h(x), we can observe that the path to the node is nothing more than a binary code where L = 0, R =1. We convert this binary code to integer, sum it to the number obtained before and we have the label of the node. A path RL is 10 = 2, and then 5 RL has label 58.



    Now, converting this algorithm is very simple as you just have to split the array in two parts, convert the R-L path in binary code and do the math.






    share|improve this answer



























      0














      Being a perfect binary tree, the lowest node at a level x is greater then the sum of 2^k, where k here is the number of nodes from the level x+1 to H. A path 5 RL tells you that you have a node in level 2 on a 5-levels tree, so that node is greater than 2^3 + 2^4 + 2^5 = 56. Now that we have reduced the tree to H = h(x), we can observe that the path to the node is nothing more than a binary code where L = 0, R =1. We convert this binary code to integer, sum it to the number obtained before and we have the label of the node. A path RL is 10 = 2, and then 5 RL has label 58.



      Now, converting this algorithm is very simple as you just have to split the array in two parts, convert the R-L path in binary code and do the math.






      share|improve this answer

























        0












        0








        0







        Being a perfect binary tree, the lowest node at a level x is greater then the sum of 2^k, where k here is the number of nodes from the level x+1 to H. A path 5 RL tells you that you have a node in level 2 on a 5-levels tree, so that node is greater than 2^3 + 2^4 + 2^5 = 56. Now that we have reduced the tree to H = h(x), we can observe that the path to the node is nothing more than a binary code where L = 0, R =1. We convert this binary code to integer, sum it to the number obtained before and we have the label of the node. A path RL is 10 = 2, and then 5 RL has label 58.



        Now, converting this algorithm is very simple as you just have to split the array in two parts, convert the R-L path in binary code and do the math.






        share|improve this answer













        Being a perfect binary tree, the lowest node at a level x is greater then the sum of 2^k, where k here is the number of nodes from the level x+1 to H. A path 5 RL tells you that you have a node in level 2 on a 5-levels tree, so that node is greater than 2^3 + 2^4 + 2^5 = 56. Now that we have reduced the tree to H = h(x), we can observe that the path to the node is nothing more than a binary code where L = 0, R =1. We convert this binary code to integer, sum it to the number obtained before and we have the label of the node. A path RL is 10 = 2, and then 5 RL has label 58.



        Now, converting this algorithm is very simple as you just have to split the array in two parts, convert the R-L path in binary code and do the math.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 6:58









        Davide VitaliDavide Vitali

        7003 silver badges16 bronze badges




        7003 silver badges16 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351011%2fis-there-a-way-to-mathematically-locate-the-correct-node%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해