Given two numbers N and P. The task is to generate an array of all positive elements, and in one operation you can choose a minimum number in the array and subtract it from all array elements. If the array element becomes 0 then you will remove it.
You have to print the minimum possible sum of the array and one possible array such that after applying exactly P steps the array will vanish.
Input : N = 4, P = 2
The Minimum Possible Sum is: 5
The Array Elements are: 1 2 1 1
The array can be [1, 2, 1, 1] after 1st step it becomes [0, 1, 0, 0] and it becomes  and after step 2 it will be vanished.Thus the sum is 5 and it is minimum possible value.
Input : N = 3 , P = 1
The Minimum Possible Sum is: 3
The Array Elements are: 1 1 1
Approach: The problem can be solved by following a greedy approach. First, we will place first P natural numbers, and for rest (N – P) positions we will fill it with 1, because we have to minimize the sum.
So the sum will be P*(P+1)/2 + (N – P).
Below is the implementation of above approach:
# Python3 implementation of above approach
# Function to find the required array
def findArray(N, P):
# calculating minimum possible sum
ans = (P * (P + 1)) // 2 + (N – P);
arr =  * (N + 1);
# place firts P natural elements
for i in range(1, P + 1):
arr[i] = i;
# Fill rest of the elements with 1
for i in range(P + 1, N + 1):
arr[i] = 1;
print(“The Minimum Possible Sum is: “, ans);
print(“The Array Elements are: “);
for i in range(1, N + 1):
print(arr[i], end = ” “);
# Driver Code
N = 5;
P = 3;
# This code is contributed by mits
The Minimum Possible Sum is: 8 The Array Elements are: 1 2 3 1 1
Time Complexity: O(N)
- Count minimum steps to get the given desired array
- Minimum steps to reach end of array under constraints
- Make array elements equal in Minimum Steps
- Minimum steps to make all the elements of the array divisible by 4
- Generate two BSTs from the given array such that maximum height among them is minimum
- Minimum steps to color the tree with given colors
- Find the minimum number of steps to reach M from N
- Minimum steps to come back to starting point in a circular tour
- Minimum steps needed to cover a sequence of points on an infinite grid
- Generate minimum sum sequence of integers with even elements greater
- Generate original array from an array that store the counts of greater elements on right
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Generate elements of the array following given conditions
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.