Problem Statement:
There is a broken calculator. Only a few of the digits [0 to 9
] and operators [+, -, *, /
] are working.
A req no. needs to be formed using the working digits and the operators. Each press on the keyboard is called an operation.
=
operator is always working and is used when the req no. is formed using operators.-1
needs to be printed in case the req no. cannot be formed using the digits and the operators provided OR exceeds the max no. of operations allowed.- At no point in time during the calculation of the result, the no. should become negative or exceed 999 [
0 <= calcno <= 999
]
Input:
- 1st line contains 3 space separated nos: no. of working digits, no. of working operators, max. no of operations allowed.
- 2nd line contains space separated working digits.
- 3rd line contains space separated working operators [
1
represents+
,2
represents-
,3
represents*
,4
represents/
]. - 4th line contains the req. no to be formed.
Output:
Find the minimum required operations to form the req no.
Example:
Input 1:
2 1 8
2 5
3
50
Possible ways:
Case 1: 2*5*5
= -> 6 operations
Case 2: 2*25
= -> 4 operations
4 is the req Answer
Input 2:
3 4 8
5 4 2
3 2 4 1
42
Possible ways:
Case 1: 42
-> 2 operations
(direct key in)
Case 2: 5*4*2+2
= -> 8 operations
..........some other ways
2 is the req Answer
I am not getting a proper approach to this problem.
Can someone suggest some ways to approach the problem.