Wednesday, February 8, 2023
HomeSoftware DevelopmentMost size of sequence shaped from value N

Most size of sequence shaped from value N


Given  N cash, the sequence of numbers consists of {1, 2, 3, 4, ……..}. The price for selecting a quantity in a sequence is the variety of digits it comprises. (For instance value of selecting 2 is 1 and for 999 is 3), the duty is to print the Most variety of components a sequence can include.

Any factor from {1, 2, 3, 4, ……..}. can be utilized at most 1 time. 

Examples: 

Enter: N = 11
Output: 10
Rationalization: For N = 11 -> deciding on 1 with value 1,  2 with value 1,  3 with value 1,  4 with value 1,  5 with value 1,  6 with value 1,  7 with value 1,  8 with value 1,  9 with value 1, 10 with value 2.
totalCost = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2  = 11.

Enter: N = 189
Output: 99

Naive strategy: The essential method to resolve the issue is as follows:

Iterate i from 1 to infinity and calculate the associated fee for present i if the associated fee for i is greater than the variety of cash which is N then i – 1 would be the reply.

Time Complexity: O(N * logN)
Auxiliary Area: O(1)

Environment friendly Method: The above strategy might be optimized primarily based on the next concept:

This Drawback might be solved utilizing Binary Search. Quite a few digits with given value is a monotonic operate of sort T T T T T F F F F. Final time the operate was true will generate a solution for the Most size of the sequence. 

Observe the steps under to unravel the issue:

  • If the associated fee required for digits from 1 to mid is lower than equal to N replace low with mid.
  • Else excessive with mid – 1 by ignoring the fitting a part of the search area.
  • For printing solutions after binary search test whether or not the variety of digits from 1 to excessive is lower than or equal to N if that is true print excessive
  • Then test whether or not the variety of digits from 1 to low is lower than or equal to N if that is true print low.
  • Lastly, if nothing will get printed from above print 0 for the reason that size of the sequence will probably be 0.

Beneath is the implementation of the above strategy:

C++

// C++ program for above strategy
#embody <bits/stdc++.h>
utilizing namespace std;

// Perform to depend whole quantity
// of digits from numbers 1 to N
int totalDigits(int N)
{

    int cnt = 0LL;
    for (int i = 1; i <= N; i *= 10)
        cnt += (N - i + 1);

    return cnt;
}

// Perform to search out Most size of
// Sequence that may be shaped from value
// N
void findMaximumLength(int N)
{

    int low = 1, excessive = 1e9;

    whereas (excessive - low > 1) {
        int mid = low + (excessive - low) / 2;

        // Verify if value for variety of digits
        // from 1 to N is lower than equal to N
        if (totalDigits(mid) <= N) {

            // atleast mid would be the reply
            low = mid;
        }
        else {

            // igonre proper search area
            excessive = mid - 1;
        }
    }

    // Verify if excessive might be the reply
    if (totalDigits(excessive) <= N)
        cout << excessive << endl;

    // else low might be the reply
    else if (totalDigits(low) <= N)
        cout << low << endl;

    // else reply will probably be zero.
    else
        cout << 0 << endl;
}

// Driver Code
int primary()
{

    int N = 11;

    // Perform Name
    findMaximumLength(N);

    int N1 = 189;

    // Perform name
    findMaximumLength(N1);

    return 0;
}

Time Complexity: O(logN2)  (first logN is for logN operations of binary search, the second logN is for locating the variety of digits from 1 to N)
Auxiliary Area: O(1)

Associated Articles: 



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments