Given an integer N, the task is to generate a sequence of N positive integers such that:
- Every element at the even position must be greater than the element succeeding it and the element preceding it i.e. arr[i – 1] < arr[i] > arr[i + 1]
- Sum of the elements must be even and minimum possible (among all the possible sequences).
Input: N = 4
Output: 1 2 1 2
Input: N = 5
Output: 1 3 1 2 1
Approach: In order to get the sequence with the minimum sum possible, the sequence must be of the form 1, 2, 1, 2, 1, 2, 1 … and for cases when the sum of the sequence is not even, any 2 from the sequence can be changed to a 3 to make the sum of the sequence even.
Below is the implementation of the above approach:
1 3 1 2 1 2 1 2 1
- Find minimum value to assign all array elements so that array product becomes greater
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Minimum number of integers required to fill the NxM grid
- Minimum removals from array to make GCD greater
- Number of arrays of size N whose elements are positive integers and sum is K
- Find integers that divides maximum number of elements of the array
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Minimum elements to be added in a range so that count of elements is divisible by K
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Minimum element whose n-th power is greater than product of an array of size n
- Minimum steps needed to cover a sequence of points on an infinite grid
- Median in a stream of integers (running integers)
- Sum of minimum elements of all subarrays
- Minimum sum after subtracting multiples of k from the elements of the array
- Minimum gcd operations to make all array elements one
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.