Find next greater number formed with exactly two unique digits for each Array element

• Last Updated : 13 Dec, 2021

Given an array arr[] having N integers, the task is to find the next greater number X i.e, X >= arr[i] for each i in the range [0, N) such that the count of unique digits in X is exactly 2.

Example:

Input: arr[] = {123, 234}
Output: 131 242
Explanation: For the given array, 131 is the smallest number greater that 123 having exactly 2 unique digits. Similarly, 242 is the smallest number greater that 234 having exactly 2 unique digits.

Input: arr[] = {35466666}
Output: 35533333

Naive Approach: The given problem can be solved by iterating over all the integers greater than arr[i] for each i in the range [0, N) and keeping track of the first integers such that the count of unique digits in the integer is exactly 2.

Time Complexity: O(N * M), where M represents the maximum element in the arr[].
Auxiliary Space: O(log N)

Efficient Approach: The above approach can be optimized using Bitmasking. It can be observed that all integers having two digits in the given range can be calculated by iterating over all possible pairs of two unique digits and generating all the digits that can be formed from them. It can be done by the algorithm discussed in this article. Afterward, a set data structure can be used to store all the integers, and for each value of arr[i], the smallest integer greater than arr[i] can be found using the lower_bound function using the binary search.

Below is the implementation of the above approach:

C++

 // C++ program of the above approach#include using namespace std; #define int long long // Stores the set of integers with 2 unique digitsset helper;vector nums; // Function to find the value of a^bint power(int a, int b){     // Stores the value    int ans = 1;    while (b > 0) {        if (b & 1) {            ans = ans * a;        }        b = b >> 1;        a = a * a;    }     // Return Answer    return ans;} void nextGreaterEle(int arr[], int N){     // Loop to iterate the given array    for (int i = 0; i < N; i++) {         // For each array element, find next        // greater element in the vector nums        // of integers using lower_bound        cout << *lower_bound(nums.begin(), nums.end(),                             arr[i])             << " ";    }} // Function to calculate the digits having// exactly two unique digitsvoid preProcess(){    // Loop to iterate over all possible    // pairs of digits from 0 to 9    for (int i = 0; i <= 9; i++) {        for (int j = 0; j <= 9; j++) {             // Stores the maximum length of integer            int len = 10;            for (int k = 0; k <= (1 << len); k++) {                int temp = k;                int number = 0;                int curLen = 0;                while (temp > 0) {                    if (temp & 1) {                         // Include numbers with the                        // next digit as i                        number = i * power(10, curLen)                                 + number;                    }                    else {                         // Include numbers with the next                        // next digit as j                        number = j * power(10, curLen)                                 + number;                    }                     // Update temp                    temp = (temp >> 1);                    curLen++;                }                 // Insert the current number into the set                helper.insert(number);                while (curLen <= len) {                    number = j * power(10, curLen) + number;                    helper.insert(number);                    curLen++;                }            }        }    }     // Loop to insert all the integers into    // a vector from the set if the unique digits    // in the integer is exactly two.    for (auto cur : helper) {         // Stores the unique digits        set count;        int orz = cur;        while (cur > 0) {            count.insert(cur % 10);            cur = cur / 10;        }         // If count of exactly two        if (count.size() == 2) {            nums.push_back(orz);        }    }} // Driver Codesigned main(){    int arr[] = { 123, 234 };    int N = sizeof(arr) / sizeof(arr);     preProcess();    nextGreaterEle(arr, N);     return 0;}
Output
131 242

Time Complexity: O(106 + N * log N)  = O(N * log N)
Auxiliary Space: O(106) = O(1)

My Personal Notes arrow_drop_up