From d74a5c17fa234938bb675fa576333a3c9562571f Mon Sep 17 00:00:00 2001 From: mankalavaishnavi Date: Sun, 16 Jun 2024 11:52:07 +0530 Subject: [PATCH 1/2] added 3131 solution lc --- .../3131-Find-The-Integer-Added-To-Array-1 | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 diff --git a/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 b/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 new file mode 100644 index 000000000..7157e0113 --- /dev/null +++ b/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 @@ -0,0 +1,129 @@ +--- +id: find-integer-added-to-array +title: Find the Integer Added to Array I (LeetCode) +sidebar_label: 3131-FindIntegerAddedToArray +tags: + - Array + - Math +description: Given two arrays of equal length, nums1 and nums2, find the integer that has been added to each element of nums1 to obtain nums2. +sidebar_position: 3131 +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i/) | [Find the Integer Added to Array I Solution on LeetCode](https://leetcode.com/problems/find-the-integer-added-to-array-i/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | + +## Problem Description + +You are given two arrays of equal length, `nums1` and `nums2`. + +Each element in `nums1` has been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. + +As a result, `nums1` becomes equal to `nums2`. Two arrays are considered equal when they contain the same integers with the same frequencies. + +Return the integer `x`. + +### Example 1 + +- **Input:** `nums1 = [2, 6, 4], nums2 = [9, 7, 5]` +- **Output:** `3` +- **Explanation:** The integer added to each element of `nums1` is `3`. + +### Example 2 + +- **Input:** `nums1 = [10], nums2 = [5]` +- **Output:** `-5` +- **Explanation:** The integer added to each element of `nums1` is `-5`. + +### Example 3 + +- **Input:** `nums1 = [1, 1, 1, 1], nums2 = [1, 1, 1, 1]` +- **Output:** `0` +- **Explanation:** The integer added to each element of `nums1` is `0`. + +### Constraints + +- `1 <= nums1.length == nums2.length <= 100` +- `0 <= nums1[i], nums2[i] <= 1000` +- The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by adding `x` to each element of `nums1`. + +## Approach + +To find the integer `x` that was added to each element of `nums1` to obtain `nums2`, we can use the following approach: + +1. Iterate through the arrays `nums1` and `nums2`. +2. Calculate the difference `x` for the first pair of elements from `nums1` and `nums2`. +3. Ensure that the calculated `x` is consistent for all elements. + +### Solution Code + +#### Python + +```python +def find_integer_added_to_array(nums1, nums2): + if len(nums1) != len(nums2): + return None # Inconsistent input lengths + + x = nums2[0] - nums1[0] + + for i in range(1, len(nums1)): + if nums2[i] - nums1[i] != x: + return None # Inconsistent difference found + + return x +``` + +#### C++ + +```c++ +#include +#include +using namespace std; + +class Solution { +public: + int findIntegerAddedToArray(vector& nums1, vector& nums2) { + if (nums1.size() != nums2.size()) { + return INT_MIN; // Inconsistent input lengths + } + + int x = nums2[0] - nums1[0]; + + for (size_t i = 1; i < nums1.size(); ++i) { + if (nums2[i] - nums1[i] != x) { + return INT_MIN; // Inconsistent difference found + } + } + + return x; + } +}; + +``` + +#### Java + +```java +class Solution { + public Integer findIntegerAddedToArray(int[] nums1, int[] nums2) { + if (nums1.length != nums2.length) { + return null; // Inconsistent input lengths + } + + int x = nums2[0] - nums1[0]; + + for (int i = 1; i < nums1.length; i++) { + if (nums2[i] - nums1[i] != x) { + return null; // Inconsistent difference found + } + } + + return x; + } +``` +#### Conclusion +The above solutions determine the integer x that was added to each element of nums1 to obtain nums2. +By ensuring consistency across the entire array, we validate the difference and return the result. +Adjustments for different languages and edge cases ensure robustness across various inputs. \ No newline at end of file From 37efa34f28da1b70c67783e6bf3b3c0126c84102 Mon Sep 17 00:00:00 2001 From: Mankala Vaishnavi <131646799+VaishnaviMankala19@users.noreply.github.com> Date: Sun, 16 Jun 2024 12:20:27 +0530 Subject: [PATCH 2/2] changed file name --- ...ded-To-Array-1 => 3131-Find-The-Integer-Added-To-Array-1.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename dsa-solutions/lc-solutions/3100-3199/{3131-Find-The-Integer-Added-To-Array-1 => 3131-Find-The-Integer-Added-To-Array-1.md} (99%) diff --git a/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 b/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1.md similarity index 99% rename from dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 rename to dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1.md index 7157e0113..84d740455 100644 --- a/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1 +++ b/dsa-solutions/lc-solutions/3100-3199/3131-Find-The-Integer-Added-To-Array-1.md @@ -126,4 +126,4 @@ class Solution { #### Conclusion The above solutions determine the integer x that was added to each element of nums1 to obtain nums2. By ensuring consistency across the entire array, we validate the difference and return the result. -Adjustments for different languages and edge cases ensure robustness across various inputs. \ No newline at end of file +Adjustments for different languages and edge cases ensure robustness across various inputs.