Open In App

Minimum Operations to form Sequence with Division

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)
      • Update Low = mid
    • Else
      • Update High = mid.
  • 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));


Output

11

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads