[Solved] Compare 2 arrays with C++

Compare 2 arrays: Write a program to find whether 2 arrays are contains the same element in same index position.
 

Input Format:

Input consists of 2n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. The next ‘n’ integers correspond to the elements in the second array.Assume that the maximum value of n is 15.
 

Output Format:

Print yes if the 2 arrays are the same. Print no if the 2 arrays are different.
 

Sample Input 1:

5
2
3
6
8
-1
2
3
6
8
-1

Sample Output 1:

yes

Sample Input 2:

5
2
3
6
8
-1
2
3
6
8
10

Sample Output 2:

no

Solution

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int *a = new int[n];
    int *b = new int[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    int flag = 0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] != b[i])
        {
            flag = 1;
        }
    }
    if (flag == 0)
    {
        cout << "yes";
    }
    else
    {
        cout << "no";
    }
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *