Code not Working For Large Permutations [closed] - java

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 8 years ago.
Improve this question
I've just completed a codility test and only achieved a score of 81%. My code failed when a 'large permutation' was tested against it.
I've got no idea why this failed, as the spec says all values are integers, and my for loop uses only int values. I would really appreciate it if somebody could look at my code and tell me why it provides a value of -1 for massive permutations:-
https://codility.com/demo/results/demo4G8CJS-9YN/
class Solution {
public int solution(int X, int[] A) {
// write your code in Java SE 8
int target = X;
int[] path = new int[X];
for(int i = 0; i < A.length-1; i++) {
if(A[i] != path[A[i]-1]) {
path[(A[i]-1)] = A[i];
target--;
}
if(target==0) {
return i;
}
}
return -1;
}
}

It should be for (int i = 0; i < A.length; i++)(not i < A.length - 1). As of now, the last element of the array is just ignored. It actually fails a very simple test: an array of one element and X = 1.

Related

Show numbers not incluided in an Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've an issue that I couldn't fix, I tried different ways. I filled an Array with ok 100 positions with randoom numbers (Range: 1-100). Then I've to show by console the numers that aren't incluides in the array. I try two loopes nested but it didn't work neither.
int arraySize = 100, aux=0;
int numeros [] = new int [arraySize];
for (int i = 0; i < numeros.length; i++) {
numeros[i] = (int) ((Math.random()*100)+1);
Here, Somebody know how to get it? Thanks
I'd just stream all the numbers and check which ones aren't included in the array. Note that in order to get better performance it may be useful to convert the array to a Set, but it isn't strictly required:
Set<Integer> existing = Arrays.stream(numeros).boxed().collect(Collectors.toSet());
IntStream.rangeClosed(1, 100)
.boxed()
.filter(i -> !existing.contains(i))
.forEach(System.out::println); // or collect them to use later
Instead of nested for loops, for better performance, try one of the following options:
Sort the array using Arrays.sort(numeros), then iterate and print missing values whenever a gap is detected.
Add all the numbers to a HashSet<Integer>, then iterate 1-100 and print any value not found in the Set.
Since your question is obviously an exercise or test, I'll leave the actual code writing to you.
Well I could fix it with an counter, It was easier more than i think, but in stressfull peaks we don't think in a right way. I'll let the Code below.
int numeros [] = new int [arraySize];
for (int i = 0; i < numeros.length; i++) {
numeros[i] = (int) ((Math.random()*100)+1);
}
//Loop to show not numbers in the array
for (int i = 0; i < numeros.length; i++) {
for (int j = 0; j < numeros.length; j++) {
if (i==numeros[j]) {
cont++;
}
}
if (cont==0) {
System.out.println(i);
}
cont=0;
}

How can i count how many times the keywords user enter is the used in the essay. its doesn't check on the entire keyword [closed]

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 6 years ago.
Improve this question
public static int keywordsChecker(String essay,String key) {
int count = 1;
String[] k=key.split(",");
for (int i = 0; i < k.length-1; i++) {
if (essay.contains(k[i])) {
count++;
}
}
return count;
}
To take into account that each keyword searched for may occur more than once, and to count such occurrences, you may use this inside your for loop:
int indexOfOccurrence = essay.indexOf(k[i]);
while (indexOfOccurrence > -1) {
count++;
indexOfOccurrence = essay.indexOf(k[i], indexOfOccurrence + 1);
}
There are a couple of other issues in your code: I believe you need to initialize count to 0 (not 1). And to count also the last keyword in key your for loop should be for (int i = 0; i < k.length; i++) (without subtracting 1 from k.length). If you want, using <= would also work: for (int i = 0; i <= k.length-1; i++), but this is non-standard, so I would not recommend it.

An array with the terms that are the differences of consecutive terms (adjacent) in another array [closed]

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 7 years ago.
Improve this question
I currently have:
public double[] differences()
{
diffs = new doublae[sequence.length-1];
for (int = 0; i<sequence.length; ++i){
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}
return diffs;
}
However this does not work when i run a test program to check it.
You range should be different - i shouldn't pass sequence.length - 2 in order for i+1 to be a valid index.
double[] diffs = new double[sequence.length-1];
for (int = 0; i < sequence.length - 1; i++) { // changed the condition
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}

Java beginner- Can somebody debug my code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
import java.util.*;
import javax.swing.*;
public class Practice {
public static void main(String[] args){
int[] numbers = {1, 2, 3, 14, 15, 16, 17};
getSmall(numbers);
}
public static void getSmall(int[] ar){
int small=0;
for(int i=0; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
}
The program is to find the smallest number in the array, there is no compiler error but it doesn't show the correct result.
Thank you in advance!
public static void getSmall(int[] ar){
int small=ar[0];
for(int i=1; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
Setting small initially to 0 is a bad idea as you are hoping that your array contains an element less than that for the answer to be correct.
A common idiom is to initialise small to ar[0] (having, of course, first checked that ar contains at least 1 element). Then run your loop from 1.
for(int i = 1;
(I dislike initialising small to a very large number since that puts the answer to your function in an undefined state if ar does not have any elements.)
Change
int small = 0
to
int small = ar[0];
Don't init small=0,
change to int small = ar[0]
You can also use
int small=Integer.MAX_VALUE;
in place of
int small=0;
this code will print small value, give the max of integer
Change this
public static void getSmall(int[] ar) {
int small = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
to
public static void getSmall(int[] ar) {
int small = ar[0];
for (int i = 1; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
because variable small is not assigned to any value from your array

What's wrong with this code that it doesn't compile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For some reason the true and false are considered "incompatible types". Am I only suppose to run this through a boolean method? What's wrong with it.
for(int i = 0; i < array.length ; i++)
{
int val = (array[i] % 2);
if(val == 0)
array[i] = true;
else
array[i] = false;
}
Well array is probably an int[], given that you're using array[i] % 2 and assigning the result to an int.
There's no conversion from boolean to int, so you can't store your result back in the int[] array. It's not clear what you're trying to do, but that's why it's not compiling.
Aside
If you had a separate boolean[] of the same size, that would work - although it would be more simply written as:
boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
even[i] = (array[i] % 2) == 0;
}
Any time you find yourself with:
if (someCondition) {
doSomething(true);
} else {
doSomething(false);
}
you should consider refactoring it to:
doSomething(someCondition);
Your array contains wrong types:
int[] a = {1, 2, 4};
boolean[] b = {true, false};
b[0] = 1; //error
a[0] = 1; //ok

Categories

Resources