From c0079c0c080abf9aa3b74bd63aea7fb59571ada7 Mon Sep 17 00:00:00 2001 From: Kajal Ahirwar Date: Sat, 9 Nov 2024 18:27:00 +0530 Subject: [PATCH 1/2] Create Plus-one.md --- docs/DSA-Problem-Solution/Plus-one.md | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/DSA-Problem-Solution/Plus-one.md diff --git a/docs/DSA-Problem-Solution/Plus-one.md b/docs/DSA-Problem-Solution/Plus-one.md new file mode 100644 index 000000000..ebdb61833 --- /dev/null +++ b/docs/DSA-Problem-Solution/Plus-one.md @@ -0,0 +1,72 @@ +--- +id: plus-one +sidebar_position: 2 +title: Plus One +sidebar_label: Plus One +description: The "Plus One" dsa problem is a classic algorithm challenge that involves manipulating an array of digits. The goal is to add one to a number represented by an array of its digits. +tags: [dsa, algorithms, problem-solving] +--- + +# Plus One + +## Description +You are given a large integer represented as an integer array ``digits``, where each ``digits[i]`` is the ``ith`` digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading ``0``'s. +Increment the large integer by one and return the resulting array of digits. + +### Example 1: + +**Input:** digits = [1,2,3] + +**Output:** [1,2,4] + +**Explanation:** The array represents the integer 123. + +Incrementing by one gives 123 + 1 = 124. + +Thus, the result should be [1,2,4]. + +### Example 2: + +**Input:** digits = [4,3,2,1] + +**Output:** [4,3,2,2] + +**Explanation:** The array represents the integer 4321. + +Incrementing by one gives 4321 + 1 = 4322. + +Thus, the result should be [4,3,2,2]. + +### Example 3: + +**Input:** digits = [9] + +**Output:** [1,0] + +**Explanation:** The array represents the integer 9. + +Incrementing by one gives 9 + 1 = 10. + +Thus, the result should be [1,0]. + +# Code in Java + +```java +class Solution { + public int[] plusOne(int[] digits) { + int n = digits.length; + + for(int i=n-1; i>=0; --i) { + ++digits[i]; + digits[i] = digits[i] % 10; + if(digits[i] != 0) { + return digits; + } + } + digits = new int[n+1]; + digits[0] = 1; + return digits; + } +} +``` + From e1343a0ae9bc0a8140134c75fcce52368dc22f1a Mon Sep 17 00:00:00 2001 From: Kajal Ahirwar Date: Sat, 9 Nov 2024 18:56:28 +0530 Subject: [PATCH 2/2] Create Find Indices of stable mountains.md --- .../Find Indices of stable mountains.md | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 docs/DSA-Problem-Solution/Find Indices of stable mountains.md diff --git a/docs/DSA-Problem-Solution/Find Indices of stable mountains.md b/docs/DSA-Problem-Solution/Find Indices of stable mountains.md new file mode 100644 index 000000000..40f1e7ff5 --- /dev/null +++ b/docs/DSA-Problem-Solution/Find Indices of stable mountains.md @@ -0,0 +1,66 @@ +--- +id: find-indices-of-stable-mountains +sidebar_position: 2 +title: Find Indices of stable mountains +sidebar_label: Find Indices of stable mountains +description: The "Find Indices of Stable Mountains" problem on involves identifying mountains that meet a specific stability criterion 1. +tags: [dsa, algorithms, problem-solving] +--- + +# Find Indices of Stable Mountains + +## Description +There are ``n`` mountains in a row, and each mountain has a height. You are given an integer array ``height`` where ``height[i]`` represents the height of mountain ``i``, and an integer ``threshold``. + +A mountain is called stable if the mountain just before it (if it exists) has a height strictly greater than ``threshold``. Note that mountain 0 is not stable. + +Return an array containing the indices of all stable mountains in any order. + +### Example 1: + +**Input:** height = [1,2,3,4,5], threshold = 2 + +**Output:** [3,4] + +**Explanation:** + +Mountain 3 is stable because height[2] == 3 is greater than threshold == 2. +Mountain 4 is stable because height[3] == 4 is greater than threshold == 2. + +### Example 2: + +**Input:** height = [10,1,10,1,10], threshold = 3 + +**Output:** [1,3] + +### Example 3: + +**Input:** height = [10,1,10,1,10], threshold = 10 + +**Output:** [ ] + +## Approach + +**1. Iterate Through Mountains:** Loop through the array starting from the second mountain. + +**2. Check Stability:** Compare the height of the current mountain with the height of the previous mountain. + +**3. Collect Stable Indices:** If the previous mountain's height is greater than the threshold, add the current mountain's index to the result list. + +# Code in Java + +```java +class Solution { + public List stableMountains(int[] height, int threshold) { + List stableMountains = new ArrayList<>(); + for(int i=1; ithreshold) { + stableMountains.add(i); + } + } + return stableMountains; + } +} +``` + +