My solution is not accepted by the online judge. Any help would be appreciated.
JP and Strings
A 1-character string is which consists of only one letter 'a',
2-character string consists of only letters 'a' and 'b'. Similarly
26-character string consists of all the small letters in alphabet i.e
from 'a' to 'z'. A string is valid if there is no character in the
string which has the same position as in the alphabetical sequence.
For example, string "bac" is not valid because the character 'c' is at
the position 3 in the string(Consider string and alphabets both as
1-indexed) which is same as in the alphabetical sequence. Whereas
string "cab" is valid. Now there is a function F(N , L) such that,
F(N , L) = Number of valid N-Character strings of length L.
Input: The first line will contain the number of queries Q. Next Q
lines will contain two space separated integers containing N and L.
Output: For every query print a single line containing the value of
the function F(N , L). Answer may be too large so print it modulo
1000000007.
Constraints: 1 <= Q <= 10^3 1 <= N <= 26 1 <= L <= 10^9
Statement: Given two integers N and L where N represents the number of
characters and L represents the length of string.
My solution approach is:
There will be two possible use cases:
1.) L <= N If length of string L is less or equal to N then at each position we can choose N-1 characters so total number of strings will be (N-1)^L
2.) L > N If length of string L is grater than N then number of possible strings will be: (N-1)^N * N^(L-N) For rest N position we can choose N-1 characters but after Nth position all N characters can be used.
Code: http://ideone.com/fzGLKH
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner();
int q = in.nextInt();
int n,l;
while(q>0){
n = in.nextInt();
l = in.nextInt();
if(n<l){
System.out.println((int)((Math.pow(n-1, n)*Math.pow(n, l-n))% 1000000007));
}
else{
System.out.println((int)(Math.pow(n-1, l)% 1000000007));
}
q--;
}
}
What's the bug?
You are doing Math.pow(n - 1, l) % 1000000007.
The problem's constraints is 1 <= l <= 1000000000, so it may be a huge value like 10^9-digit number. It will cause overflow and you cannot get correct answer.
The solution for the bug
What do you do if you want to calculate a^b mod m (a^b is the bth power of a).
You can use Exponentation by squaring algorithm for calculating a^b mod m.
The algorithm in java is following:
public static long modpow(long a, long b, long m) {
long ret = 1;
while(b > 0) {
if((b & 1) == 1) ret = ret * a % m;
a = a * a % m;
b >>= 1;
}
return ret;
}
The time complexity is O(log b), so you can solve this problem!
Related
I am trying to identify a solution (preferably in JAVA) for the given scenario :
Input :
Binary String : e.g. "1001"
N - Number Of Iterations
K - String index position
After each iteration, all the 1s become "01" and 0s become "10"
e.g.
initial value/input --> "1001"
after 1st iteration --> "01101001"
after 2nd iteration --> "1001011001101001"
...
...
after Nth iteration --> "100101011......[Kth index value]......00101010111.." (here our program needs to find the Kth index value that would be either 0 or 1)
Output :
value (either 0 or 1) at kth index after N iterations.
After N iterations, each original digit becomes 2N digits, so skip digits until we get close to K.
E.g. for input "1001", N=3, K=20, the 1st digit becomes 8 digits, so if we skip those 8 digits twice, i.e. skip 2 original digits = 16 K's, we're at the 3nd original digit (a 0), and have a remaining K=4.
We then apply an iteration on that digit, and get 0 -> "10". We can now restart the operation, with input "10", N=2, K=4.
Repeat until done. Solution in O(N), regardless of input length or value of K.
Of course, you could just brute-force it, but that won't perform well at higher values, since solution is O(L * 2N), with L being the input length.
// Brute-force solution
static char solve(String input, int n, int k) {
String s = input;
for (int i = 0; i < n; i++)
s = s.chars().mapToObj(c -> c == '0' ? "10" : "01").collect(Collectors.joining());
return s.charAt(k); // assuming k is 0-based
}
Compact version of solution described above:
// Optimized solution
static char solve(String input, int n, int k) {
for (; n > 0; k %= 1 << n--)
input = input.charAt(k / (1 << n)) == '0' ? "10" : "01";
return input.charAt(k);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm writing a code that determines the smallest integer that is a sequence of sevens followed by some number of zeros (possibly none) that is divisible by int n. Since this number can be massive, the return value should be a BigInteger.
My code so far has an if-else ladder that covers the case that if any int n is not divisible by two or five is guaranteed to only contain sevens (no zeros). In the case where int n is not divisible by two or five, my thought process was to continue adding sevens to a LinkedList in a while loop, until that list (converted to a BigInteger) is divisible by int n. The same logic goes for the case where int n is divisible by two or five, except a two for-loops would add seven and zero to the list.
My code is getting a runtime error when converting the list to a string and then to a BigInteger, specifically on the line BigInteger numBig = new BigInteger(str);. The error is: "java.lang.NumberFormatException: Zero length BigInteger (in java.math.BigInteger)" Also, I'm not quite sure the logic is sound for the case where int n is divisible by two or five.
You don't need BigInteger for this task. The idea is the following:
First you determine the number of required zeros. Since number composed of only sevens cannot be divided by 2 or 5, the number of zeros is equal to the maximum power of 2 or 5 in number n.
Now we have a number n which is not divisible by 2 or 5. Suppose that a remainder of the division of a number composed of m sevens by n is equal to r:
777...m-times..777 mod n = r
Then number composed of (m+1) sevens will have a remainder 10*r + 7, because
777..(m+1)-times...777 = 777...m-times...7 * 10 + 7
So you can just recalculate the remainder until it becomes zero.
public static BigInteger method(int n) {
int two;
for (two = 0; n % 2 == 0; two++) n /= 2;
int five;
for (five = 0; n % 5 == 0; five++) n /= 5;
int zeros = Math.max(two, five);
int sevens = 1;
int r = 7 % n;
while (r != 0) {
r = (r * 10 + 7) % n;
sevens++;
}
// Now just make a number of 'sevens' sevens and 'zeros' zeros:
StringBuilder result = new StringBuilder();
for (int i = 0; i < sevens; i++) {
result.append("7");
}
for (int i = 0; i < zeros; i++) {
result.append("0");
}
return new BigInteger(result.toString());
}
"Zero length BigInteger" means you're trying to create a BigInteger from something that has length of 0. The stack trace would tell you on which line exactly.
I would guess the bug is in your convert method. If you pass in an empty list, it tries to convert an empty string into BigInteger with new BigInteger("")
I don't know what your algorithm is supposed to do in this case. If for example you want to convert an empty list into the number zero, you can do:
if (res.isEmpty()) return BigInteger.ZERO;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
interviewstreet Triplet challenge
There is an integer array d which does not contain more than two elements of the same value. How many distinct ascending triples (d[i] < d[j] < d[k], i < j < k) are present?
Input format:
The first line contains an integer N denoting the number of elements in the array. This is followed by a single line containing N integers separated by a single space with no leading/trailing spaces
Output format:
A single integer that denotes the number of distinct ascending triples present in the array
Constraints:
N <= 10^5
Every element of the array is present at most twice
Every element of the array is a 32-bit positive integer
Sample input:
6
1 1 2 2 3 4
Sample output:
4
Explanation:
The distinct triplets are
(1,2,3)
(1,2,4)
(1,3,4)
(2,3,4)
EDIT: I assumed that the integers were ordered, which wasn't mentioned in your answer. With unordered integers, no optimization is possible, you'd have to use the brute force counting approach. If they are sorted, continue reading.
Thing is, you don't really need a lot of programming here. This is more of a mathematical problem.
First, it doesn't matter if there are 1, 2 or more occurences of an integer. The resulting triplets ave to be unique anyway. This reduces to merely counting how many different integers there are in the array. Name that variable s.
Then we merely apply following formula:
result = 0;
for(first_index = 0; first_index < s; first_index++) {
for(second_index = first_index + 1; second_index < s; second_index++) {
result += s - second_index - 1;
}
}
return result;
This can, however, be simplified. Now, if we reason about the values s - second_index - 1 takes for one outer loop, that's just the row of all integers from 0 to s - 2 reversed! Aren't we glad that there's a formula for its sum: where n is an integer, n * (n + 1) / 2 is the sum of the first n integers.
This means we can optimize our program to:
result = 0;
for(first_index = 0; first_index < s; first_index++) {
result += (s - 2 - first_index) * (s - 2 - first_index + 1) / 2;
}
Note that we can further simplify this to result = (x^3 - 3*x^2 + 2*x) / 6.
Help me to understand how this code works. It essentially adds commas into a string of numbers. So if the user types a 1 to 3 digit number it is unchanged. For a four digit number ,it adds a comma so
1111 becomes 1,111
11111 becomes 11,111
111111111 becomes 11,111,111
and so on. Here's the code:
private String addCommasToNumericString (String digits)
{
String result = "";
int len = digits.length();
int nDigits = 0;
for (int i = len - 1; i >= 0; i--)
{
result = digits.charAt(i) + result;
nDigits++;
if (((nDigits % 3) == 0) && (i > 0))
{
result = "," + result;
}
}
return (result);
}
I´ll explain what I do understand of it
The for loop basically counts the length of the number the user has written to avoid putting a comma before the first number (e.g. ,1111). And while i is less than the length of the string it subtracts 1.
result returns the char at position i, since it counts downwards it returns the chars "opposite" from right towards left.
nDigits adds 1 from to the initial value of 0 on each iteration through the loop.
I guess now is where I am having trouble seeing exactly what is going on: if ("nDigits % 3) == 0.
So for the two first iteration through loop it will not execute the if loop because:
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
nDigits starts out as 1 because of the nDigits++ code inside the for loop, so how does it put the comma after three digits and not two? And how does it know when there is only 4 or 5 digits to place the comma corretly at position 1 and two (1,111 - 11,111)?
I think the easiest way to explain this is to slow it down to each pass.
The loop starts at the end of the string so if you have the string 12345, then after the first time through the loop result will be "5" and nDigits will be 1.
The next time through, '4' will be added to the front of the result giving you "45" and nDigits will be 2.
The third time through, it adds '3' to the front of result making that "345" and then the if-then triggers and adds a comma to the front. Result is now ",345".
More passes will give you "12,345".
I think what is confusing you is that loop starts at the '5' and not at the '1'. Everything is added to the front of result and not to the end as you would normally expect.
Hope this helps!
The key thing in this method is to count the digits from right to left. If you don't do it that way it won't work.
You can also do the same with String Manipulation instead of char manipulation. Maybe it makes it easier to understand so I'll provide an example.
My solution involves the use of the subString Method and operates in a similar manner to yours. Starting FROM RIGHT TO LEFT, it divides the original String in two substrings and adds a comma in between them every time there is a 3 digits group.
private String addCommas (String digits) {
String result = digits;
if (digits.length() <= 3) return digits; // If the original value has 3 digits or less it returns that value
for (int i = 0; i < (digits.length() – 1) / 3; i++) {
int commaPos = digits.length() – 3 – (3 * i); // comma position in each cicle
result = result.substring(0, commaPos) + "," + result.substring(commaPos);
}
return result;
}
The variable result is used for incremental build of the final output, in each iteration one or two chars are concatenated from left (i.e. the string is build from right to left).
One char is concatenated everytime by running
result = digits.charAt(i) + result;
it is the actual digit
the second char is concatenated in each third iteration by running
result = "," + result;
it is the order separator
The implementation is not optimal at all, because in Java the string are immutable and result = "," + result; ends up in creating a new object. The StringBuffer or StringBuilder are far more effective for this purpose.
Essentially what this does is start at the last digit of the number and iterate through from right to left, prepending them to the result String and putting a comma in every 3 characters.
In this particular code, len holds the total length of the number and nDigits is a count of how many of those digits have been evaluated already. Starting at position len-1 (so the index of the last digit of the number), the for-loop iterates through position 0 (the first digit of the number). It takes the digit at position i, puts it at the front of the result String, and then evaluates if there should be a comma in front of it. nDigits % 3 will return 0 every 3rd digit, so the if statement evaluates if there should be a comma by checking that if there have been 3 digits written and the one you just wrote was not 0.
for (int i = len - 1; i >= 0; i--)
i starts with len - 1, to start from the last digit. i > 0 in if (((nDigits % 3) == 0) && (i > 0)) is the one that avoid a comma before the first number (e.g. ,1111).
I modified answer of #Antonio Ricardo Diegues Silva for my purposes.
/**
* Get number in {#link String} with divider after 'n' number of digits
* #param number number for processing
* #param n amount after which will be inserted divider
* #return {#link String} number with dividers
*/
public static <T extends Number> String insertDividerBetweenEveryNDigits(T number, int n, String divider) {
StringBuilder builder = new StringBuilder().append(number);
int digitsNumber = builder.length();
if (digitsNumber > n) { // If the original value has n digits or less it just returns that value
for (int i = 1; i <= (digitsNumber - 1) / n; i++) {
int dividerPos = digitsNumber - (n * i); // divider position in each cycle
builder.insert(dividerPos, divider);
}
}
return builder.toString();
}
I have a number. This number has many digits. I want to write a function which returns the largest number that consists of some digits of that number. While getting that largest number, the sequence of the digits should not change.
int myFunction(int n, int cat){
...
return max;
}
If n = 38462637 and cat = 3 the function has to return 86637, i.e. if cat = 3 the function is expected to return 5-digit number, as 8 - 3 = 5. The original number has many variations of 5 digits numbers, but the largest possible number is 86637. In this case, the most important requirement is that the digits should not change their place.
Be greedy - select the largest digit that can be leftmost in the answer(if there are several positions where this digit appears, choose its leftmost occurance). A digit may be leftmost if it is not 0 and we have at least n - cat - 1 digits to the right of it.
After that use the same algorithm to create the largest number on the right of the position of this digit that has exactly n - cat - 1 digits. Continue iterating until you have your number composed. Only note that the digits you select after the first iteration may be zero(as they will no longer be leftmost in the resulting number)
EDIT: best solution that uses the algorithm described above - use range minimum query to compute the highest value that is possible for each consecutive digit position. In theory this can be done in constant time per query and linear extra memory using linear precomputation, but the algorithm is so complex and hard to implement that it will only give you improvement for really big values of n. I personally suggest using a segment tree approach that will result in O(n*log(n)) time complexity.
This is probably a bit overcomplicated, but it seems to work:
public static int myFunction(int n, int cat) {
String numString = String.valueOf(n);
int finalLength = numString.length() - cat;
int[] positions = new int[finalLength];
StringBuilder answer = new StringBuilder();
for (int i = 0; i < finalLength; i++) {
for (int j = (i == 0 ? i : positions[i - 1] + 1); j <= numString.length() - finalLength + i; j++) {
if (positions[i] == 0 || numString.charAt(j) > numString.charAt(positions[i]) ) {
positions[i] = j;
}
}
answer.append(numString.charAt(positions[i]));
}
return Integer.parseInt(answer.toString());
}
[EDIT]: A cleaner version without all the String nonsense:
public static int myFunction(int n, int cat) {
List<Integer> digits = new ArrayList<Integer>();
int number = n;
while (number > 0) {
digits.add(number % 10);
number /= 10;
}
int finalLength = digits.size() - cat;
int lastIndex = digits.size();
int answer = 0;
for (int i = 0; i < finalLength; i++) {
int highestDigit = -1;
for (int j = lastIndex - 1; j >= finalLength - i - 1; j--) {
if (digits.get(j) > highestDigit) {
highestDigit = digits.get(j);
lastIndex = j;
}
}
answer = answer * 10 + highestDigit;
}
return answer;
}
If you have access to the code, store the number as a string with a seperator (space, comma, etc) in it, then use the string separator function to put each number (string character) into it's own array location. Parse the string array and make an integer array. Then run a quick sort on the array. When that is done, take the first X number of integers and that is your number.