Maximize first element of Array by deleting first or adding a previously deleted element
Last Updated :
08 Apr, 2022
Given an array arr[] of size N, and an integer K, the task is to maximize the first element of the array in K operations where in each operation:
- If the array is not empty, remove the topmost element of the array.
- Add any one of the previously removed element back at the starting of the array.
Examples:
Input: arr[] = [5, 2, 2, 4, 0, 6], K = 4
Output: 5
Explanation: The 4 operations are as:
- Remove the topmost element = 5. The arr becomes [2, 2, 4, 0, 6].
- Remove the topmost element = 2. The arr becomes [2, 4, 0, 6].
- Remove the topmost element = 2. The arr becomes [4, 0, 6].
- Add 5 back onto the arr. The arr becomes [5, 4, 0, 6].
Here 5 is the largest answer possible after 4 moves.
Input: arr[] = [2], K = 1
Output: -1
Explanation: Only one move can be applied and in the first move.
The only option is to remove the first element of the arr[].
If that is done the array becomes empty. So answer is -1
Approach: This problem can be solved with the help of the Greedy approach based on the following idea:
In first K-1 operations, the K-1 value of the starting can be removed. So currently at Kth node. Now in the last operation there are two possible choice:
- Either remove the current starting node (optimal if the value of the (K+1)th node is greater than the largest amongst first K-1 already removed elements)
- Add the largest from the already removed K-1 elements (optimal when the (K+1)th node has less value than this largest one)
Follow the illustration shown below for a better understanding.
Illustration:
For example arr[] = {5, 2, 2, 4, 0, 6}, K = 4
1st Operation:
=> Remove 5. arr[] = {2, 2, 4, 0, 6}
=> maximum = 5, K = 4 - 1 = 3
2nd Operation:
=> Remove 2. arr[] = {2, 4, 0, 6}
=> maximum = max (5, 2) = 5, K = 3 - 1 = 2
3rd Operation:
=> Remove 2. arr[] = {4, 0, 6}
=> maximum = max (5, 2) = 5, K = 2 - 1 = 1
4th Operation:
=> Here the current 2nd element i.e. 0 is less than 5.
=> So add 5 back in the array. arr[] = {5, 4, 0, 6}
=> maximum = max (5, 0) = 5, K = 1 - 1 = 0
Therefore the maximum possible first element is 5.
Follow the steps to solve the problem:
- If K = 0, then return first node value.
- If K = 1, then return the second node value(if any) else return -1 (because after K operations the list does not exist).
- If the size of the linked list is one then in every odd operation (i.e. 1, 3, 5, . . . ), return -1, else return the first node value (because if performed odd operation then array will become empty).
- If K > 2, then:
- Traverse first K-1 nodes and find out the maximum value.
- Compare that maximum value with the (K+1)th node value.
- If (K+1)th value is greater than the previous maximum value, update it with (K+1)th Node value. Otherwise, don't update the maximum value.
- Return the maximum value.
Below is the implementation of the above approach :
C++
// C++ code to implement the approach
#include <iostream>
using namespace std;
int maximumTopMost(int arr[], int k,int N){
// Checking if k is odd and
// length of array is 1
if (N == 1 and k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for(int i = 0; i < min(N, k - 1); i++)
ans = max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = max(ans, arr[k]);
// Returning ans
return ans;
}
// Driver code
int main() {
int arr[] = {5, 2, 2, 4, 0, 6};
int N = 6;
int K = 4;
cout <<(maximumTopMost(arr, K, N));
return 0;
}
// This code is contributed by hrithikgarg03188.
Java
// Java code to implement the approach
class GFG {
static int maximumTopMost(int[] arr, int k, int N)
{
// Checking if k is odd and
// length of array is 1
if (N == 1 && k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for (int i = 0; i < Math.min(N, k - 1); i++)
ans = Math.max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = Math.max(ans, arr[k]);
// Returning ans
return ans;
}
public static void main(String[] args)
{
int[] arr = { 5, 2, 2, 4, 0, 6 };
int N = 6;
int K = 4;
System.out.println(maximumTopMost(arr, K, N));
}
}
// This code is contributed by phasing17.
Python3
# Python code to implement the approach
def maximumTopMost(arr, k):
# Checking if k is odd and
# length of array is 1
if len(arr) == 1 and k % 2 != 0:
return -1
# Initializing ans with -1
ans = -1
# If k is greater or equal to the
# length of array
for i in range(min(len(arr), k - 1)):
ans = max(ans, arr[i])
# If k is less than length of array
if k < len(arr):
ans = max(ans, arr[k])
# Returning ans
return ans
# Driver code
if __name__ == "__main__":
arr = [5, 2, 2, 4, 0, 6]
K = 4
print(maximumTopMost(arr, K))
C#
// C# code to implement the approach
using System;
class GFG {
static int maximumTopMost(int[] arr, int k, int N)
{
// Checking if k is odd and
// length of array is 1
if (N == 1 && k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for (int i = 0; i < Math.Min(N, k - 1); i++)
ans = Math.Max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = Math.Max(ans, arr[k]);
// Returning ans
return ans;
}
// Driver code
public static void Main()
{
int[] arr = { 5, 2, 2, 4, 0, 6 };
int N = 6;
int K = 4;
Console.Write(maximumTopMost(arr, K, N));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code to implement the approach
const maximumTopMost = (arr, k) => {
// Checking if k is odd and
// length of array is 1
if (arr.length == 1 && k % 2 != 0)
return -1;
// Initializing ans with -1
let ans = -1;
// If k is greater or equal to the
// length of array
for (let i = 0; i < Math.min(arr.length, k - 1); ++i)
ans = Math.max(ans, arr[i]);
// If k is less than length of array
if (k < arr.length)
ans = Math.max(ans, arr[k]);
// Returning ans
return ans;
}
// Driver code
let arr = [5, 2, 2, 4, 0, 6];
let K = 4;
document.write(maximumTopMost(arr, K));
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize frequency of an element by at most one increment or decrement of all array elements | Set 2 Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1,
6 min read
Maximize distinct elements of Array by combining two elements or splitting an element Given an array arr[] of length N, the task is to maximize the number of distinct elements in the array by performing either of the following operations, any number of times: For an index i(0 ⤠i < N), replace arr[i] with a and b such that arr[i] = a + b.For two indices i (0 ⤠i < N) and n (0 â¤
9 min read
Maximum score of deleting an element from an Array based on given condition Given an array arr[], the task is to find the maximum score of deleting an element where each element of the array can be deleted with the score of the element, but the constraint is if we delete arr[i], then arr[i] + 1 and arr[i] - 1 is gets automatically deleted with 0 scores.Examples: Input: arr[
15 min read
Minimize operations to empty Array by deleting two unequal elements or one element Given an array A of size N, the task is to find the minimum number of operations required to make array A empty. The following operations can be performed: Choose any two elements, say X and Y, from A such that X != Y and remove them.Otherwise, choose any one element, say X, from A and remove it. Ex
9 min read
Maximum possible middle element of the array after deleting exactly k elements Given an integer array of size n and a number k. If the indexing is 1 based then the middle element of the array is the element at index (n + 1) / 2, if n is odd otherwise n / 2. The task is to delete exactly k elements from the array in such a way that the middle element of the reduced array is as
8 min read
Maximize distinct elements by incrementing/decrementing an element or keeping it same Given an array arr[] of N elements, the task is to maximize the count of distinct elements in the array, by either of the given operation on each element of the array: either increasing the element by 1or decreasing the element by 1or keeping the element as it is. Note: No element can be less than o
6 min read
Reduce the array by deleting elements which are greater than all elements to its left Given an array arr[] of N integers, the task is to delete the element from the given array if element to it's left is smaller than it. Keep on deleting the elements from the array until no element has a smaller adjacent left element. Print the resultant array after above operation. Examples: Input:
5 min read
Delete First Occurrence of Given Element from an Array Given an array of integers, the task is to delete a given element from the array. If there are multiple occurrences of the element, we need to remove only its first occurrence.Examples:Input: arr[] = [10, 20, 30, 40], ele = 20Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], ele = 25Output: [10,
8 min read
Minimize maximum difference between any pair of array elements by exactly one removal Given an array arr[] consisting of N integers(N > 2), the task is to minimize the maximum difference between any pair of elements (arr[i], arr[j]) by removing exactly one element. Examples: Input: arr[] = {1, 3, 4, 7}Output: 3Explanation:Removing the element 7 from array, modifies the array to {1
9 min read
Delete an Element from the end of an array Given an array of integers, the task is to delete an element from the end of the array.Examples:Input: arr[] = [10, 20, 30, 40]Output: [10, 20, 30]Input: arr[] = [20]Output: []Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approach 1] Using Built-In MethodsWe wil
5 min read