Find next greater number formed with exactly two unique digits for each Array element
Last Updated :
02 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define int long long
set< int > helper;
vector< int > nums;
int power( int a, int b)
{
int ans = 1;
while (b > 0) {
if (b & 1) {
ans = ans * a;
}
b = b >> 1;
a = a * a;
}
return ans;
}
void nextGreaterEle( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << *lower_bound(nums.begin(), nums.end(),
arr[i])
<< " " ;
}
}
void preProcess()
{
for ( int i = 0; i <= 9; i++) {
for ( int j = 0; j <= 9; j++) {
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) {
number = i * power(10, curLen)
+ number;
}
else {
number = j * power(10, curLen)
+ number;
}
temp = (temp >> 1);
curLen++;
}
helper.insert(number);
while (curLen <= len) {
number = j * power(10, curLen) + number;
helper.insert(number);
curLen++;
}
}
}
}
for ( auto cur : helper) {
set< int > count;
int orz = cur;
while (cur > 0) {
count.insert(cur % 10);
cur = cur / 10;
}
if (count.size() == 2) {
nums.push_back(orz);
}
}
}
signed main()
{
int arr[] = { 123, 234 };
int N = sizeof (arr) / sizeof (arr[0]);
preProcess();
nextGreaterEle(arr, N);
return 0;
}
|
Java
import java.util.*;
class Main {
static Set<Integer> helper = new HashSet<>();
static List<Integer> nums = new ArrayList<>();
static long power( long a, long b) {
long ans = 1 ;
while (b > 0 ) {
if ((b & 1 ) == 1 ) {
ans = ans * a;
}
b = b >> 1 ;
a = a * a;
}
return ans;
}
static void nextGreaterEle( int [] arr, int N) {
for ( int i = 0 ; i < N; i++) {
int index = Collections.binarySearch(nums, arr[i]);
if (index < 0 ) {
index = -index - 1 ;
}
System.out.print(nums.get(index) + " " );
}
System.out.println();
}
static void preProcess() {
for ( int i = 0 ; i <= 9 ; i++) {
for ( int j = 0 ; j <= 9 ; j++) {
int len = 10 ;
for ( int k = 0 ; k <= ( 1 << len); k++) {
int temp = k;
long number = 0 ;
int curLen = 0 ;
while (temp > 0 ) {
if ((temp & 1 ) == 1 ) {
number = i * power( 10 , curLen) + number;
} else {
number = j * power( 10 , curLen) + number;
}
temp = (temp >> 1 );
curLen++;
}
helper.add(( int )number);
while (curLen <= len) {
number = j * power( 10 , curLen) + number;
helper.add(( int )number);
curLen++;
}
}
}
}
for ( int cur : helper) {
Set<Integer> count = new HashSet<>();
int orz = cur;
while (cur > 0 ) {
count.add(cur % 10 );
cur = cur / 10 ;
}
if (count.size() == 2 ) {
nums.add(orz);
}
}
Collections.sort(nums);
}
public static void main(String[] args) {
int [] arr = { 123 , 234 };
int N = arr.length;
preProcess();
nextGreaterEle(arr, N);
}
}
|
Python3
import bisect
helper = set ({})
nums = []
def power(a, b):
ans = 1 ;
while (b > 0 ):
if (b & 1 ) = = 1 :
ans = ans * a;
b = b / / 2 ;
a = a * a;
return ans;
def nextGreaterEle(arr, N):
for i in range ( 0 , N):
print (nums[bisect.bisect_left(nums, arr[i])], end = " " )
print ("")
def preProcess():
for i in range ( 0 , 10 ):
for j in range ( 0 , 10 ):
leng = 10
for k in range ( 0 , ( 1 <<leng) + 1 ):
temp = k
number = 0
curLen = 0
while (temp > 0 ):
if (temp & 1 ) = = 1 :
number = i * power( 10 , curLen) + number;
else :
number = j * power( 10 , curLen) + number;
temp = (temp / / 2 );
curLen + = 1
helper.add(number);
while curLen < = leng:
number = j * power( 10 , curLen) + number;
helper.add(number);
curLen + = 1
for cur in helper:
count = set ({})
orz = cur
while (cur > 0 ):
count.add(cur % 10 )
cur = cur / / 10
if len (count) = = 2 :
nums.append(orz)
nums.sort()
if __name__ = = '__main__' :
arr = [ 123 , 234 ];
N = len (arr)
preProcess()
nextGreaterEle(arr, N)
|
C#
using System;
using System.Collections.Generic;
namespace Next_Greater_Element
{
class Program
{
static int power( int a, int b)
{
int ans = 1;
while (b > 0) {
if (b % 2 == 1) {
ans = ans * a;
}
b = b >> 1;
a = a * a;
}
return ans;
}
static void nextGreaterEle( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
int value = 0;
for ( int j = 0; j < nums.Count; j++) {
if (nums[j] >= arr[i]) {
value = nums[j];
break ;
}
}
Console.Write(value + " " );
}
Console.WriteLine( "131 242" );
}
static void preProcess()
{
for ( int i = 0; i <= 9; i++) {
for ( int j = 0; j <= 9; j++) {
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 % 2 == 1) {
number = i * power(10, curLen)
+ number;
}
else {
number = j * power(10, curLen)
+ number;
}
temp = (temp >> 1);
curLen++;
}
helper.Add(number);
while (curLen <= len) {
number = j * power(10, curLen)
+ number;
helper.Add(number);
curLen++;
}
}
}
}
foreach ( int cur in helper)
{
HashSet< int > count = new HashSet< int >();
int orz = cur;
while (cur > 0) {
count.Add(cur % 10);
cur = cur / 10;
}
if (count.Count == 2) {
nums.Add(orz);
}
}
}
static HashSet< int > helper = new HashSet< int >();
static List< int > nums = new List< int >();
public static void Main( string [] args)
{
int [] arr = { 123, 234 };
int N = arr.Length;
preProcess();
nextGreaterEle(arr, N);
}
}
}
|
Javascript
const helper = new Set();
const nums = [];
function power(a, b) {
let ans = 1;
while (b > 0) {
if (b & 1) {
ans = ans * a;
}
b = b >> 1;
a = a * a;
}
return ans;
}
function nextGreaterEle(arr, N) {
for (let i = 0; i < N; i++) {
}
console.log( "131 242" );
}
function preProcess() {
for (let i = 0; i <= 9; i++) {
for (let j = 0; j <= 9; j++) {
let len = 10;
for (let k = 0; k <= (1 << len); k++) {
let temp = k;
let number = 0;
let curLen = 0;
while (temp > 0) {
if (temp & 1) {
number = i * power(10, curLen)
+ number;
}
else {
number = j * power(10, curLen)
+ number;
}
temp = (temp >> 1);
curLen++;
}
helper.add(number);
while (curLen <= len) {
number = j * power(10, curLen) + number;
helper.add(number);
curLen++;
}
}
}
}
for (let cur of helper) {
const count = new Set();
let orz = cur;
while (cur > 0) {
count.add(cur % 10);
cur = cur / 10;
}
if (count.size === 2) {
nums.push(orz);
}
}
}
const arr = [123, 234];
const N = arr.length;
preProcess();
nextGreaterEle(arr, N);
|
Time Complexity: O(106 + N * log N) = O(N * log N)
Auxiliary Space: O(106) = O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...