Why doesn't this array check work? - java

I am trying to check an array for a 1 or a 3, if either is found, print false, else print true. I ahve this:
if(array[i] == 1){
bool = false;
}
else if(array[i] == 3){
bool = false;
}
else{
bool = true;
}
However it does not work in all cases.
If the input is 0 2 4 it prints true as it should.
but if the input is 4 2 7 1 8 it should be false as there is a 1, but it prints true.
What am i doing wrong?
EDIT: Added a break to the if and changed the if statement. Works now.
if(array[i] == 1 || array[i] == 3){
bool = false;
break;
}
else{
bool = true;
}

A better approach is
boolean flag = true;
for(int i=0; i<myArray.length; i++)
{
if(myArray[i]==1 || myArray[i]==3){
flag = false;
break;
}
}
return flag;

this code
if(array[i] == 1 || array[i] == 3){
bool = false;
break;
}
else{
bool = true;
}
equal
if(array[i] == 1){
bool = false;
}else{
bool = true;
}
if(array[i] == 3){
bool = false;
}
else{
bool = true;
}

Related

Are there any efficient micro-optimizations to find the number of unique grid paths?

I am trying to solve this CSES problem: Grid Paths. You are given a string of length 48, and you have to find the amount of paths such that you traverse all of the grid and end up at the lower left corner.
I believe I have pruned the search to the best of my ability, as according to this book: CP Handbook (Look in the pruning the search category), the best optimization for this type of problem is to prevent your path from closing yourself off, and I have already implemented this. The time limits for this specific problem are tight, and although I have basically solved this problem, I am still failing 1-2 test cases because my solution takes around 1.01 seconds instead of being below the 1 second time limit.
Finally, I just wanted to know if there were any cool micro-optimizations I could use to marginally enhance the speed of my java code, so I could actually pass all of the test cases for this problem.
import java.io.*;
public class GridPaths {
public static class FastIO {
InputStream dis;
byte[] buffer = new byte[1 << 17];
int pointer = 0;
public FastIO(String fileName) throws Exception {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) {
dis = is;
}
int nextInt() throws Exception {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
long nextLong() throws Exception {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
Integer[] readArray(int n) throws Exception {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
byte nextByte() throws Exception {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
String next() throws Exception {
StringBuilder ret = new StringBuilder();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
}
static char[] board;
static boolean[][] visited = new boolean[7][7];
static int ans = 0;
public static boolean works(int i, int j) {
//makes sure that current spot is on the 7x7 grid and is not visited
return (i >= 0 && i<=6 && j>=0 && j<=6 && !visited[i][j]);
}
public static void solve(int i, int j, int steps) {
if (i == 6 && j == 0) {
if (steps == 48) ans++; //all spots of the grid have to be visited in order to be counted as part of the answer
return;
}
visited[i][j] = true;
//you are given ? characters in the input string, and those mean that you have to try out all 4 combinations (U,D,L,R)
if (board[steps] == '?' || board[steps] == 'L') {
//second condition of the second if statement checks if the spot directly ahead of the current spot is blocked, and if it is, the left and right spots cannot both be unvisited or else you will not continue searching
if (works(i,j-1) && !(!works(i,j-2) && works(i+1,j-1) && works(i-1,j-1))) {
solve(i, j - 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'R') {
if (works(i,j+1) && !(!works(i,j+2) && works(i+1,j+1) && works(i-1,j+1))) {
solve(i, j + 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'U') {
if (works(i-1,j) && !(!works(i-2,j) && works(i-1,j+1) && works(i-1,j-1))) {
solve(i - 1, j, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'D') {
if (works(i+1,j) && !(!works(i+2,j) && works(i+1,j+1) && works(i+1,j-1))) {
solve(i + 1, j, steps + 1);
}
}
visited[i][j] = false;
}
public static void main(String[] args) throws Exception {
FastIO in = new FastIO(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
board = in.next().toCharArray();
solve(0,0,0);
out.println(ans);
out.close();
}
}
Note: I am already using one of the fastest, if not the fastest, ways to receive input in Java, so I do not believe I can actually improve upon that.
I've been playing around with this. In addition to using a standard mechanism for reading the input file (which I suggested in a comment), you can gain a little time in the search alg itself by doing two things:
Break the case board[steps] == '?' off from the other cases. So check for board[steps] == '?' first, and just try all four directions in that case. Otherwise (the else case for if (board[steps] == '?'), just check for U/D/L/R. Since for most steps, the character will be '?', you save having to make the U/D/L/R tests most of the time.
Look up the character to be tested once, with c = board[steps],and then use c in each test instead of board[steps].
Doing these two things saved about 5% it seems. I was doing 100 reps of the solve and timing with System.currentTimeMillis(). I know there are more accurate ways of timing, but this was good enough to see a definite improvement even though the times jumped around quite a bit trial to trial. The best I ever saw in each case was 3600 millis for 100 iterations as originally written vs 3400 millis with the improvements.
My guess is that it's mostly the first change that matters. I'd expect the compiler to be doing the second already, but I didn't try the two optimizations independently.
I also solved this problem in java (AC), and here is how I did it
public static char[] defaultPath;
public static boolean[][] isUsed;
public static int counter = 0;
public static void solve(int indexChar, int indexRow, int indexColumn) {
if (indexRow == 8 && indexColumn == 2) {
if (indexChar == 48) {
counter++;
}
}else {
// Find correct way: 'D', 'U', 'L', 'R'
char correctWay = '?';
// (1) (1)
// 0 1 or 1 0
// 1 1
if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow+1][indexColumn-1])
&& isUsed[indexRow+2][indexColumn] && !isUsed[indexRow+1][indexColumn]) {
correctWay = 'D';
}
// 1 1
// 0 1 or 1 0
// (1) (1)
else if ((isUsed[indexRow-1][indexColumn+1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow-1][indexColumn] && isUsed[indexRow-2][indexColumn]) {
correctWay = 'U';
}
// 1 0 (1) or 1
// 1 1 0 (1)
else if ((isUsed[indexRow+1][indexColumn-1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow][indexColumn-1] && isUsed[indexRow][indexColumn-2]) {
correctWay = 'L';
}
//(1) 0 1 or 1
// 1 (1) 0 1
else if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow-1][indexColumn+1])
&& !isUsed[indexRow][indexColumn+1] && isUsed[indexRow][indexColumn+2]) {
correctWay = 'R';
}
// Check input path (default path)
char c = defaultPath[indexChar];
if (c == '?') {
if (correctWay == '?') {
// 'D'
if (!isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}
// 'U'
if (!isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}
// 'L'
if (!isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}
// 'R'
if (!isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}else {
if (correctWay == 'D') {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (correctWay == 'U') {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (correctWay == 'L') {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (correctWay == 'R') {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}else {
if (c == correctWay || correctWay == '?') {
if (c == 'D' && !isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (c == 'U' && !isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (c == 'L' && !isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (c == 'R' && !isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
defaultPath = scanner.next().toCharArray();
isUsed = new boolean[11][11];
for (int i = 0; i < 11; i++) {
isUsed[0][i] = true;
isUsed[1][i] = true;
isUsed[9][i] = true;
isUsed[10][i] = true;
isUsed[i][0] = true;
isUsed[i][1] = true;
isUsed[i][9] = true;
isUsed[i][10] = true;
}
isUsed[2][2] = true;
isUsed[8][1] = false;
isUsed[9][2] = false;
solve(0, 2, 2);
System.out.println(counter);
scanner.close();
}

Is there any way to optimize this method?

I have this code, basically check is there any null. I used SonarQube and I have to optimize as much as I can, I feel is ok but i have to try. Any ideas?
In the following code, each fila has simple records of Strings.
public boolean isColumnNull(DbfReader reader, int[] pos) {
Object[] fila = null;
boolean isNull = false;
int cont = 0;
while (cont < pos.length) {
while ((fila = reader.nextRecord()) != null) {
for (int j = 0; j < fila.length; j++) {
if ((j == pos[0] || j == pos[1]) && fila[j] == null) {
isNull = true;
break;
}
}
cont++;
}
}
return isNull;
}
The inner for loop can be replaced with
if ((pos[0] < fila.length && fila[pos[0]] == null) ||
(pos[1] < fila.length && fila[pos[1]] == null)) {
isNull = true;
break;
}
As suggested by Andy Turner both pos[0] and pos[1] should be checked that they are >= 0, this should be done once before the while loop starts.

What's wrong with my simple Java program?

It's just supposed to print the prime numbers below 100 but it only gets the number '3' as an output. I'm only just starting to learn Java so it all looks right to me.
public class ClassesAndObjects {
public static void main(String[] args) {
Prime n = new Prime();
for (int i = 3; i < 100; i++){
n.Number = i;
n.factors();
}
}
}
class Prime{
long Number;
long fact;
boolean state = true;
void factors(){
for (fact = 2; fact < Number; fact++){
if (fact != Number){
if (Number % fact == 0){
state = false;
break;
}
}
}
if (state == true){
System.out.println(Number);
}
}
}
You have to reset the boolean state to true at the beginning of each call, otherwise it's always false except for the first call (when i =3 )
void factors(){
state = true;
for (fact = 2; fact < Number; fact++){
if (fact != Number){
if (Number % fact == 0){
state = false;
break;
}
}
}
if (state == true){
System.out.println(Number);
}
}
Add a statement like this:
if (state == true){
System.out.println(Number);
}
state = true; //reset the state variable
Here we are resetting the state variable to true for next iteration.
Well, you dont have state = true as default in your factor() method. So when it runs state = false for the first time (happens when Number = 4), then it is always false. Just add the bolded line in your code and you are good to go.
void factors(){
**boolean state = true;**
for (fact = 2; fact < Number; fact++){
if (fact != Number){
if (Number % fact == 0){
state = false;
break;
}
}
}
if (state == true){
System.out.println(Number);
}
}

Why is this becoming an infinite loop?

All i need this method to do is take a string lets say "aaxbb" and it will return true because there is an 'aa' and a 'bb',
If the string given is length = 0 or length = 1 it should fail.
The issue i'm having is that which i do not know.. i know that in my terminal after hasAdjacentPair's first test case Pass's, i get a blinking curser meaning that somehwere in this method i'm not kicking out of one of my loops for it to continue to check the string for any more adjacent pairs
the first test case passes while its an empty string "" = because it returned false
the second test case passes while its "a" = because it returned false
We are also not allowed to use Arrays :(
public boolean hasAdjacentPair(String str)
{
boolean result = false;
if (str.length() == 0)
{
result = false;
}
if (str.length() == 1)
{
result = false;
}
while (str.length() != 0)
{
for (int i = 0; i < str.length() - 1; ++i)
{
char adjChar = str.charAt(i);
char nextAdjChar = str.charAt(i + 1);
if (adjChar == nextAdjChar)
{
result = true;
}
}
}
return result;
}
Changed my while loop while (str.length() != 0) to while (str.length() != 0 && str.length() != 1) this enabled test 2 to work
EDIT 2 : After i completely took out the while (str.length() != 0) All 5 of my test cases pass :) so i guess it was just that?
while (str.length() != 0)
is always true and loop never end. Instead of if..if..while structure use either switch on length of string or if-else.
You could try something like this
if (str.length() == 0)
{
result = false;
}
else if (str.length() == 1)
{
result = false;
}
else
{
for (int i = 0; i < str.length() - 1; ++i)
{
char adjChar = str.charAt(i);
char nextAdjChar = str.charAt(i + 1);
if (adjChar == nextAdjChar)
{
result = true;
break;
}
}
If you checking for atleast one pair make use of break statement as you already know that you got the result
try this alternative:
boolean c = false;
//Your other code
while (str.length() != 0 && c == false)
{
for (int i = 0; i < str.length() - 1; ++i)
{
char adjChar = str.charAt(i);
char nextAdjChar = str.charAt(i + 1);
if (adjChar == nextAdjChar)
{
result = true;
}
}
c = true;
}
//Your other code
Is there a reason you have a while loop checking for the str length? The str is not modified in the loop.
What you most likely want is an if, else if, else structure.
You can most likely return true within the adjChar == nextAdjChar if statement and return false at the end of your function.

Get out of a "for loop" in Java

I would like to get out from a "for loop" when an event happened. For instance there is a part of my code :
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
//exit here
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
//exit here
}
}
}
Actually, I would like to stop the loop where there are the comments "exit here".
Please, can you tell me how can I proceed? Sorry for my english...
break;
This exits out of the for loop entirely.
continue;
This goes on to the next iteration of the loop.
You are looking for the break keyword, this will end the loop immediately:
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
break;
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
break;
}
}
}
Simply use keyword
break;
where you want to stop the loop
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
break;
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
break;
}
}
}
Use:
break;
Please refer to the following links for further information.
1) Control Flow - Wiki
2) "Break" and "Continue" in JAVA
Hope they would help.

Categories

Resources