├── main.js ├── index.js └── README.md /main.js: -------------------------------------------------------------------------------- 1 | // Brute Force 2 | // Not work properly 3 | let q = []; 4 | let engSum = 0; 5 | let distSum = 0; 6 | let ind = 0; 7 | function marathon(eng, dist) { 8 | engSum += eng; 9 | distSum += dist; 10 | if (engSum > distSum) { 11 | q.push(ind); 12 | } else { 13 | q.push('#'); 14 | engSum = 0; 15 | distSum = 0; 16 | } 17 | ind++; 18 | } 19 | marathon(2, 5); 20 | marathon(9, 3); 21 | marathon(1, 4); 22 | marathon(3, 5); 23 | 24 | console.log(q); 25 | // let q = ["#",1,2,"#",4,5,6,"#",8]; 26 | let c = 0; 27 | let len = q.length; 28 | let front = 0; // fornt< 29 | while (front < len) { 30 | let n = q.length; // 2 31 | let pNum = c + 1; // 1 32 | if (q[c] == '#') { 33 | // true 34 | while (q.length != n - pNum) { 35 | //2!=1 36 | q.shift(); // [#] 37 | } 38 | c = 0; // 0 39 | } else { 40 | c++; 41 | } 42 | front++; 43 | } 44 | // console.log(3%4); 45 | if (q.length == 0) { 46 | console.log('No Starting Point'); 47 | } else { 48 | console.log(q[0]); 49 | } 50 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // done 2 | function runProgram(input) { 3 | input = input.trim().split("\n"); 4 | let tc = +input[0] 5 | let line = 1; 6 | let arr1 = [] 7 | let arr2 = [] 8 | for(let t=0;t=tc){ 43 | console.log("No starting points") 44 | }else{ 45 | console.log(start) 46 | } 47 | } 48 | if (process.env.USER === "") { 49 | runProgram(``); 50 | } else { 51 | process.stdin.resume(); 52 | process.stdin.setEncoding("ascii"); 53 | let read = ""; 54 | process.stdin.on("data", function (input) { 55 | read += input; 56 | }); 57 | process.stdin.on("end", function () { 58 | read = read.replace(/\n$/, ""); 59 | read = read.replace(/\n$/, ""); 60 | runProgram(read); 61 | }); 62 | process.on("SIGINT", function () { 63 | read = read.replace(/\n$/, ""); 64 | runProgram(read); 65 | process.exit(0); 66 | }); 67 | } 68 | 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🏃‍♀️ Marathon Energy Drink Problem 2 | 3 | This repository contains a solution to the Marathon Energy Drink Problem, which is a type of DSA (Data Structure and Algorithms) question. The problem involves participating in a marathon and choosing the optimal starting point to complete the entire marathon. Each starting point has a different amount of energy drinks, and each energy drink gives a specific amount of energy. 💪💧 4 | 5 | ## 📜 Problem Description 6 | 7 | In this marathon, there are multiple starting points labeled from 0 to n-1, where n is the total number of starting points. Each starting point has an associated energy drink counter that indicates the number of energy drinks available at that point. However, not all energy drinks provide the same amount of energy. ⚡🚀 8 | 9 | Assuming that you start the marathon with no energy, your goal is to determine the starting point that allows you to complete the entire marathon. You can only run 1 km with one unit of energy. 🏁🏃‍♂️ 10 | 11 | ## 💡 Solution Approach 12 | 13 | To solve this problem efficiently, we can use the concept of prefix sums or cumulative sums. The idea is to calculate the total energy at each starting point by accumulating the energy from the energy drinks encountered so far. 🧮💡 14 | 15 | Here's the general approach to find the optimal starting point: 16 | 17 | 1. Calculate the total energy required to complete the marathon by summing up the distances between all starting points. 📏🔋 18 | 19 | 2. Initialize variables `totalEnergy` and `minStartingPoint` to track the total energy and the optimal starting point, respectively. 20 | 21 | 3. Iterate over each starting point and calculate the total energy obtained by considering the energy drinks encountered. 🏃‍♂️💧 22 | 23 | 4. If the total energy obtained at a particular starting point is greater than or equal to the total energy required to complete the marathon, update the `minStartingPoint` to the current starting point. 24 | 25 | 5. Finally, the value of `minStartingPoint` will represent the optimal starting point from which you should begin running to complete the marathon. 🏆🥇 26 | 27 | ## 🚀 Usage 28 | 29 | To use the solution provided in this repository, you can follow these steps: 30 | 31 | 1. Clone the repository to your local machine using the following command: 32 | 33 | ``` 34 | git clone https://github.com/Shubh2-0/Marathon.git 35 | ``` 36 | 37 | 2. Navigate to the cloned repository: 38 | 39 | ``` 40 | cd Marathon 41 | ``` 42 | 43 | 3. Open the repository in your preferred code editor. 44 | 45 | 4. Inside the repository, you will find the solution code in the `solution.js` file. 46 | 47 | 5. Implement the solution based on the provided approach. You can modify the existing `fun` function or create a new function to solve the problem. 48 | 49 | 6. Once you have implemented the solution, you can run the code using Node.js or your preferred JavaScript runtime environment. 50 | 51 | ``` 52 | node solution.js 53 | ``` 54 | 55 | 7. The code will execute, and the output will be displayed in the console, indicating the optimal starting point for the marathon. 56 | 57 | Feel free to modify the code, adapt it to your specific needs, and experiment with different approaches to solve the Marathon Energy Drink Problem. 58 | 59 | If you encounter any issues or have questions, please feel free to reach out. Happy coding! 60 | 61 | ## 🤝 Contributing 62 | 63 | If you have any improvements or suggestions for solving the Marathon Energy Drink Problem more efficiently, please feel free to contribute. You can submit a pull request with your changes, and they will be reviewed. 64 | 65 | Please ensure that any contributions adhere to the existing code style and follow good software development practices. 66 | 67 | ## 🙏 Acknowledgments 68 | 69 | - This problem is a common type of DSA question, and the solution provided here is a general approach. Different optimizations or variations 70 | --------------------------------------------------------------------------------