Smallest number after n removals after k day
Last Updated :
23 Dec, 2023
Given a set containing all the natural numbers till infinity. You have k days and an array arr, on each day you remove a[i]th smallest natural number from the set, the task is to tell the smallest number in the set after k days.
Examples:
Input: N = 5, K = 1, arr[] = {1, 2, 4, 5, 6}
Output: 3
Input: N = 5, K = 3, arr = {1, 3, 5, 6, 7}
Output: 9
Approach: To solve the problem follow the below idea:
Consider the scenario where numbers are arranged in increasing order on a line. Instead of deleting numbers at positions a1, a2, …, an in each operation and then checking the first number after k operations, we approach it differently. Begin with the number 1 at the front and attempt to insert zeroes right after positions a1-1, a2-2, …, an-n in each operation. The goal is to have the zeroes occupy positions a1, a2, …, an after the insertions. After k insertions, we examine the position that 1 will end up at.
If a1 is not equal to 1, the answer is 1. Otherwise, each insertion can be processed in O(1) time if we keep track of how many of a1-1, a2-2, …, an-n are before the current position x of 1. If a1-1 through ai-i are before x, then we will insert i zeroes before x.
Step-by-step approach:
- Initializes variables j (pointer for array traversal) and x (smallest number).
- Iterate for each day: Indicates the start of the loop that simulates each day’s removal operation.
- Iterate through the array elements until the condition is met (i.e, the inner loop that increments j until the condition arr[j] <= x + j is false.)
- Update the smallest number based on the current position (i.e, updates the smallest number x based on the current position of j.)
- Output the smallest number after k days
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
int main()
{
ll n = 5, k = 1;
vector<ll> arr = { 1, 2, 4, 5, 6 };
ll j = 0, x = 1;
while (k--) {
while (j < n && arr[j] <= x + j)
j++;
x += j;
}
cout << x << "\n";
return 0;
}
|
Java
import java.util.*;
public class SmallestNumber {
public static void main(String[] args)
{
long n = 5 , k = 1 ;
List<Long> arr = Arrays.asList(1L, 2L, 4L, 5L, 6L);
long j = 0 , x = 1 ;
while (k-- > 0 ) {
while (j < n && arr.get(( int )j) <= x + j)
j++;
x += j;
}
System.out.println(x);
}
}
|
Python3
n = 5
k = 1
arr = [ 1 , 2 , 4 , 5 , 6 ]
j = 0
x = 1
while k > 0 :
while j < n and arr[j] < = x + j:
j + = 1
x + = j
k - = 1
print (x)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class SmallestNumber
{
public static void Main( string [] args)
{
long n = 5, k = 1;
List< long > arr = new List< long > { 1, 2, 4, 5, 6 };
long j = 0, x = 1;
while (k-- > 0)
{
while (j < n && arr[( int )j] <= x + j)
j++;
x += j;
}
Console.WriteLine(x);
}
}
|
Javascript
let n = 5, k = 1;
let arr = [1, 2, 4, 5, 6];
let j = 0, x = 1;
while (k > 0) {
while (j < n && arr[j] <= x + j)
j++;
x += j;
k--;
}
console.log(x);
|
Time complexity: O(k + n).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...