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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Minimum array elements to be changed to make Recaman's sequence
- Generate N integers satisfying the given conditions
- Find minimum value to assign all array elements so that array product becomes greater
- Find sum in range L to R in given sequence of integers
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count of array elements which are greater than all elements on its left
- Generate array with minimum sum which can be deleted in P steps
- Generate elements of the array following given conditions
- Generate an Array such with elements maximized through swapping bits
- Generate original array from difference between every two consecutive elements
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Longest sub-sequence with minimum LCM
- Find the minimum permutation of A greater than B
- Minimum number of integers required to fill the NxM grid
- Find integral points with minimum distance from given set of integers using BFS
- Minimum removals from array to make GCD greater
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.