Largest K digit integer from given Array divisible by M
Last Updated :
09 Dec, 2021
Given an array arr[] of size N and 2 integers K and M the task is to find the largest integer of K digits from the array arr[] which is divisible by M. Print -1 if it is impossible to find any such integer.
Note: The value of K is such that the found integer is always less than or equal to INT_MAX.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, N=6, K=3, M=4
Output: 652
Explanation: The largest of 3 digits is formed by the digits 6, 5, 2 with their indices 1. 4, and 5.
Input: arr[] = {4, 5, 4}, N=3, K=3, M=50
Output: -1
Explanation :- There exists no integer from the array which is divisible by 50 as there is no 0.
Naive Approach: This can be done by finding all sub-sequences of size K and then checking the condition to find the largest one.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to check every number in multiple of M in the array that if it is possible to form that number or not. If it is possible, then update the answer. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
void find(vector< int >& arr, int N, int K, int M)
{
if (K > N) {
cout << "-1\n" ;
return ;
}
vector< int > freq(10, 0);
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
sort(arr.begin(), arr.end());
int X = 0;
for ( int i = N - 1; K > 0; i--) {
X = X * 10 + arr[i];
K--;
}
if (X < M) {
cout << "-1\n" ;
return ;
}
int answer = -1;
for ( int i = M; i <= X; i = i + M) {
int y = i;
vector< int > v(10, 0);
while (y > 0) {
v[y % 10]++;
y = y / 10;
}
bool flag = true ;
for ( int j = 0; j <= 9; j++) {
if (v[j] > freq[j]) {
flag = false ;
break ;
}
}
if (flag)
answer = max(answer, i);
}
cout << answer << endl;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5, 6 };
int N = 6, K = 3, M = 4;
find(arr, N, K, M);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;;
class GFG
{
static void find( int [] arr, int N, int K, int M)
{
if (K > N) {
System.out.println( "-1\n" );
return ;
}
int freq[] = new int [ 10 ];
for ( int i = 0 ; i < N; i++) {
freq[arr[i]]++;
}
Arrays.sort(arr);
int X = 0 ;
for ( int i = N - 1 ; K > 0 ; i--) {
X = X * 10 + arr[i];
K--;
}
if (X < M) {
System.out.println( "-1" );
return ;
}
int answer = - 1 ;
for ( int i = M; i <= X; i = i + M) {
int y = i;
int v[] = new int [ 10 ];
while (y > 0 ) {
v[y % 10 ]++;
y = y / 10 ;
}
boolean flag = true ;
for ( int j = 0 ; j <= 9 ; j++) {
if (v[j] > freq[j]) {
flag = false ;
break ;
}
}
if (flag)
answer = Math.max(answer, i);
}
System.out.println(answer);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = 6 , K = 3 , M = 4 ;
find(arr, N, K, M);
}
}
|
Python3
def find(arr, N, K, M):
if (K > N):
print ( "-1" )
return
freq = [ 0 ] * 10
for i in range (N):
freq[arr[i]] + = 1
arr.sort()
X = 0
for i in range (N - 1 , 2 , - 1 ):
X = X * 10 + arr[i]
K - = 1
if (X < M):
print ( "-1" )
return
answer = - 1
for i in range (M, X + 1 , M):
y = i
v = [ 0 ] * 10
while (y > 0 ):
v[y % 10 ] + = 1
y = y / / 10
flag = True
for j in range ( 10 ):
if (v[j] > freq[j]):
flag = False
break
if (flag):
answer = max (answer, i)
print (answer)
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = 6
K = 3
M = 4
find(arr, N, K, M)
|
C#
using System;
public class GFG
{
static void find( int [] arr, int N, int K, int M)
{
if (K > N) {
Console.WriteLine( "-1\n" );
return ;
}
int []freq = new int [10];
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
Array.Sort(arr);
int X = 0;
for ( int i = N - 1; K > 0; i--) {
X = X * 10 + arr[i];
K--;
}
if (X < M) {
Console.WriteLine( "-1" );
return ;
}
int answer = -1;
for ( int i = M; i <= X; i = i + M) {
int y = i;
int []v = new int [10];
while (y > 0) {
v[y % 10]++;
y = y / 10;
}
bool flag = true ;
for ( int j = 0; j <= 9; j++) {
if (v[j] > freq[j]) {
flag = false ;
break ;
}
}
if (flag)
answer = Math.Max(answer, i);
}
Console.WriteLine(answer);
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = 6, K = 3, M = 4;
find(arr, N, K, M);
}
}
|
Javascript
<script>
function find(arr, N, K, M) {
if (K > N) {
document.write( "-1<br>" );
return ;
}
let freq = new Array(10).fill(0);
for (let i = 0; i < N; i++) {
freq[arr[i]]++;
}
arr.sort((a, b) => a - b);
let X = 0;
for (let i = N - 1; K > 0; i--) {
X = X * 10 + arr[i];
K--;
}
if (X < M) {
cout << "-1\n" ;
return ;
}
let answer = -1;
for (let i = M; i <= X; i = i + M) {
let y = i;
let v = new Array(10).fill(0);
while (y > 0) {
v[y % 10]++;
y = Math.floor(y / 10);
}
let flag = true ;
for (let j = 0; j <= 9; j++) {
if (v[j] > freq[j]) {
flag = false ;
break ;
}
}
if (flag)
answer = Math.max(answer, i);
}
document.write(answer);
}
let arr = [1, 2, 3, 4, 5, 6];
let N = 6, K = 3, M = 4;
find(arr, N, K, M);
</script>
|
Time Complexity: O(max(M, N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...