Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.
public boolean twoTwo(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==nums[i+1])||(nums[i]==nums[i-1])){
result=true;
}
else{
result=false;
}
}
return result;
}
Expected Run
twoTwo([4, 2, 2, 3]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 4]) → true true OK
twoTwo([2, 2, 4, 2]) → false false OK
twoTwo([1, 3, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([1, 2, 2, 3, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([1, 2, 3, 4]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2]) → true true OK
twoTwo([2, 2, 7]) → true true OK
twoTwo([2, 2, 7, 2, 1]) → false false OK
twoTwo([4, 2, 2, 2]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 2]) → true true OK
twoTwo([1, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2]) → false false OK
twoTwo([1]) → true false X
twoTwo([]) → true false X
twoTwo([5, 2, 2, 3]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 5, 2]) → false false OK
other tests
X
Your progress graph for this problem
public boolean twoTwo(int[] nums) {
boolean result=false;
for(int i=1;i<nums.length-1;i++){
if((nums[i]==nums[i+1])||(nums[i]==nums[i-1])){
result=true;
}
else{
result=false;
}
}
return result;
}
Expected Run
twoTwo([4, 2, 2, 3]) → true true OK
twoTwo([2, 2, 4]) → true true OK
twoTwo([2, 2, 4, 2]) → false false OK
twoTwo([1, 3, 4]) → true false X
twoTwo([1, 2, 2, 3, 4]) → true false X
twoTwo([1, 2, 3, 4]) → false false OK
twoTwo([2, 2]) → true false X
twoTwo([2, 2, 7]) → true true OK
twoTwo([2, 2, 7, 2, 1]) → false false OK
twoTwo([4, 2, 2, 2]) → true true OK
twoTwo([2, 2, 2]) → true true OK
twoTwo([1, 2]) → false false OK
twoTwo([2]) → false false OK
twoTwo([1]) → true false X
twoTwo([]) → true false X
twoTwo([5, 2, 2, 3]) → true true OK
twoTwo([2, 2, 5, 2]) → false false OK
other tests
X
public boolean twoTwo(int[] nums) {
boolean result = true;
int consecutiveTwos = 0;
for(int num: nums){
if(num == 2){ // found a two
if(consecutiveTwos > 0){ // found twos adjacent to each other
consecutiveTwos++;
result = true;
}
else{ // found a single two
consecutiveTwos = 1;
result = false;
}
}
else{ // found a number that was not a two
if(consecutiveTwos == 1){ // there was a single two
result = false;
break; // break out of for loop
}
else{
consecutiveTwos = 0; // reset variable
}
}
}
return result;
}
twoTwo([1, 3, 4]) → true false X
Given an array of ints, return true if every 2 that appears in the array is next to another 2.
twoTwo([4, 2, 2, 3]) → true
twoTwo([2, 2, 4]) → true
twoTwo([2, 2, 4, 2]) → false
Given an array of ints, return true if every 2 that appears in the array is next to another 2.
or as challenge did not talk about below i should just blindly assume it is always true?Yes, I think that is what they had in mind. That is what the tests confirm.
twoTwo([1, 3, 4])]
public boolean twoTwo(int[] nums) {
boolean result=false;
for(int i=1;i<nums.length-1;i++){
if( (nums[i]==2) && (nums[i]==nums[i+1])||(nums[i]==nums[i-1])){
result=true;
}
else{
result=false;
}
}
return result;
}
Expected Run
twoTwo([4, 2, 2, 3]) → true true OK
twoTwo([2, 2, 4]) → true true OK
twoTwo([2, 2, 4, 2]) → false false OK
twoTwo([1, 3, 4]) → true false X
twoTwo([1, 2, 2, 3, 4]) → true false X
twoTwo([1, 2, 3, 4]) → false false OK
twoTwo([2, 2]) → true false X
twoTwo([2, 2, 7]) → true true OK
twoTwo([2, 2, 7, 2, 1]) → false false OK
twoTwo([4, 2, 2, 2]) → true true OK
twoTwo([2, 2, 2]) → true true OK
twoTwo([1, 2]) → false false OK
twoTwo([2]) → false false OK
twoTwo([1]) → true false X
twoTwo([]) → true false X
twoTwo([5, 2, 2, 3]) → true true OK
twoTwo([2, 2, 5, 2]) → false false OK
other tests
X
public boolean twoTwo(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length-1;i++){
if( (nums[i]==2) && (nums[i]==nums[i+1])||(nums[i]==nums[i-1])){
result=true;
}
else{
result=false;
}
}
return result;
}
Expected Run
twoTwo([4, 2, 2, 3]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 4]) → true true OK
twoTwo([2, 2, 4, 2]) → false false OK
twoTwo([1, 3, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([1, 2, 2, 3, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([1, 2, 3, 4]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2]) → true true OK
twoTwo([2, 2, 7]) → true true OK
twoTwo([2, 2, 7, 2, 1]) → false false OK
twoTwo([4, 2, 2, 2]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 2]) → true true OK
twoTwo([1, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2]) → false false OK
twoTwo([1]) → true false X
twoTwo([]) → true false X
twoTwo([5, 2, 2, 3]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:4) X
twoTwo([2, 2, 5, 2]) → false false OK
other tests
X
Your progress graph for this problem
int consecutiveTwos = 0;
for(int num: nums){
if(num == 2){ // found a two
if(consecutiveTwos > 0){ // found twos adjacent to each other
below is always 0 right and always false as we are initializing to 0 as int consecutiveTwos = 0;?
consecutiveTwos = 1;
On a subsequent pass through the loop, if another two is found then
consecutiveTwos > 0
will be true
public boolean twoTwo(int[] nums) {
boolean result = true;
int consecutiveTwos = 0;
for(int num: nums){
if(num == 2){ // found a two
if(consecutiveTwos == 0){ // found a single two
consecutiveTwos = 1;
result = false;
}
else{ // found twos adjacent to each other
consecutiveTwos++;
result = true;
}
}
else{ // found a number that was not a two
if(consecutiveTwos == 1){ // there was a single two
result = false;
break; // break out of for loop
}
else{
consecutiveTwos = 0; // reset variable
}
}
}
return result;
}
public boolean twoTwo(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
if (i == (nums.length - 1) || nums[++i] != 2) {
return false;
}
while (i < nums.length && nums[i] == 2) {
i++;
}
}
}
return true;
}
public boolean twoTwo(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (accessArray(nums, i) == 2) {
if (accessArray(nums, i - 1) != 2 && accessArray(nums, i + 1) != 2) {
return false;
}
}
}
return true;
}
private int accessArray(int[] arr, int index) {
return index >= 0 && index < arr.length ? arr[index] : 0;
}
We only need to care if we find a 2, if it is at the end of the array or there is no 2 following it, return false. If it was followed by a 2, the "while" loop skips however many 2's are next to each other (so that it caters for 2, 2 and 2, 2, 2, 2, 2 in the same way)
private int accessArray(int[] arr, int index) {
return index >= 0 && index < arr.length ? arr[index] : 0;
}
for (int i = 0; i < nums.length; i++) {
if (accessArray(nums, i) == 2) {
if (accessArray(nums, i - 1) != 2 && accessArray(nums, i + 1) != 2) {
return false;
}
}
}
public boolean twoTwo(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
if (i == (nums.length - 1) || nums[i++] != 2) {
return false;
}
while (i < nums.length && nums[i] == 2) {
i++;
}
}
}
return true;
}
Expected Run
twoTwo([4, 2, 2, 3]) → true true OK
twoTwo([2, 2, 4]) → true true OK
twoTwo([2, 2, 4, 2]) → false false OK
twoTwo([1, 3, 4]) → true true OK
twoTwo([1, 2, 2, 3, 4]) → true true OK
twoTwo([1, 2, 3, 4]) → false true X
twoTwo([2, 2]) → true true OK
twoTwo([2, 2, 7]) → true true OK
twoTwo([2, 2, 7, 2, 1]) → false true X
twoTwo([4, 2, 2, 2]) → true true OK
twoTwo([2, 2, 2]) → true true OK
twoTwo([1, 2]) → false false OK
twoTwo([2]) → false false OK
twoTwo([1]) → true true OK
twoTwo([]) → true true OK
twoTwo([5, 2, 2, 3]) → true true OK
twoTwo([2, 2, 5, 2]) → false false OK
other tests
OK
instead of ++i if i take i++ i am failing 2 test cases as below. i wonder why(just because increment is after assigment?)
int i,j;
i = 3;
j = i++;
System.out.println("i = " + i + " j = " + j);
i = 3;
j = ++i;
System.out.println("i = " + i + " j = " + j);
we only need to find ANY 2 not adjacent to another 2 ... and return falsecompare that to the challenge given by CodingBat
Given an array of ints, return true if every 2 that appears in the array is next to another 2.
I think we should let him decide how he wants to learn.
I personally prefer solutions with code rather than solutions without code which saves so much of time for me and and to experts.I agree. I definitely agree. That is the way I like to learn. I think having a bunch of solutions from a bunch of people is a very valuable resource. Given such a resource, I study each and every approach and try to learn how each contributor thinks. My favorite example is
public boolean twoTwo(int[] nums) {
boolean result=true;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
int count = 0;
for (int k = i; k < nums.length; k++) {
if (nums[k] == 2)
count++;
else
break;
}
i = i+count;
if (count < 2)
result=false;
}
}
return result;
}
public boolean twoTwo(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
if (i == (nums.length - 1) || nums[++i] != 2) {
return false;
}
while (i < nums.length && nums[i] == 2) {
i++;
}
}
}
return true;
}
Select all
Open in new window
Basic description...
We only need to care if we find a 2, if it is at the end of the array or there is no 2 following it, return false. If it was followed by a 2, the "while" loop skips however many 2's are next to each other (so that it caters for 2, 2 and 2, 2, 2, 2, 2 in the same way)
public boolean twoTwo(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (accessArray(nums, i) == 2) {
if (accessArray(nums, i - 1) != 2 && accessArray(nums, i + 1) != 2) {
return false;
}
}
}
return true;
}
private int accessArray(int[] arr, int index) {
return index >= 0 && index < arr.length ? arr[index] : 0;
}
i was not clear on above. please advise
if (accessArray(nums, i) == 2) {//check to see if nums[i] is 2 or not?
if (accessArray(nums, i - 1) != 2 && accessArray(nums, i + 1) != 2) {////check to see if nums[i-1] is not 2 and nums[i+1] os not 2 by calling that method which does some padding?
return false;
}
}
If the value we are looking at (based on i) is a 2 and the value before is NOT a 2 and the value after is NOT a 2, then return false. Or in different words, if the value pointed at by i is a 2 that is surrounded by non-2, return false.
return index >= 0 && index < arr.length ? arr[index] : 0;
if index >= 0 && index < arr.length is true then return arr[index]
otherwise if false then return 0?
pads with how many zero's before and after?
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.