screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; function App() { const [numbers, setNumbers] = useState([4, 2, 4, 7, 2, 1, 10, 8, 6]); // Replace with your numbers const [minPair, setMinPair] = useState([]); const [minSum, setMinSum] = useState(null); const findMinimumPairSum = () => { if (numbers.length < 2) { alert("There should be at least two numbers to find a pair sum."); return; } // Sort the array in ascending order const sortedNumbers = [...numbers].sort((a, b) => a - b); // Initialize the minimum sum and pair let minSum = Number.MAX_VALUE; let minPair = []; // Iterate through the sorted array and find the minimum sum for (let i = 0; i < sortedNumbers.length - 1; i++) { let sum = sortedNumbers[i] + sortedNumbers[i + 1]; if (sum < minSum) { minSum = sum; minPair = [sortedNumbers[i], sortedNumbers[i + 1]]; } } setMinSum(minSum); setMinPair(minPair); }; return ( <div className="container"> <h2>React Js Find Minimum Pair Sum in list</h2> <p>Numbers: {numbers.join(', ')}</p> <button onClick={findMinimumPairSum}>Find Minimum Pair Sum</button> {minSum !== null && ( <div> <p>Minimum Pair Sum: {minSum}</p> <p>Pair: {minPair.join(', ')}</p> </div> )} </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { width: 600px; margin: 0 auto; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px; padding: 20px; } /* Style for the heading */ h1 { font-size: 24px; color: #333; } /* Style for the paragraph */ p { font-size: 16px; color: #555; margin: 10px 0; } /* Style for the button */ button { padding: 10px 20px; font-size: 18px; background-color: #007BFF; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } </style> </body> </html>