Deleting arrays

You are given two integers N and K denoting the number of elements in the array and an arbitrary integer respectively. You can perform the following operations:

  1. Select an index i.
  2. Delete the ith element.
  3. Delete K 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 K.
  4. Delete K 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 K.

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[] argsthrows 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