()
| 43 | var sideL, sideR; |
| 44 | |
| 45 | function cross() { |
| 46 | |
| 47 | num = document.getElementById("input").value; |
| 48 | output = ""; |
| 49 | showOutput = document.getElementById("output"); |
| 50 | err = document.getElementById("errorMsg"); |
| 51 | |
| 52 | err.innerText = ""; |
| 53 | if (num.length == 0 || num.match(/[^0-9]/i)) { |
| 54 | err.innerText = "Enter a number !" |
| 55 | showOutput.innerHTML = `<p><br><b>PROBLEM : <br> Crossing the bridge</b><br><br> |
| 56 | Write a program to print the minimum time taken to get N people from one side of a bridge to the other if |
| 57 | <br><br> |
| 58 | <b>a)</b><i>The bridge can only hold two people at a time.</i><br><br> |
| 59 | <b>b)</b><i>It's dark and the only flashlight must be carried while crossing.</i><br><br> |
| 60 | <b>c)</b><i>People are numbered from 1 to N. The k-th person takes k seconds to cross the bridge.</i><br><br> |
| 61 | <b>d)</b><i> A pair crosses at the speed of the slowest member.</i><br><br> |
| 62 | <b>For example:</b><br> |
| 63 | Input: 4 <br> |
| 64 | Output: 11<br><br>i.e<br> |
| 65 | <i>1-3 cross(3), 1 returns(1)<i/><br> |
| 66 | <i>1-4 cross(4), 1 returns(1)</i><br> |
| 67 | <i>1-2 cross(2)</i><br> |
| 68 | <i>SUM : 3 + 1 + 4 + 1 + 2 = 11</i><br></p>`; |
| 69 | } else { |
| 70 | //initializing both side array |
| 71 | sideL = []; sideR = []; |
| 72 | |
| 73 | //filling left side of river array with people |
| 74 | for (let i = 0; i < num; i++) { |
| 75 | sideL[i] = i + 1; |
| 76 | } |
| 77 | output = "<br><b>Solution :</b> <br><div id = 'sec'>"; |
| 78 | |
| 79 | //executing alogrithm |
| 80 | make_pair(sideL, sideR); |
| 81 | |
| 82 | showOutput.innerHTML = output; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | //function to make pairs |
| 88 | const make_pair = (l, r) => { |
no test coverage detected