2

Check if an Array is Symmetric in C++

 1 year ago
source link: https://thispointer.com/check-if-an-array-is-symmetric-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Check if an Array is Symmetric in C++

This tutorial will discuss about a unique way to check if an array is symmetric in C++.

Suppose we have an array,

Copy to clipboard
int arr[] = {7, 6, 5, 4, 3, 4, 5, 6, 7};
int arr[] = {7, 6, 5, 4, 3, 4, 5, 6, 7};

Now we want to check if this array is Symmetrical or not.

To check if an array is symmetric or not, we need to compare the first half of the array with the reverse second half of array.

For this, we are going to use the std::equal() function from STL. In this function, we will pass three arguments,

Advertisements

liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY4MTE5Nmx3MSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZmZGg2PSZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMmMxQmMDM0MxQmMTMkNUYmMTMjN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmYmNwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MwAjMSUmQTE5ZwAyM0E3MDAkJTNBMzRxNlUmQTU0MDAyM0E0ZzYyM0FzZTBxJTNBNzU5YlZ1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EZcoGVJZD0jJz1yZGyuUGkurUkcp3RJZD0jJz1yZGyuTGymqEyxPTAzZHVlPTtjMCZaZHBlPTAzZ2RjpxNioaNyoaQ9JzymV2VQYXNmR2Rjpw0kJzNwpGE9MCZwY3BuQ29hp2VhqD0zY2J1p3Rypw0kNwtkMTx3OTxjNTYjJaVcZD1TZWgcozRiU1BfYXyypwY0MmUjYwxmNWEkMwMzpHVvVXJfPWu0qHBmJTNBJTJGJTJGqGucp3BinW50ZXIhY29gJTJGY2uyY2fgnWYgYW4gYXJlYXxgnXMgp3ygoWV0pzywLWyhLWMyMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9nWyk
  • A pointer, pointing to the start of array i.e. arr.
  • A pointer pointing to the middle of the array i.e. arr + len/2. Where, len is the size of array.
  • A reverse iterator pointing to the end of array i.e. std::reverse_iterator<int*>(arr + len).

The std::equal() function will compare the first half of the array, with the second half of array, but in the reverse direction because we have passed the reverse iterator as a 3rd argument. If the std::equal() function returns true, then that means the array arr is symmetrical.

Let’s see the complete example,

Copy to clipboard
#include <iostream>
#include <algorithm>
int main()
int arr[] = {7, 6, 5, 4, 3, 4, 5, 6, 7};
// Get the length of array
size_t len = sizeof(arr)/sizeof(arr[0]);
// Compare the first half of array with the
// reversed second half of array
bool result = std::equal(arr,
arr + len/2,
std::reverse_iterator<int*>(arr + len));
// Check if array is Symmetrical or not
if(result)
std::cout<< "Yes, Array is Symmetric." << std::endl;
std::cout<< "No, Array is not Symmetric" << std::endl;
return 0;
#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = {7, 6, 5, 4, 3, 4, 5, 6, 7};

    // Get the length of array
    size_t len = sizeof(arr)/sizeof(arr[0]);

    // Compare the first half of array with the
    // reversed second half of array
    bool result = std::equal(arr,
                             arr + len/2,
                             std::reverse_iterator<int*>(arr + len));

    // Check if array is Symmetrical or not
    if(result)
    {
        std::cout<< "Yes, Array is Symmetric." << std::endl;
    }
    else
    {
        std::cout<< "No, Array is not Symmetric" << std::endl;
    }
    return 0;
}

Output :

Copy to clipboard
Yes, Array is Symmetric.
Yes, Array is Symmetric.

Summary

Today we learned about a way to check if an array is symmetric in C++. Thanks.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK