A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
return true if the array contains any of
2, 7, -1
or
2, 7, 0
or
2, 7, 1
or
2, 7, 2
or
2, 7, 3
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j]==7 && nums[j]==1)
return true;
}
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true false X
has271({1, 2, 8, 1}) â†’ false false OK
has271({2, 7, 1}) â†’ true false X
has271({3, 8, 2}) â†’ true false X
has271({2, 7, 3}) â†’ true false X
has271({2, 7, 4}) â†’ false false OK
has271({2, 7, -1}) â†’ true false X
has271({2, 7, -2}) â†’ false false OK
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
has271({2, 7, 5, 10, 1}) â†’ false false OK
has271({2, 7, -2, 4, 10, 2}) â†’ false false OK
public boolean has271(int[] nums) {
for(int i=0;i<xxxxxxxxxxxxx;i++){
if(nums[i+1]==xxxxxxxxx)
if (Math.xxx((numsxxxxx)-nums[i+2])<=x){return true;}
}
return false;
}
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false false OK
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true false X
has271({2, 7, 3}) â†’ true false X
has271({2, 7, 4}) â†’ false false OK
has271({2, 7, -1}) â†’ true false X
has271({2, 7, -2}) â†’ false false OK
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
has271({2, 7, 5, 10, 1}) â†’ false false OK
has271({2, 7, -2, 4, 10, 2}) â†’ false false OK
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
Given an array of ints, return true if it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
has271({1, 2, 7, 1}) â†’ true
has271({1, 2, 8, 1}) â†’ false
has271({2, 7, 1}) â†’ true
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false false OK
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true false X
has271({2, 7, 3}) â†’ true false X
has271({2, 7, 4}) â†’ false false OK
has271({2, 7, -1}) â†’ true false X
has271({2, 7, -2}) â†’ false false OK
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
has271({2, 7, 5, 10, 1}) â†’ false false OK
has271({2, 7, -2, 4, 10, 2}) â†’ false
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
return true;
}
}
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false false OK
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true false X
has271({2, 7, 3}) â†’ true true OK
has271({2, 7, 4}) â†’ false false OK
has271({2, 7, -1}) â†’ true true OK
has271({2, 7, -2}) â†’ false false OK
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
has271({2, 7, 5, 10, 1}) â†’ false false OK
has271({2, 7, -2, 4, 10, 2}) â†’ false false OK
stil failing on 4 test cases. please advise
has271({3, 8, 2}) â†’ true false X
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X
i am failing above four.3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.you mean it should be as below right
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.
1st int = anythingabove is clear
2nd int = 1st int plus 5
If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*has271({3, 8, 2}) â†’ true false X
has271({4, 5, 3, 8, 0}) â†’ true false X
has271({2, 7, 5, 10, 4}) â†’ true false X
has271({2, 7, -2, 4, 9, 3}) â†’ true false X*/
has271({3, 8, 2});
}
public static boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
return true;
}
}
}
return false;
}
}
you mean it should be as below right
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]={1,8,2};
System.out.println("value is--->"+has271(arr));
}
public static boolean has271(int[] nums) {
/** get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[1]==nums[0]+5 && nums[2]==nums[0]-1);
j=j+2;
return true;
}
}
return false;
}
}
i wrote as aboe to check second and third value of the array based on first. I see i am getting true no matter what i pass as belowor is 2 away from the first int-1 IN EITHER DIRECTION.please advise
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false true X
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true true OK
has271({2, 7, 3}) â†’ true true OK
has271({2, 7, 4}) â†’ false true X
has271({2, 7, -1}) â†’ true true OK
has271({2, 7, -2}) â†’ false true X
has271({4, 5, 3, 8, 0}) â†’ true true OK
has271({2, 7, 5, 10, 4}) â†’ true true OK
has271({2, 7, -2, 4, 9, 3}) â†’ true true OK
has271({2, 7, 5, 10, 1}) â†’ false true X
has271({2, 7, -2, 4, 10, 2}) â†’ false true X
i am failing some test cases as above
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]={1,8,2};
System.out.println("value is--->"+has271(arr));
}
public static boolean has271(int[] nums) {
/** get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[1]==nums[0]+5 && (nums[2]==nums[0]-1||nums[2]==nums[0]-2||nums[2]==nums[0]+1||nums[2]==nums[0]+2));
j=j+2;
return true;
}
}
return false;
}
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false true X
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true true OK
has271({2, 7, 3}) â†’ true true OK
has271({2, 7, 4}) â†’ false true X
has271({2, 7, -1}) â†’ true true OK
has271({2, 7, -2}) â†’ false true X
has271({4, 5, 3, 8, 0}) â†’ true true OK
has271({2, 7, 5, 10, 4}) â†’ true true OK
has271({2, 7, -2, 4, 9, 3}) â†’ true true OK
has271({2, 7, 5, 10, 1}) â†’ false true X
has271({2, 7, -2, 4, 10, 2}) â†’ false true X
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
j=j+2;
return true;
}
}
return false;
}
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]== nums[j]-2| |nums[j+2] ==nums[j]+ 1||nums[j+ 2]==nums[j ]+2));
j=j+2;
return true;
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false true X
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true true OK
has271({2, 7, 3}) â†’ true true OK
has271({2, 7, 4}) â†’ false true X
has271({2, 7, -1}) â†’ true true OK
has271({2, 7, -2}) â†’ false true X
has271({4, 5, 3, 8, 0}) â†’ true true OK
has271({2, 7, 5, 10, 4}) â†’ true true OK
has271({2, 7, -2, 4, 9, 3}) â†’ true true OK
has271({2, 7, 5, 10, 1}) â†’ false true X
has271({2, 7, -2, 4, 10, 2}) â†’ false true X
Sorry that i am not able to completely follow on this challenge(since it seems bit stretch for me). Please advise how to fix and improve my code
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
return true;
}
return false;
}
has271({1, 2, 8, 1}) â†’ false true X
has271({2, 7, 5, 10, 1}) â†’ false true X
has271({2, 7, -2, 4, 10, 2}) â†’ false true X
has271({2, 7, 4}) â†’ false true X
is the 3rd int AT MOST 2 different from the 1st int-1? If it is ( return true; );}
is the 3rd int AT MOST 2 different from the 1st int? If it is ( return true; );}
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]-3||nums[j+2]==nums[j]||nums[j+2]==nums[j]+1));
return true;
}
return false;
}
public boolean has271(int[] nums) {
if(nums.length>2){
for(int i=0;i<nums.length-1;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i]-nums[i+2])<=3))
{
return true;
}
Expected Run
has271({1, 2, 7, 1}) â†’ true true OK
has271({1, 2, 8, 1}) â†’ false false OK
has271({2, 7, 1}) â†’ true true OK
has271({3, 8, 2}) â†’ true true OK
has271({2, 7, 3}) â†’ true true OK
has271({2, 7, 4}) â†’ false true X
has271({2, 7, -1}) â†’ true true OK
has271({2, 7, -2}) â†’ false false OK
has271({4, 5, 3, 8, 0}) â†’ true true OK
has271({2, 7, 5, 10, 4}) â†’ true true OK
has271({2, 7, -2, 4, 9, 3}) â†’ true true OK
has271({2, 7, 5, 10, 1}) â†’ false true X
has271({2, 7, -2, 4, 10, 2}) â†’ false false OK
The SECOND int has to be EXACTLY equal to the first int + 5.100% clear
then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.still clear 50%
In this series of ints:100% clear
1,2,32,33,45,766,12,2,7,1
only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :
1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
. . . . so given the same set of ints as I just used above, with one change :
1,2,32,33,45,766,12,2,7,-1
would that series return true or false??
followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.above highlighted one is in 2,7,1 or the one italicized above?
3rd = (1st-1) give or take 2.
public boolean has271(int[] nums) {
if(nums.length>2){
for(int i=0;i<nums.length-2;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i+2]-(nums[i]-1))<=2))
{
return true;
}
}
}
return false;
}
followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
above highlighted one is in 2,7,1 or the one italicized above?
int in the position that the 1 is in
"OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int"
"OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int - 1"
please correct me if i am wrong
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.