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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
#define int long long
// Function to find minimum number of operations
// to form required sequence
int minimumOperations(int N)
{
// low and high pointer with respective range
int low = 1, high = 2e9;
// while loop till low and high are not
// adjacent
while (high - low > 1) {
// finding mid element
int mid = (low + high) / 2;
// if sum from 1 to mid is less than equal to N + 1
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
// returning minimum operations answer
return N - low + 1;
}
// Driver Code
int32_t main()
{
// Input
int N = 15;
// Function Call
cout << minimumOperations(N) << endl;
return 0;
}
Java
import java.util.Scanner;
public class MinimumOperations {
// Function to find the minimum number of operations
// to form the required sequence
static long minimumOperations(long N) {
// Low and high pointers with the respective range
long low = 1, high = 2_000_000_000;
// While loop until low and high are not adjacent
while (high - low > 1) {
// Finding the mid element
long mid = (low + high) / 2;
// If the sum from 1 to mid is less than or equal to N + 1
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
// Returning the minimum operations answer
return N - low + 1;
}
// Driver Code
public static void main(String[] args) {
// Input
long N = 15;
// Function Call
System.out.println(minimumOperations(N));
}
}
// This code is contributed by akshitaguprzj3
Python3
# Function to find minimum number of operations
# to form the required sequence
def minimum_operations(N):
# low and high pointers with respective range
low, high = 1, 2e9
# while loop till low and high are not adjacent
while high - low > 1:
# finding the mid element
mid = (low + high) // 2
# if the sum from 1 to mid is less than or equal to N + 1
if (mid * (mid + 1)) // 2 <= N + 1:
low = mid
else:
high = mid
# returning the minimum operations answer
return N - low + 1
# Driver Code
if __name__ == "__main__":
# Input
N = 15
# Function Call
print(minimum_operations(N))
C#
using System;
class Program
{
// Function to find minimum number of operations
// to form the required sequence
static long MinimumOperations(long N)
{
// low and high pointers with respective range
long low = 1, high = 2_000_000_000;
// Loop until low and high are not adjacent
while (high - low > 1)
{
// Finding the mid element
long mid = (low + high) / 2;
// If sum from 1 to mid is less than or equal to N + 1
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
// Returning the minimum operations answer
return N - low + 1;
}
// Driver Code
static void Main(string[] args)
{
// Input
long N = 15;
// Function Call
Console.WriteLine(MinimumOperations(N));
}
}
JavaScript
// Function to find minimum number of operations
// to form the required sequence
function minimumOperations(N) {
// Low and high pointers with respective range
let low = 1, high = 2e9;
// While loop until low and high are not adjacent
while (high - low > 1) {
// Finding the mid element
let mid = Math.floor((low + high) / 2);
// If sum from 1 to mid is less than or equal to N + 1
if ((mid * (mid + 1)) / 2 <= N + 1)
low = mid;
else
high = mid;
}
// Returning the minimum operations answer
return N - low + 1;
}
// Driver Code
let N = 15;
// Function Call
console.log(minimumOperations(N));
Time Complexity: O(logN)
Auxiliary Space: O(1)
Similar Reads
Minimum steps to reduce N to 0 by given operations Give an integer N, the task is to find the minimum number of moves to reduce N to 0 by one of the following operations: Reduce N by 1.Reduce N to (N/2), if N is divisible by 2.Reduce N to (N/3), if N is divisible by 3. Examples: Input: N = 10Output: 4Explanation: Here N = 10Step 1: Reducing N by 1 i
11 min read
Minimum number operations required to convert n to m | Set-2 Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1. Examples: Input: n = 120, m = 51840, a = 2, b = 3
5 min read
Minimize operations required to obtain N Given an integer N, the task is to obtain N, starting from 1 using minimum number of operations. The operations that can be performed in one step are as follows: Multiply the number by 2.Multiply the number by 3.Add 1 to the number. Explanation: Input: N = 5 Output: 3 Explanation: Starting value: x
6 min read
Increasing sequence with given GCD Given two integers n and g, the task is to generate an increasing sequence of n integers such that: The gcd of all the elements of the sequence is g.And, the sum of all the elements is the minimum among all possible sequences. Examples: Input: n = 6, g = 5 Output: 5 10 15 20 25 30 Input: n = 5, g =
3 min read
Reduce N to 1 by given operations Given an integer N. Then your task is to output a minimum number of operations to reduce N into 1. You can below operations to do the same: Subtract 1 from NUpdate N to N/2, if N is divisible by 2Update N to N/3, if N is divisible by 3Examples: Input: N = 10Output: 3Explanation: The operations are p
5 min read