Questions


October 2023 1 2 Report
the physicists of a new world are working on an algorithm to create waves. An array can be represented as a wave if either of the following conditions is satisfied:
a1 > a2 and a2 < a3 and a3 > a4 and so on
a1 < a2 and a2 > a3 and a3 < a4 and so on
where a[i] is the element of the array.

Given an array arr of n integers, consisting of integers in the range 1 to m inclusive, or -1, find the number of ways to replace all the -1s in the array with an integer in the range 1 to m such that the resulting array can be represented as a wave. Since the answer can be large, compute it modulo 10^9 + 7

Example
Suppose n = 3, arr = [-1,3,-1], m =3
The possible ways to replace all the -1s in the array such that the resulting is a wave array are-
[1,3,2]
[1,3,1]
[2,3,1]
[2,3,2]
Hence the answer is 4

Function description:

Complete then function countWaysToCreateWave in the editor below.
countWaysToCreateWave have the following parameters:
arr[n]: an array of integers
m: an array

Returns
int: the number of ways to replace -1s in the array to make it a wave array, modulo 10^9+7


Code in C++

Answers & Comments


Add an Answer


Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Helpful Social

Copyright © 2024 EHUB.TIPS team's - All rights reserved.