-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_15.cpp
46 lines (34 loc) · 1.03 KB
/
question_15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
==> Perfect Arrays <==
Question Link:- https://practice.geeksforgeeks.org/problems/perfect-arrays4645/1?page=1&difficulty[]=-2&category[]=Arrays&sortBy=submissions
Given an array of size N and you have to tell whether the array is perfect or not.
An array is said to be perfect if it's reverse array matches the original array.
If the array is perfect then print "PERFECT" else print "NOT PERFECT".
Example 1:
Input : Arr[] = {1, 2, 3, 2, 1}
Output : PERFECT
Explanation:
Here we can see we have [1, 2, 3, 2, 1]
if we reverse it we can find [1, 2, 3, 2, 1]
which is the same as before.
So, the answer is PERFECT.
Example 2:
Input : Arr[] = {1, 2, 3, 4, 5}
Output : NOT PERFECT
*/
#include <iostream>
using namespace std;
bool isPerfect( int arr [], int n ) {
int s = 0, e = n-1;
while ( s < e ) {
if ( arr[s] != arr[e] ) return false;
s++;
e--;
}
return true;
}
int main () {
int arr [] = { 1, 2, 3, 2, 1 };
cout << isPerfect(arr, 5);
return 0;
}