Find the amplitude and number of waves for the given array
Last Updated :
24 Feb, 2022
Given an array arr[] of N integers, the task is to find the amplitude and number of waves for the given array. If the array is not a wave array then print -1.
Wave Array: An array is a wave array if it is continuously strictly increasing and decreasing or vice-versa.
Amplitude is defined as the maximum difference of consecutive numbers.
Examples:
Input: arr[] = {1, 2, 1, 5, 0, 7, -6}
Output: Amplitude = 13, Waves = 3
Explanation:
For the array observe the pattern 1->2 (increase), 2->1 (decrease), 1->5 (increase), 5->0 (decrease), 0->7 (increase), 7->-6 (decrease). Amplitude = 13 (between 7 and -6) and total waves = 3
Input: arr[] = {1, 2, 1, 5, 0, 7, 7}
Output: -1
Explanation:
The array is not waved array as the last two elements of the array are equal, hence the answer is -1.
Approach:
The idea is to check for both sides adjacent elements where both must be either less or greater than the current element. If this condition is satisfied then count the number of waves otherwise print -1, where the number of waves is (n – 1) / 2. While traversing the array keep updating the maximum difference between the consecutive element to get the amplitude of the given wave array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int a[], int n)
{
int ma = a[1] - a[0];
for ( int i = 1; i < n - 1; i++) {
if ((a[i] > a[i - 1]
&& a[i + 1] < a[i])
|| (a[i] < a[i - 1]
&& a[i + 1] > a[i]))
ma = max(ma, abs (a[i] - a[i + 1]));
else
return false ;
}
cout << "Amplitude = " << ma;
cout << endl;
return true ;
}
int main()
{
int a[] = { 1, 2, 1, 5, 0, 7, -6 };
int n = sizeof a / sizeof a[0];
int wave = (n - 1) / 2;
if (check(a, n))
cout << "Waves = " << wave;
else
cout << "-1" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean check( int a[], int n)
{
int ma = a[ 1 ] - a[ 0 ];
for ( int i = 1 ; i < n - 1 ; i++)
{
if ((a[i] > a[i - 1 ] &&
a[i + 1 ] < a[i]) ||
(a[i] < a[i - 1 ] &&
a[i + 1 ] > a[i]))
ma = Math.max(ma, Math.abs(a[i] - a[i + 1 ]));
else
return false ;
}
System.out.print( "Amplitude = " + ma);
System.out.println();
return true ;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 1 , 5 , 0 , 7 , - 6 };
int n = a.length;
int wave = (n - 1 ) / 2 ;
if (check(a, n))
System.out.print( "Waves = " + wave);
else
System.out.print( "-1" );
}
}
|
Python3
def check(a, n):
ma = a[ 1 ] - a[ 0 ]
for i in range ( 1 , n - 1 ):
if ((a[i] > a[i - 1 ] and
a[i + 1 ] < a[i]) or
(a[i] < a[i - 1 ] and
a[i + 1 ] > a[i])):
ma = max (ma, abs (a[i] - a[i + 1 ]))
else :
return False
print ( "Amplitude = " , ma)
return True
if __name__ = = '__main__' :
a = [ 1 , 2 , 1 , 5 , 0 , 7 , - 6 ]
n = len (a)
wave = (n - 1 ) / / 2
if (check(a, n)):
print ( "Waves = " ,wave)
else :
print ( "-1" )
|
C#
using System;
class GFG{
static bool check( int []a, int n)
{
int ma = a[1] - a[0];
for ( int i = 1; i < n - 1; i++)
{
if ((a[i] > a[i - 1] &&
a[i + 1] < a[i]) ||
(a[i] < a[i - 1] &&
a[i + 1] > a[i]))
ma = Math.Max(ma, Math.Abs(a[i] - a[i + 1]));
else
return false ;
}
Console.Write( "Amplitude = " + ma);
Console.WriteLine();
return true ;
}
public static void Main(String[] args)
{
int []a = { 1, 2, 1, 5, 0, 7, -6 };
int n = a.Length;
int wave = (n - 1) / 2;
if (check(a, n))
Console.Write( "Waves = " + wave);
else
Console.Write( "-1" );
}
}
|
Javascript
<script>
function check(a, n)
{
let ma = a[1] - a[0];
for (let i = 1; i < n - 1; i++)
{
if ((a[i] > a[i - 1] &&
a[i + 1] < a[i]) ||
(a[i] < a[i - 1] &&
a[i + 1] > a[i]))
ma = Math.max(ma, Math.abs(a[i] - a[i + 1]));
else
return false ;
}
document.write( "Amplitude = " + ma);
document.write( "<br/>" );
return true ;
}
let a = [ 1, 2, 1, 5, 0, 7, -6 ];
let n = a.length;
let wave = (n - 1) / 2;
if (check(a, n))
document.write( "Waves = " + wave);
else
document.write( "-1" );
</script>
|
Output:
Amplitude = 13
Waves = 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...