1920. Build Array from Permutation | Leetcode Daily 6 May 2025 | Java | Hindi
Автор: AlgorithmHQ
Загружено: 2025-05-05
Просмотров: 676
Описание:
"1920. Build Array from Permutation" is a medium-level problem and the LeetCode daily challenge (POTD) for 5 May 2025. The solution, written in Java, is explained with a dry-run on a blackboard, making it accessible to viewers with diverse programming backgrounds by focusing on the logic rather than language-specific details.
Intuition:
The problem asks us to build a new array such that ans[i] = nums[nums[i]] using the original array nums. A naive solution would require creating a new array, thus using extra space. However, we can cleverly modify the original array in-place using a technique that temporarily encodes both the old and new values in each index. The key idea here is to store two values in one integer without losing the original — we take advantage of the fact that all values are in the range 0 to n-1, so multiplying by n lets us shift one value into the higher digits.
During the first loop, we update nums[i] to store both the original value and the desired new value: nums[i] = nums[i] + n * (nums[nums[i]] % n). The modulo ensures we only use the original value (in case nums[nums[i]] was already updated). Then, in the second loop, we divide each element by n to extract just the new value we want, discarding the original. This trick gives us an efficient in-place solution with O(n) time and O(1) extra space, which is a smart and elegant use of number encoding.
Link to the problem: https://leetcode.com/problems/build-a...
Link to the Java Code: https://github.com/AditiChourasia/Lee...
For doubts/queries, please reach out on [email protected]
Connect with me on Linkedin: / aditi-chourasia-a2a572121
Other problems for practice:
• 1128. Number of Equivalent Domino Pairs | ...
• 1007. Minimum Domino Rotations For Equal R...
• 838. Push Dominoes | Leetcode Daily 2 May ...
• 1295. Find Numbers with Even Number of Dig...
• 2302. Count Subarrays With Score Less Than...
• 3392. Count Subarrays of Length Three With...
• 2444. Count Subarrays With Fixed Bounds | ...
• 2845. Count of Interesting Subarrays | Lee...
#leetcodejava #leetcode #dailychallenge #potd #hindi
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: