Check whether the given array is perfect or not
Last Updated :
30 Sep, 2022
There is an array containing some non-negative integers. Check whether the array is perfect or not. An array is called perfect if it is first strictly increasing, then constant and finally strictly decreasing. Any of the three parts can be empty.
Examples:
Input : arr[] = {1, 8, 8, 8, 3, 2}
Output : Yes
The array is perfect as it is first increasing, then constant and finally decreasing.
Input : arr[] = {1, 1, 2, 2, 1}
Output : No
The first part is not strictly increasing
Input : arr[] = {4, 4, 4, 4}
Output : Yes
The approach is to iterate the array from the left to the right and increase i (that is 1 initially) while the next element is strictly more than previous. then, iterate the array from the left to the right and increase i while the two adjacent elements are equal. Finally, iterate array from the left to the right and increase i while the next element is strictly less than previous. If we reach end with this process, we say that the array is perfect.
Implementation:
C++
// CPP program to check whether the given array
// is perfect or not.
#include <iostream>
using namespace std;
int checkUnimodal(int arr[], int n)
{
// Cover first strictly increasing part
int i = 1;
while (arr[i] > arr[i - 1] && i < n)
++i;
// Cover middle equal part
while (arr[i] == arr[i - 1] && i < n)
++i;
// Cover last decreasing part
while (arr[i] < arr[i - 1] && i < n)
++i;
// Return true if we reached end.
return (i == n);
}
int main()
{
int arr[] = { 1, 5, 5, 5, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
if (checkUnimodal(arr, n))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
Java
// Java program to check whether the given array
// is perfect or not.
import java.util.*;
class Node
{
static boolean checkUnimodal(int arr[], int n)
{
// Cover first strictly increasing part
int i = 1;
while (i < n && arr[i] > arr[i - 1])
++i;
// Cover middle equal part
while (i < n && arr[i] == arr[i - 1])
++i;
// Cover last decreasing part
while (i < n &&arr[i] < arr[i - 1])
++i;
// Return true if we reached end.
return (i == n);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 5, 5, 5, 4, 2 };
int n = arr.length;
if (checkUnimodal(arr, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program to check whether the
# given array is perfect or not.
def checkUnimodal(arr, n):
# Cover first strictly increasing part
i = 1
while (i < n and arr[i] > arr[i - 1]):
i += 1
# Cover middle equal part
while (i < n and arr[i] == arr[i - 1]):
i += 1
# Cover last decreasing part
while (i < n and arr[i] < arr[i - 1]):
i += 1
# Return true if we reached end.
return (i == n)
# Driver Code
if __name__ == '__main__':
arr = [1, 5, 5, 5, 4, 2 ]
n = len(arr)
if (checkUnimodal(arr, n)):
print("Yes")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to check whether the given array
// is perfect or not.
using System;
class GFG
{
static bool checkUnimodal(int []arr, int n)
{
// Cover first strictly increasing part
int i = 1;
while (i < n && arr[i] > arr[i - 1])
++i;
// Cover middle equal part
while (i < n && arr[i] == arr[i - 1])
++i;
// Cover last decreasing part
while (i < n &&arr[i] < arr[i - 1])
++i;
// Return true if we reached end.
return (i == n);
}
// Driver code
static public void Main ()
{
int []arr = { 1, 5, 5, 5, 4, 2 };
int n = arr.Length;
if (checkUnimodal(arr, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by ajit..
PHP
<?php
// PHP program to check whether the
// given array is perfect or not.
function check($arr, $n)
{
// Cover first strictly increasing part
$i = 1;
while ($arr[$i] > $arr[$i - 1] && $i < $n)
++$i;
// Cover middle equal part
while ($arr[$i] == $arr[$i - 1] && $i < $n)
++$i;
// Cover last decreasing part
while ($arr[$i] < $arr[$i - 1] && $i < $n)
++$i;
// Return true if we reached end
return ($i == $n);
}
// Driver Code
$arr = array(1, 5, 5, 5, 4, 2);
$n = sizeof($arr);
if (check($arr, $n))
echo "Yes";
else
echo "No";
// This code is contributed by Akanksha Rai
?>
JavaScript
<script>
// Javascript program to check whether the given array
// is perfect or not.
function checkUnimodal(arr, n)
{
// Cover first strictly increasing part
let i = 1;
while (arr[i] > arr[i - 1] && i < n)
++i;
// Cover middle equal part
while (arr[i] == arr[i - 1] && i < n)
++i;
// Cover last decreasing part
while (arr[i] < arr[i - 1] && i < n)
++i;
// Return true if we reached end.
return (i == n);
}
let arr = [ 1, 5, 5, 5, 4, 2 ];
let n = arr.length;
if (checkUnimodal(arr, n))
document.write("YES");
else
document.write("NO");
</script>
Time Complexity: O(n), where n is the size of the array.
Auxiliary Space: O(1)
Similar Reads
Check whether Array represents a Fibonacci Series or not Given an array arr[] consisting of N integers, the task is to check whether a Fibonacci series can be formed using all the array elements or not. If possible, print "Yes". Otherwise, print "No". Examples: Input: arr[] = { 8, 3, 5, 13 } Output: Yes Explanation: Rearrange given array as {3, 5, 8, 13}
9 min read
Check whether the given String is good for any ascending Array or not Given a string S of length N. Consider any ascending array A[] such that each A[i] > 0 for all (1 <= i <= N) and some conditions, the task is to output YES/NO, by checking that the above-given conditions are true for all possible ascending A[]'s or not with given S. Sum up all the elements
7 min read
Check whether the product of every subsequence is a perfect square or not Given an array arr[] consisting of N positive integers, the task is to check if the product of elements of every subsequence of the given array arr[] is a perfect square or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: arr[] = {1, 4, 100}Output: YesExplanation:Follo
5 min read
Check if the Product of all Array elements is a Perfect Square or not Given an array arr[] consisting of N positive integers, the task is to check if the product of all the elements of the given array arr[] is a perfect square or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: arr[] = {1, 4, 100}Output: YesExplanation: The product of al
6 min read
Check if the sum of perfect squares in an array is divisible by x Given an array arr[] and an integer x, the task is to check whether the sum of all the perfect squares from the array is divisible by x or not. If divisible then print Yes else print No. Examples: Input: arr[] = {2, 3, 4, 6, 9, 10}, x = 13 Output: Yes 4 and 9 are the only perfect squares from the ar
6 min read