-
Notifications
You must be signed in to change notification settings - Fork 930
Pyramid pattern #495
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
Closed
lieutenant-Rohit
wants to merge
3
commits into
kishanrajput23:main
from
lieutenant-Rohit:pyramidPattern
Closed
Pyramid pattern #495
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import java.util.*; | ||
|
|
||
| public class NextGreaterElement { | ||
|
|
||
| /** | ||
| * Function to find Next Greater Element for each element in the array. | ||
| * | ||
| * @param arr Input array of integers | ||
| * @return Array containing next greater elements | ||
| */ | ||
| public static int[] findNextGreater(int[] arr) { | ||
| int n = arr.length; | ||
| int[] result = new int[n]; | ||
|
|
||
| // Stack to store elements | ||
| Stack<Integer> stack = new Stack<>(); | ||
|
|
||
| // Traverse from right to left | ||
| for (int i = n - 1; i >= 0; i--) { | ||
|
|
||
| // Remove elements from stack that are <= current element | ||
| while (!stack.isEmpty() && stack.peek() <= arr[i]) { | ||
| stack.pop(); | ||
| } | ||
|
|
||
| // If stack is empty, no greater element exists | ||
| if (stack.isEmpty()) { | ||
| result[i] = -1; | ||
| } else { | ||
| // Top of stack is the next greater element | ||
| result[i] = stack.peek(); | ||
| } | ||
|
|
||
| // Push current element onto stack | ||
| stack.push(arr[i]); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Driver method to test the function | ||
| */ | ||
| public static void main(String[] args) { | ||
| int[] arr = {4, 5, 2, 10, 8}; | ||
|
|
||
| int[] result = findNextGreater(arr); | ||
|
|
||
| System.out.println("Next Greater Elements:"); | ||
| for (int num : result) { | ||
| System.out.print(num + " "); | ||
| } | ||
| } | ||
| } |
Submodule JournalApp
added at
113662
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class PyramidPattern { | ||
| public static void main(String[] args) { | ||
| int n = 5; // number of rows | ||
|
|
||
| for (int i = 1; i <= n; i++) { | ||
|
|
||
| // print spaces | ||
| for (int j = 1; j <= n - i; j++) { | ||
| System.out.print(" "); | ||
| } | ||
|
|
||
| // print stars | ||
| for (int j = 1; j <= i; j++) { | ||
| System.out.print("* "); | ||
| } | ||
|
|
||
| // move to next line | ||
| System.out.println(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this file used for?
Could you please provide clarification on this?
Thanks