From 42bfaed554a74f74ba82fe5ac3073fc689aac79a Mon Sep 17 00:00:00 2001 From: Madhurima Rawat <105432776+madhurimarawat@users.noreply.github.com> Date: Mon, 11 Nov 2024 19:42:58 +0530 Subject: [PATCH] Added 10-11-2024 Solution and 11-11-2024 Question Added 10-11-2024 Solution and 11-11-2024 Question --- 10-11-2024/Explanation.md | 42 +++++++++++++++++++++++++++++++++++++++ 10-11-2024/Solution.py | 31 +++++++++++++++++++++++++++++ 11-11-2024/Question.md | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 10-11-2024/Explanation.md create mode 100644 10-11-2024/Solution.py create mode 100644 11-11-2024/Question.md diff --git a/10-11-2024/Explanation.md b/10-11-2024/Explanation.md new file mode 100644 index 0000000..8c79e15 --- /dev/null +++ b/10-11-2024/Explanation.md @@ -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] +``` diff --git a/10-11-2024/Solution.py b/10-11-2024/Solution.py new file mode 100644 index 0000000..9f61333 --- /dev/null +++ b/10-11-2024/Solution.py @@ -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) diff --git a/11-11-2024/Question.md b/11-11-2024/Question.md new file mode 100644 index 0000000..bf5dcf4 --- /dev/null +++ b/11-11-2024/Question.md @@ -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`.