From 83a6c5074dc608c9f9f4c211f561af4d5f720b2d Mon Sep 17 00:00:00 2001 From: Harsh Tenguriya Date: Mon, 18 Nov 2024 13:32:16 +0000 Subject: [PATCH] Pending changes exported from your codespace --- src/class_practice/carry_forward.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/class_practice/carry_forward.js diff --git a/src/class_practice/carry_forward.js b/src/class_practice/carry_forward.js new file mode 100644 index 0000000..504a64a --- /dev/null +++ b/src/class_practice/carry_forward.js @@ -0,0 +1,27 @@ +// Carry_forward +function min_max() { + const A = [3, 6, 4, 8, 3, 7, 2, 1, 6, 8, 5, 3, 4, 1]; + const N = A.length; + const max = Math.max(...A); + const min = Math.min(...A); + + let iMax = -1; + let iMin = -1; + + let ans = N; + + for (let i = 0; i < N; i++) { + if (A[i] == min) { + iMin = i; + } + else if (A[i] == max) { + iMax = i; + } + + if (iMax >= 0 && iMin >= 0) { + const length = Math.abs(iMax - iMin); + ans = Math.min(ans, length); + } + } + return ans; +} \ No newline at end of file