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++
#include <iostream>
using namespace std;
int checkUnimodal( int arr[], int n)
{
int i = 1;
while (arr[i] > arr[i - 1] && i < n)
++i;
while (arr[i] == arr[i - 1] && i < n)
++i;
while (arr[i] < arr[i - 1] && i < n)
++i;
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
import java.util.*;
class Node
{
static boolean checkUnimodal( int arr[], int n)
{
int i = 1 ;
while (i < n && arr[i] > arr[i - 1 ])
++i;
while (i < n && arr[i] == arr[i - 1 ])
++i;
while (i < n &&arr[i] < arr[i - 1 ])
++i;
return (i == n);
}
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" );
}
}
|
Python3
def checkUnimodal(arr, n):
i = 1
while (i < n and arr[i] > arr[i - 1 ]):
i + = 1
while (i < n and arr[i] = = arr[i - 1 ]):
i + = 1
while (i < n and arr[i] < arr[i - 1 ]):
i + = 1
return (i = = n)
if __name__ = = '__main__' :
arr = [ 1 , 5 , 5 , 5 , 4 , 2 ]
n = len (arr)
if (checkUnimodal(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkUnimodal( int []arr, int n)
{
int i = 1;
while (i < n && arr[i] > arr[i - 1])
++i;
while (i < n && arr[i] == arr[i - 1])
++i;
while (i < n &&arr[i] < arr[i - 1])
++i;
return (i == n);
}
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" );
}
}
|
PHP
<?php
function check( $arr , $n )
{
$i = 1;
while ( $arr [ $i ] > $arr [ $i - 1] && $i < $n )
++ $i ;
while ( $arr [ $i ] == $arr [ $i - 1] && $i < $n )
++ $i ;
while ( $arr [ $i ] < $arr [ $i - 1] && $i < $n )
++ $i ;
return ( $i == $n );
}
$arr = array (1, 5, 5, 5, 4, 2);
$n = sizeof( $arr );
if (check( $arr , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkUnimodal(arr, n)
{
let i = 1;
while (arr[i] > arr[i - 1] && i < n)
++i;
while (arr[i] == arr[i - 1] && i < n)
++i;
while (arr[i] < arr[i - 1] && i < n)
++i;
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...