check if the numbers in the array between 0-array.length -1
up vote
0
down vote
favorite
I need to write a program that will check that all the numbers inside the array will be between 0
to array.length-1
, and will occur just once, returning true or false.
For example [0,1,3,2]
will return True, and [4,3.0,1]
and [0,1,2,2]
will both return False.
I've try to write it :
public static boolean isPermutation (int array)
boolean isPermutation =true ;
for (int i = 0 ; i<array.length & isPermutation; i = i+1)
for (int j = 1 ; j<array.length & isPermutation; j = j+1)
if (array[i]==array[j]
return isPermutation ;
the problem is that when we check array[i]==array[j]
its equal when i
equals j
and not the numbers in the array.
Can anyone help please?
java
|
show 2 more comments
up vote
0
down vote
favorite
I need to write a program that will check that all the numbers inside the array will be between 0
to array.length-1
, and will occur just once, returning true or false.
For example [0,1,3,2]
will return True, and [4,3.0,1]
and [0,1,2,2]
will both return False.
I've try to write it :
public static boolean isPermutation (int array)
boolean isPermutation =true ;
for (int i = 0 ; i<array.length & isPermutation; i = i+1)
for (int j = 1 ; j<array.length & isPermutation; j = j+1)
if (array[i]==array[j]
return isPermutation ;
the problem is that when we check array[i]==array[j]
its equal when i
equals j
and not the numbers in the array.
Can anyone help please?
java
Why is[4,3.0,1]
false? Also its not possible given that your array isint
– Nicholas K
Nov 10 at 10:53
@NicholasK 4 is greater thanarray.length-1
(dot was most likely a typo).
– Pshemo
Nov 10 at 10:54
for (int j = 1
should probably befor (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.
– Pshemo
Nov 10 at 11:01
Copy the array, sort the copy, then iterate thru sorted copy checking thatsortedCopy[i]==i
for alli: 0<= i < sortedCopy.length
.
– Kevin Anderson
Nov 10 at 11:17
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to write a program that will check that all the numbers inside the array will be between 0
to array.length-1
, and will occur just once, returning true or false.
For example [0,1,3,2]
will return True, and [4,3.0,1]
and [0,1,2,2]
will both return False.
I've try to write it :
public static boolean isPermutation (int array)
boolean isPermutation =true ;
for (int i = 0 ; i<array.length & isPermutation; i = i+1)
for (int j = 1 ; j<array.length & isPermutation; j = j+1)
if (array[i]==array[j]
return isPermutation ;
the problem is that when we check array[i]==array[j]
its equal when i
equals j
and not the numbers in the array.
Can anyone help please?
java
I need to write a program that will check that all the numbers inside the array will be between 0
to array.length-1
, and will occur just once, returning true or false.
For example [0,1,3,2]
will return True, and [4,3.0,1]
and [0,1,2,2]
will both return False.
I've try to write it :
public static boolean isPermutation (int array)
boolean isPermutation =true ;
for (int i = 0 ; i<array.length & isPermutation; i = i+1)
for (int j = 1 ; j<array.length & isPermutation; j = j+1)
if (array[i]==array[j]
return isPermutation ;
the problem is that when we check array[i]==array[j]
its equal when i
equals j
and not the numbers in the array.
Can anyone help please?
java
java
edited Nov 10 at 15:45
Lee Mac
3,16021138
3,16021138
asked Nov 10 at 10:50
Elad Solomon
93
93
Why is[4,3.0,1]
false? Also its not possible given that your array isint
– Nicholas K
Nov 10 at 10:53
@NicholasK 4 is greater thanarray.length-1
(dot was most likely a typo).
– Pshemo
Nov 10 at 10:54
for (int j = 1
should probably befor (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.
– Pshemo
Nov 10 at 11:01
Copy the array, sort the copy, then iterate thru sorted copy checking thatsortedCopy[i]==i
for alli: 0<= i < sortedCopy.length
.
– Kevin Anderson
Nov 10 at 11:17
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29
|
show 2 more comments
Why is[4,3.0,1]
false? Also its not possible given that your array isint
– Nicholas K
Nov 10 at 10:53
@NicholasK 4 is greater thanarray.length-1
(dot was most likely a typo).
– Pshemo
Nov 10 at 10:54
for (int j = 1
should probably befor (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.
– Pshemo
Nov 10 at 11:01
Copy the array, sort the copy, then iterate thru sorted copy checking thatsortedCopy[i]==i
for alli: 0<= i < sortedCopy.length
.
– Kevin Anderson
Nov 10 at 11:17
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29
Why is
[4,3.0,1]
false? Also its not possible given that your array is int
– Nicholas K
Nov 10 at 10:53
Why is
[4,3.0,1]
false? Also its not possible given that your array is int
– Nicholas K
Nov 10 at 10:53
@NicholasK 4 is greater than
array.length-1
(dot was most likely a typo).– Pshemo
Nov 10 at 10:54
@NicholasK 4 is greater than
array.length-1
(dot was most likely a typo).– Pshemo
Nov 10 at 10:54
for (int j = 1
should probably be for (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.– Pshemo
Nov 10 at 11:01
for (int j = 1
should probably be for (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.– Pshemo
Nov 10 at 11:01
Copy the array, sort the copy, then iterate thru sorted copy checking that
sortedCopy[i]==i
for all i: 0<= i < sortedCopy.length
.– Kevin Anderson
Nov 10 at 11:17
Copy the array, sort the copy, then iterate thru sorted copy checking that
sortedCopy[i]==i
for all i: 0<= i < sortedCopy.length
.– Kevin Anderson
Nov 10 at 11:17
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can avoid for loop within a for loop and can use mathematics to your advantage and keep adding all the numbers and finally check if the actual sum is equal to the intended sum then return true else false. If all the numbers are withing range and exactly appear once, only then their sum will be equal to the sum of all numbers 1 to N. Meanwhile while scanning the numbers in array, if you encounter any number greater than array length - 1 or less than zero, you can immediately return false.
Here is the kind of code that may help.
public static boolean areNumbersInclusive(int arr)
long sum = 0;
for (int n : arr)
if (n > arr.length - 1
long intendedSum = ((arr.length - 1) * arr.length) / 2; // sum from 1 to n is n*(n+1)/2
return intendedSum == sum;
public static void main(String args)
int arr1 = 1,0,5,3,2,4;
int arr2 = 1,0,3,4;
int arr3 = -1,0,3,2;
int arr4 = 1,0,3,2;
System.out.println(areNumbersInclusive(arr1));
System.out.println(areNumbersInclusive(arr2));
System.out.println(areNumbersInclusive(arr3));
System.out.println(areNumbersInclusive(arr4));
This prints following output as expected.
true
false
false
true
Here is OP's correct version of method, although nested for loop can be avoided with my answer.
public static boolean isPermutation(int array)
for (int i = 0; i < array.length; i++) array[i] < 0)
return false;
for (int j = i + 1; j < array.length; j++)
if (array[i] == array[j])
return false;
return true;
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
add a comment |
up vote
0
down vote
By using a Set
you can easily check if there are the same numbers inside the array, because a set stores only 1 occurrence of each number and then in one loop you check if all the numbers are in the range 0..array.length-1:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
for (int x : array) (x > length - 1))
return false;
return true;
public static void main(String args)
Integer a = 0, 1, 3, 2;
Integer b = 0, 1, 3, 1;
Integer c = 0, 1, 3, -1;
System.out.println(isPermutation(a));
System.out.println(isPermutation(b));
System.out.println(isPermutation(c));
will print:
true
false
false
Or even better avoid the loop by filtering the Set
to a List
with only the valid values and check the list's size if it is equal to the array's length:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
List<Integer> list = set.stream().filter(x -> x >= 0 && x < length).collect(Collectors.toList());
return list.size() == length;
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can avoid for loop within a for loop and can use mathematics to your advantage and keep adding all the numbers and finally check if the actual sum is equal to the intended sum then return true else false. If all the numbers are withing range and exactly appear once, only then their sum will be equal to the sum of all numbers 1 to N. Meanwhile while scanning the numbers in array, if you encounter any number greater than array length - 1 or less than zero, you can immediately return false.
Here is the kind of code that may help.
public static boolean areNumbersInclusive(int arr)
long sum = 0;
for (int n : arr)
if (n > arr.length - 1
long intendedSum = ((arr.length - 1) * arr.length) / 2; // sum from 1 to n is n*(n+1)/2
return intendedSum == sum;
public static void main(String args)
int arr1 = 1,0,5,3,2,4;
int arr2 = 1,0,3,4;
int arr3 = -1,0,3,2;
int arr4 = 1,0,3,2;
System.out.println(areNumbersInclusive(arr1));
System.out.println(areNumbersInclusive(arr2));
System.out.println(areNumbersInclusive(arr3));
System.out.println(areNumbersInclusive(arr4));
This prints following output as expected.
true
false
false
true
Here is OP's correct version of method, although nested for loop can be avoided with my answer.
public static boolean isPermutation(int array)
for (int i = 0; i < array.length; i++) array[i] < 0)
return false;
for (int j = i + 1; j < array.length; j++)
if (array[i] == array[j])
return false;
return true;
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
add a comment |
up vote
1
down vote
accepted
You can avoid for loop within a for loop and can use mathematics to your advantage and keep adding all the numbers and finally check if the actual sum is equal to the intended sum then return true else false. If all the numbers are withing range and exactly appear once, only then their sum will be equal to the sum of all numbers 1 to N. Meanwhile while scanning the numbers in array, if you encounter any number greater than array length - 1 or less than zero, you can immediately return false.
Here is the kind of code that may help.
public static boolean areNumbersInclusive(int arr)
long sum = 0;
for (int n : arr)
if (n > arr.length - 1
long intendedSum = ((arr.length - 1) * arr.length) / 2; // sum from 1 to n is n*(n+1)/2
return intendedSum == sum;
public static void main(String args)
int arr1 = 1,0,5,3,2,4;
int arr2 = 1,0,3,4;
int arr3 = -1,0,3,2;
int arr4 = 1,0,3,2;
System.out.println(areNumbersInclusive(arr1));
System.out.println(areNumbersInclusive(arr2));
System.out.println(areNumbersInclusive(arr3));
System.out.println(areNumbersInclusive(arr4));
This prints following output as expected.
true
false
false
true
Here is OP's correct version of method, although nested for loop can be avoided with my answer.
public static boolean isPermutation(int array)
for (int i = 0; i < array.length; i++) array[i] < 0)
return false;
for (int j = i + 1; j < array.length; j++)
if (array[i] == array[j])
return false;
return true;
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can avoid for loop within a for loop and can use mathematics to your advantage and keep adding all the numbers and finally check if the actual sum is equal to the intended sum then return true else false. If all the numbers are withing range and exactly appear once, only then their sum will be equal to the sum of all numbers 1 to N. Meanwhile while scanning the numbers in array, if you encounter any number greater than array length - 1 or less than zero, you can immediately return false.
Here is the kind of code that may help.
public static boolean areNumbersInclusive(int arr)
long sum = 0;
for (int n : arr)
if (n > arr.length - 1
long intendedSum = ((arr.length - 1) * arr.length) / 2; // sum from 1 to n is n*(n+1)/2
return intendedSum == sum;
public static void main(String args)
int arr1 = 1,0,5,3,2,4;
int arr2 = 1,0,3,4;
int arr3 = -1,0,3,2;
int arr4 = 1,0,3,2;
System.out.println(areNumbersInclusive(arr1));
System.out.println(areNumbersInclusive(arr2));
System.out.println(areNumbersInclusive(arr3));
System.out.println(areNumbersInclusive(arr4));
This prints following output as expected.
true
false
false
true
Here is OP's correct version of method, although nested for loop can be avoided with my answer.
public static boolean isPermutation(int array)
for (int i = 0; i < array.length; i++) array[i] < 0)
return false;
for (int j = i + 1; j < array.length; j++)
if (array[i] == array[j])
return false;
return true;
You can avoid for loop within a for loop and can use mathematics to your advantage and keep adding all the numbers and finally check if the actual sum is equal to the intended sum then return true else false. If all the numbers are withing range and exactly appear once, only then their sum will be equal to the sum of all numbers 1 to N. Meanwhile while scanning the numbers in array, if you encounter any number greater than array length - 1 or less than zero, you can immediately return false.
Here is the kind of code that may help.
public static boolean areNumbersInclusive(int arr)
long sum = 0;
for (int n : arr)
if (n > arr.length - 1
long intendedSum = ((arr.length - 1) * arr.length) / 2; // sum from 1 to n is n*(n+1)/2
return intendedSum == sum;
public static void main(String args)
int arr1 = 1,0,5,3,2,4;
int arr2 = 1,0,3,4;
int arr3 = -1,0,3,2;
int arr4 = 1,0,3,2;
System.out.println(areNumbersInclusive(arr1));
System.out.println(areNumbersInclusive(arr2));
System.out.println(areNumbersInclusive(arr3));
System.out.println(areNumbersInclusive(arr4));
This prints following output as expected.
true
false
false
true
Here is OP's correct version of method, although nested for loop can be avoided with my answer.
public static boolean isPermutation(int array)
for (int i = 0; i < array.length; i++) array[i] < 0)
return false;
for (int j = i + 1; j < array.length; j++)
if (array[i] == array[j])
return false;
return true;
edited Nov 10 at 11:42
answered Nov 10 at 11:04
Pushpesh Kumar Rajwanshi
3,9531825
3,9531825
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
add a comment |
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
thank you . but can u tell me what he problem with the code that i wrote ?
– Elad Solomon
Nov 10 at 11:14
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
One problem I see you didn't use OR properly. You need to write || instead of | when doing OR in comparison. You should write if (array[i]==array[j] || (array[i]>=array.length)) instead of if (array[i]==array[j]|(array[i]>=array.length)) Single | is called bitwise OR operation. Same goes for & that you have written. You need to write && for And operation and there could be more logical issues. Let me check your method in detail.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:16
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
ok thank i will try.. the broblem that i do debug i see that inside the loop it check if iand j equal and not the number inside the array.. its doesnt check if array[i]==array[j]
– Elad Solomon
Nov 10 at 11:23
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
@EladSolomon: As I checked, there were multiple problems in your code. Main problem was, you were comparing array[i]==array[j] and at point where i and j were equal, it use to set isPermutation = false due to which it always returned false. I have edited my answer to include the correct version of your method. You can have a look of what all changes I have made there.
– Pushpesh Kumar Rajwanshi
Nov 10 at 11:41
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
Glad to help :)
– Pushpesh Kumar Rajwanshi
Nov 10 at 12:22
add a comment |
up vote
0
down vote
By using a Set
you can easily check if there are the same numbers inside the array, because a set stores only 1 occurrence of each number and then in one loop you check if all the numbers are in the range 0..array.length-1:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
for (int x : array) (x > length - 1))
return false;
return true;
public static void main(String args)
Integer a = 0, 1, 3, 2;
Integer b = 0, 1, 3, 1;
Integer c = 0, 1, 3, -1;
System.out.println(isPermutation(a));
System.out.println(isPermutation(b));
System.out.println(isPermutation(c));
will print:
true
false
false
Or even better avoid the loop by filtering the Set
to a List
with only the valid values and check the list's size if it is equal to the array's length:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
List<Integer> list = set.stream().filter(x -> x >= 0 && x < length).collect(Collectors.toList());
return list.size() == length;
add a comment |
up vote
0
down vote
By using a Set
you can easily check if there are the same numbers inside the array, because a set stores only 1 occurrence of each number and then in one loop you check if all the numbers are in the range 0..array.length-1:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
for (int x : array) (x > length - 1))
return false;
return true;
public static void main(String args)
Integer a = 0, 1, 3, 2;
Integer b = 0, 1, 3, 1;
Integer c = 0, 1, 3, -1;
System.out.println(isPermutation(a));
System.out.println(isPermutation(b));
System.out.println(isPermutation(c));
will print:
true
false
false
Or even better avoid the loop by filtering the Set
to a List
with only the valid values and check the list's size if it is equal to the array's length:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
List<Integer> list = set.stream().filter(x -> x >= 0 && x < length).collect(Collectors.toList());
return list.size() == length;
add a comment |
up vote
0
down vote
up vote
0
down vote
By using a Set
you can easily check if there are the same numbers inside the array, because a set stores only 1 occurrence of each number and then in one loop you check if all the numbers are in the range 0..array.length-1:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
for (int x : array) (x > length - 1))
return false;
return true;
public static void main(String args)
Integer a = 0, 1, 3, 2;
Integer b = 0, 1, 3, 1;
Integer c = 0, 1, 3, -1;
System.out.println(isPermutation(a));
System.out.println(isPermutation(b));
System.out.println(isPermutation(c));
will print:
true
false
false
Or even better avoid the loop by filtering the Set
to a List
with only the valid values and check the list's size if it is equal to the array's length:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
List<Integer> list = set.stream().filter(x -> x >= 0 && x < length).collect(Collectors.toList());
return list.size() == length;
By using a Set
you can easily check if there are the same numbers inside the array, because a set stores only 1 occurrence of each number and then in one loop you check if all the numbers are in the range 0..array.length-1:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
for (int x : array) (x > length - 1))
return false;
return true;
public static void main(String args)
Integer a = 0, 1, 3, 2;
Integer b = 0, 1, 3, 1;
Integer c = 0, 1, 3, -1;
System.out.println(isPermutation(a));
System.out.println(isPermutation(b));
System.out.println(isPermutation(c));
will print:
true
false
false
Or even better avoid the loop by filtering the Set
to a List
with only the valid values and check the list's size if it is equal to the array's length:
public static boolean isPermutation (Integer array)
int length = array.length;
Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
if (set.size() < length)
return false;
List<Integer> list = set.stream().filter(x -> x >= 0 && x < length).collect(Collectors.toList());
return list.size() == length;
edited Nov 10 at 11:44
answered Nov 10 at 11:21
forpas
5,4981217
5,4981217
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238197%2fcheck-if-the-numbers-in-the-array-between-0-array-length-1%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why is
[4,3.0,1]
false? Also its not possible given that your array isint
– Nicholas K
Nov 10 at 10:53
@NicholasK 4 is greater than
array.length-1
(dot was most likely a typo).– Pshemo
Nov 10 at 10:54
for (int j = 1
should probably befor (int j = i+ 1
to: avoid situation where i==j; also there is no need to compare same pair twice (a==b) and later (b==a). BTW you can use Sets to check if elements are unique.– Pshemo
Nov 10 at 11:01
Copy the array, sort the copy, then iterate thru sorted copy checking that
sortedCopy[i]==i
for alli: 0<= i < sortedCopy.length
.– Kevin Anderson
Nov 10 at 11:17
you need to check the condition for element >0 , i.e if (array[i]==array[j]||(array[i]>=array.length) || array[i] < 0) and the inner loop you can have for(int j = i+1; j<array.length && isPermutation; j++ ) . check the last number is within 0 and array.length -1. you problem would be done.
– Hrudayanath
Nov 10 at 11:29