My code is giving a wrong answer in codechef
First of all, you should use double rather then float (precision matters)!
Secondly, you should update your conditions for status because you take in consideration only the first sub-task with (T less than 10, and N less than 100) which will give you only 20 points! the second sub-task (that rewards 80 points) takes a T less than 70 and a N less than 1000.
Finally, the issue with the code comes from the condition of updating pa & pb, you use :
Integer.parseInt(f1) == A[0] // same for B[0]
instead of
Integer.parseInt(f1) == A[t] // same for B[t]
Here is the complete code and submission results
import java.util.*;
import java.lang.*;
/**
*
* @author aoubidar
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// number of test cases
int T = Integer.parseInt(in.nextLine());
int[] N = new int[T];
int[] A = new int[T];
int[] B = new int[T];
double[] Probability = new double[T];
for (int t = 0; t < T; t++) {
String[] input = in.nextLine().split(" ");
N[t] = Integer.parseInt(input[0]);
A[t] = Integer.parseInt(input[1]);
B[t] = Integer.parseInt(input[2]);
int total, pa = 0, pb = 0 ;
String[] faces = in.nextLine().split(" ");
total = faces.length;
for (String f : faces) {
if (Integer.parseInt(f) == A[t]) {
pa++;
}
if (Integer.parseInt(f) == B[t]) {
pb++;
}
}
double pn = (double) (total * total);
Probability[t] = (pa * pb) / pn ;
}
for (double d : Probability) {
System.out.println(d);
}
}
}
submission success:
Python: error invalid literal for int() with base 10
You only have a couple of problems.
- The
while
loop keeps checkingn
. It only needs to checksum
- The final print tries to add an
int
to astr
. Just use print arguments instead.
def persistence(n):
count = 1
sum = int(str(n)[0:1]) * int(str(n)[1:2])
while sum > 9:
sum = int(str(sum)[0:1]) * int(str(sum)[1:2])
count = count + 1
print(sum)
print("how many times: ", count)
persistence(39)
Output as expected.
However, you should not use sum
as the name of a variable. You can reuse n
instead:
def persistence(n):
count = 1
n = int(str(n)[0:1]) * int(str(n)[1:2])
while n > 9:
n = int(str(n)[0:1]) * int(str(n)[1:2])
count = count + 1
print(n)
print("how many times: ", count)
Error correction in names
For phonetic matching, see Soundex.
I think modifying a Levenshtein distance algorithm to treat "abbreviate to an initial" and "expand from an initial" as single-distance edits ought to be straightforward, but the details are beyond me at the moment.
Truthful solution of binary search task
You might want to revise how to implement a binary search:
Your getValue function should be changed to:
private static int getValue() {
int highLimit = sum / managerCount;
int lowLimit = 0;
int halfPart;
boolean isNear;
while (highLimit > lowLimit) {
halfPart = lowLimit + (highLimit - lowLimit + 1) / 2;
if (isNear(halfPart, managerCount)) {
lowLimit = halfPart;
} else {
highLimit = halfPart - 1;
}
}
return lowLimit;
}
Why Array Searching Error Fail for case [-2,0,10,-19,4,6,-8]
The problem is that you always compare x-th element with itself. Almost always it won't be a problem. However, in case of 0 (zero), 0 * 2 == 0
is true. All you need to do is to skip the loop iteration if i == j
. Actually if you look closely at your task, it is stated in there that i != j
in the formal definition.
Why am I for this simple task getting the Timed out error even if the arrays are not that big?
Looks like your array A
doesn't contain any positive numbers. Thus your loop does not break.
O(n log n) solution:
public int solution(int[] A) {
final int solution[] = {1};
Arrays.stream(A)
.filter(i -> i > 0)
.sorted()
.forEach(i -> {
if (i == solution[0]) {
solution[0]++;
}
});
return solution[0];
}
O(n) solution:
public int solution2(int[] A) {
BitSet bitSet = new BitSet();
Arrays.stream(A)
.filter(i -> i > 0)
.forEach(bitSet::set);
return bitSet.nextClearBit(1);
}
Related Topics
How to Set a Timeout on a Spring Boot Rest API
Java Spring: Best Way to Convert a File to a Multipartfile
How to Convert Linkedhashmap to Custom Java Object
Junit 5 - No Parameterresolver Registered for Parameter
Spring Boot Could Not Resolve Placeholder in String
Testing @Postconstruct With Mockito
How to Test for Blank Line With Java Scanner
Return Json Object from a Spring-Boot Rest Controller
No String-Argument Constructor/Factory Method to Deserialize from String Value ('')
Regular Expression (Regex). How to Ignore or Exclude Everything in Between
Parameter 0 of Constructor in Required a Bean of Type 'Java.Lang.String' That Could Not Be Found
How to Sort Date in Descending Order Using Comparator
Set Zoom Level to 100% in Selenium Webdriver When Default Is Not 100
Loop Over All Fields in a Java Class
How to Solve Liquibase Checksum Validation Fail After Liquibase Upgrade
How to Mock Classes With Constructor Injection
Calculate the Number of Items Displayed by Recyclerview and Place in a Textview