Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Plus-one.md #1984

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/DSA-Problem-Solution/Find Indices of stable mountains.md
Original file line number Diff line number Diff line change
@@ -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<Integer> stableMountains(int[] height, int threshold) {
List<Integer> stableMountains = new ArrayList<>();
for(int i=1; i<height.length; i++) {
if(height[i-1]>threshold) {
stableMountains.add(i);
}
}
return stableMountains;
}
}
```


72 changes: 72 additions & 0 deletions docs/DSA-Problem-Solution/Plus-one.md
Original file line number Diff line number Diff line change
@@ -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;
}
}
```

Loading