Skip to content

Check Equal Arrays problem from gfg is added #4247

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

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions dsa-solutions/gfg-solutions/Easy problems/Check-Equal-Arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
id: check-equal-arrays
title: Check Equal Arrays
sidebar_label: Check-Equal-Arrays
tags:
- Modular Arithmetic
- Algorithms
description: "This tutorial covers the solution to the Check Equal Arrays problem from the GeeksforGeeks."
---
## Problem Description
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.
Note: If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.

## Examples

**Example 1:**

```
Input: arr1[] = [1, 2, 5, 4, 0], arr2[] = [2, 4, 5, 0, 1]
Output: true
Explanation: Both the array can be rearranged to [0,1,2,4,5]
```

**Example 2:**

```
Input: arr1[] = [1, 2, 5], arr2[] = [2, 4, 15]
Output: false
Explanation: arr1[] and arr2[] have only one common value.
```


Expected Time Complexity: O(n)

Expected Auxiliary Space: O(n)

## Constraints

* `1<= arr1.size, arr2.size<=10^7`

## Problem Explanation

The task is to traverse the array and search the number.

## Code Implementation

### C++ Solution

```cpp

#include <algorithm>
#include <vector>

bool areEqual(std::vector<int> arr1, std::vector<int> arr2) {
std::sort(arr1.begin(), arr1.end());
std::sort(arr2.begin(), arr2.end());
return arr1 == arr2;
}


```

```java
import java.util.Arrays;

public class Main {
public static boolean areEqual(int[] arr1, int[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
}



```

```python

def are_equal(arr1, arr2):
return sorted(arr1) == sorted(arr2)
```

```javascript

function areEqual(arr1, arr2) {
return arr1.sort((a, b) => a - b).every((value, index) => value === arr2.sort((a, b) => a - b)[index]);
}


```

## Solution Logic:
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.




## Time Complexity

* The time complexity is $O(log(n))$ where n is the input number.


## Space Complexity

* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array.
Loading