-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 10-11-2024 Solution and 11-11-2024 Question
Added 10-11-2024 Solution and 11-11-2024 Question
- Loading branch information
1 parent
1db631c
commit 42bfaed
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
#### Program Explanation: | ||
|
||
**Why do arrays never get lost?** | ||
👉 **Answer**: Because they always have an _index_ to find their way! 😆🔢 | ||
|
||
This program finds the _next greater element_ for each element in `nums1` from `nums2`. Here’s a detailed explanation: | ||
|
||
1. **Input Handling**: | ||
|
||
- The program takes input for `nums1` and `nums2` as space-separated integers. | ||
- These inputs are converted into lists of integers for processing. | ||
|
||
2. **Logic to Find Next Greater Element**: | ||
|
||
- For each element in `nums1`, the program looks for the next greater element in `nums2` starting from the position right after the current element's index. | ||
- If a greater element is found, it is added to the result list (`res`). | ||
- If no greater element is found, `-1` is added to `res`. | ||
|
||
3. **Nested Loop Explanation**: | ||
|
||
- The outer loop iterates over each element in `nums1`. | ||
- The inner loop starts from the next index of the current element in `nums2` and searches for the next greater element. | ||
|
||
4. **Output**: | ||
- The result list is printed, showing the next greater element for each item in `nums1` or `-1` if none exists. | ||
|
||
--- | ||
|
||
### Example Execution: | ||
|
||
**Input:** | ||
|
||
```plaintext | ||
Enter elements for nums1 separated by space: 4 1 2 | ||
Enter elements for nums2 separated by space: 1 3 4 2 | ||
``` | ||
|
||
**Output:** | ||
|
||
```plaintext | ||
Next greater elements for nums1 in nums2: [-1, 3, -1] | ||
``` |
This file contains 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,31 @@ | ||
# Declaring Two Arrays | ||
nums1, nums2 = [], [] # Initialize empty lists for nums1 and nums2 | ||
|
||
# Input for nums1 | ||
nums1_input = input("Enter elements for nums1 separated by space: ") | ||
# Convert the input string into a list of integers | ||
nums1 = [int(x) for x in nums1_input.split()] | ||
|
||
# Input for nums2 | ||
nums2_input = input("Enter elements for nums2 separated by space: ") | ||
# Convert the input string into a list of integers | ||
nums2 = [int(x) for x in nums2_input.split()] | ||
|
||
# Result list to store the next greater elements | ||
res = [] | ||
|
||
# Loop through each element in nums1 | ||
for i in range(len(nums1)): | ||
found = False # Flag to track if a greater element is found | ||
for j in range( | ||
nums2.index(nums1[i]) + 1, len(nums2) | ||
): # Start searching in nums2 after the current element's position | ||
if nums2[j] > nums1[i]: # Check if a greater element is found | ||
res.append(nums2[j]) # Append the next greater element | ||
found = True # Set the flag to true | ||
break # Break out of the loop once found | ||
if not found: # If no greater element is found | ||
res.append(-1) # Append -1 to the result | ||
|
||
# Print the result list | ||
print("Next greater elements for nums1 in nums2:", res) |
This file contains 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,38 @@ | ||
## Question: **Equal Contiguous Array** | ||
|
||
**Difficulty Level:** 🟢 Beginner | ||
**Domain:** Arrays and Searching | ||
|
||
### **Objective:** | ||
|
||
Create a program to find the maximum length of a contiguous subarray with an equal number of `0`s and `1`s in a given binary array. | ||
|
||
### **Problem Description:** | ||
|
||
Given a binary array consisting of only `0`s and `1`s, your task is to identify the longest contiguous subarray that contains an equal number of `0`s and `1`s and return its length. | ||
|
||
### **Input:** | ||
|
||
- A binary array `arr` where (1 ≤ length ≤ 10,000). | ||
|
||
### **Output:** | ||
|
||
- An integer representing the length of the longest contiguous subarray with an equal number of `0`s and `1`s. | ||
|
||
### **Example:** | ||
|
||
#### Input: | ||
|
||
```plaintext | ||
arr = [0, 1, 0] | ||
``` | ||
|
||
#### Output: | ||
|
||
```plaintext | ||
2 | ||
``` | ||
|
||
#### Explanation: | ||
|
||
- The subarray `[0, 1]` has an equal number of `0`s and `1`s, and its length is `2`. |