You are given two integers and denoting the number of elements in the array and an arbitrary integer respectively. You can perform the following operations:
- Select an index .
- Delete the element.
- Delete elements to the left of it if they exist or you can delete all the elements if the number of elements to the left of it is less than .
- Delete elements to the right of it if they exist or you can delete all if the number of elements to the right of it is less than .
Your task is to delete all the elements of the array using the minimum number of provided operations.
Example:
Input: n = 4, k = 1
Output: 2
Approach
Java
public class DeletingArrays {public static void main(String[] args) throws Exception {int n = 4;int k = 1;System.out.println((n + 2l * k) / (2l * k + 1));}}
C++
#include <bits/stdc++.h>using namespace std;int main(){int n = 4;int k = 1;cout << ((n + 2l * k) / (2l * k + 1)) << "\n";return 0;}
No comments:
Post a Comment