Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ program to find permutation with n peaks
Suppose we have two numbers n and k. We have to construct a permutation A using the numbers from 1 to n which has exactly k peaks. An index i is said to be peak of an array A, if A[i] > A[i-1] and A[i] > A[i+1]. If this is not possible, return -1.
So, if the input is like n = 5; k = 2, then the output will be [2, 4, 1, 5, 3], other answers are also possible.
Steps
To solve this, we will follow these steps −
if k > (n - 1) / 2, then: return -1 Define an array a of size: 101. for initialize i := 1, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; void solve(int n, int k) { if (k > (n - 1) / 2) { cout Input
5, 2Output
1, 3, 2, 5, 4,
Advertisements
