Open In App

Minimum Operations to form Sequence with Division

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:

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:

Code to implement the approach:




// 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;
}




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




# 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))




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));
    }
}




// 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));

Output
11

Time Complexity: O(logN)
Auxiliary Space: O(1)


Article Tags :