Minimum Operations to form Sequence with Division
Last Updated :
29 Jan, 2024
Consider a sequence S1 consisting of first (N+1) natural numbers. Then your task is to output the minimum number of operations required to make sequence S2 of first N natural numbers using the below steps of operation:
- Remove an element from S1 let’s say K and divide it into M number of integers such (A1 + A2 + . . . + AM) = K
- Insert all or some integers into S2 from among M integers.
Examples:
Input: N = 4
Output: 3
Explanation: S1 = {1, 2, 3, 4, 5}, then we have to construct S2 as = {1, 2, 3, 4}. Let us take S2 Initially empty.
- First operation: Choose K = S1[5] = 5, divide it into 1 and 4 and insert both in S2. Updated S1 and S2 are {1, 2, 3, 4} and {1, 4} respectively.
- Second operation: Choose K = S1[4] = 4, divide it into 1 and 3 and just insert 3 in S2. Updated S1 and S2 are {1, 2, 3} and {1, 3, 4} respectively.
- Third operation: Choose K = S1[2] = 2, don’t dividing and insert 2 directly in S2. Updated S1 and S2 are {1, 3} and {1, 2, 3, 4} respectively.
Now, it can be seen that S2 contains first N = 4 natural numbers and minimum operations required are 3. Therefore, output is 3.
Input: N = 15
Output: 11
Explanation: It can be verified that the minimum number of operations required will be 11.
Approach: We can solve this problem using below idea:
Binary search can be used to solve this problem. The main observation is that we have to pick N + 1 for forming first K elements of required sequence as [1, 2, 3,. ., K]. Since we can divide N + 1 in any number of integers. We have to find maximum K such that K * (K + 1) / 2 <= N + 1. After that our required answer will be (N – Kmax + 1). As we know that, sum of first N natural numbers is = N * (N + 1) / 2.
Steps were taken to solve the problem:
- Declare two variables let say Low and High with value 1 and 2*109 respectively.
- Run a while loop with condition (High – Low > 1) and follow below mentioned steps under the scope of loop:
- Declare a variable let say Mid and initialize it with (Low + High) / 2.
- If (Sum of first mid elements of sequence is less than equal to N + 1)
- Else
- Return N – Low + 1.
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int minimumOperations( int N)
{
int low = 1, high = 2e9;
while (high - low > 1) {
int mid = (low + high) / 2;
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
return N - low + 1;
}
int32_t main()
{
int N = 15;
cout << minimumOperations(N) << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class MinimumOperations {
static long minimumOperations( long N) {
long low = 1 , high = 2_000_000_000;
while (high - low > 1 ) {
long mid = (low + high) / 2 ;
if ((mid * (mid + 1 )) / 2 <= N + 1 )
low = mid;
else
high = mid;
}
return N - low + 1 ;
}
public static void main(String[] args) {
long N = 15 ;
System.out.println(minimumOperations(N));
}
}
|
Python3
def minimum_operations(N):
low, high = 1 , 2e9
while high - low > 1 :
mid = (low + high) / / 2
if (mid * (mid + 1 )) / / 2 < = N + 1 :
low = mid
else :
high = mid
return N - low + 1
if __name__ = = "__main__" :
N = 15
print (minimum_operations(N))
|
C#
using System;
class Program
{
static long MinimumOperations( long N)
{
long low = 1, high = 2_000_000_000;
while (high - low > 1)
{
long mid = (low + high) / 2;
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
return N - low + 1;
}
static void Main( string [] args)
{
long N = 15;
Console.WriteLine(MinimumOperations(N));
}
}
|
Javascript
function minimumOperations(N) {
let low = 1, high = 2e9;
while (high - low > 1) {
let mid = Math.floor((low + high) / 2);
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
return N - low + 1;
}
let N = 15;
console.log(minimumOperations(N));
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...