Skip to content

Commit 69d0fbb

Browse files
authored
Merge pull request #4247 from Ishitamukherjee2004/new-branch
Check Equal Arrays problem from gfg is added
2 parents c809e17 + c197003 commit 69d0fbb

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
id: check-equal-arrays
3+
title: Check Equal Arrays
4+
sidebar_label: Check-Equal-Arrays
5+
tags:
6+
- Modular Arithmetic
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Check Equal Arrays problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given two arrays `arr1` and `arr2` of equal size, the task is to find whether the given arrays are equal. Two arrays are said to be equal if both contain the same set of elements, arrangements (or permutations) of elements may be different though.
12+
Note: If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input: arr1[] = [1, 2, 5, 4, 0], arr2[] = [2, 4, 5, 0, 1]
20+
Output: true
21+
Explanation: Both the array can be rearranged to [0,1,2,4,5]
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: arr1[] = [1, 2, 5], arr2[] = [2, 4, 15]
28+
Output: false
29+
Explanation: arr1[] and arr2[] have only one common value.
30+
```
31+
32+
33+
Expected Time Complexity: O(n)
34+
35+
Expected Auxiliary Space: O(n)
36+
37+
## Constraints
38+
39+
* `1<= arr1.size, arr2.size<=10^7`
40+
41+
## Problem Explanation
42+
43+
The task is to traverse the array and search the number.
44+
45+
## Code Implementation
46+
47+
### C++ Solution
48+
49+
```cpp
50+
51+
#include <algorithm>
52+
#include <vector>
53+
54+
bool areEqual(std::vector<int> arr1, std::vector<int> arr2) {
55+
std::sort(arr1.begin(), arr1.end());
56+
std::sort(arr2.begin(), arr2.end());
57+
return arr1 == arr2;
58+
}
59+
60+
61+
```
62+
63+
```java
64+
import java.util.Arrays;
65+
66+
public class Main {
67+
public static boolean areEqual(int[] arr1, int[] arr2) {
68+
Arrays.sort(arr1);
69+
Arrays.sort(arr2);
70+
return Arrays.equals(arr1, arr2);
71+
}
72+
}
73+
74+
75+
76+
```
77+
78+
```python
79+
80+
def are_equal(arr1, arr2):
81+
return sorted(arr1) == sorted(arr2)
82+
```
83+
84+
```javascript
85+
86+
function areEqual(arr1, arr2) {
87+
return arr1.sort((a, b) => a - b).every((value, index) => value === arr2.sort((a, b) => a - b)[index]);
88+
}
89+
90+
91+
```
92+
93+
## Solution Logic:
94+
In the solution, the arrays are sorted and then compared. If the sorted arrays are equal, then the original arrays contain the same set of elements, regardless of order. Note that this approach assumes that the arrays contain only elements that can be compared using the default comparison operators (e.g., numbers, strings). If the arrays contain custom objects, a custom comparison function may be needed.
95+
96+
97+
98+
99+
## Time Complexity
100+
101+
* The time complexity is $O(log(n))$ where n is the input number.
102+
103+
104+
## Space Complexity
105+
106+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array.

0 commit comments

Comments
 (0)