Problem:
Given an array of ints, return true if there is a 1 in the array with a 2 somewhere later in the array.
has12({1, 3, 2}) → true
has12({3, 1, 2}) → true
has12({3, 1, 4, 5, 2}) → true
Solution:
public boolean has12(int[] nums) { boolean foundOne = false; boolean foundOneTwo = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) foundOne = true; if (nums[i] == 2 && foundOne) foundOneTwo = true; } return foundOneTwo; }
boolean hasT = false;
ReplyDeletefor(int i=0; i<nums.length-1; i++){
if(nums[i] == 1){
for(int j = i+1; j<nums.length; j++){
if(nums[j] == 2) return true;
}
}
}
return false;
public boolean has12(int[] nums) {
ReplyDeleteint i=0;
int onePos = -1;
int twoPos=-1;
for(i=0;ionePos && onePos>=0){
return true;
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean result = false;
boolean found1 = false;
boolean found2 = false;
int search1 = 0;
int search2 = 0;
for(int i=0; i<nums.length; i++){
if(nums[i] == 1){
found1 = true;
search1 = i;
}
}
for(int j=0; j<nums.length; j++){
if(nums[j] == 2){
found2 = true;
search2 = j;
}
}
if(search1<search2 && found1 && found2)
result = true;
return result;
}
this is the correct one, the others are wrong
Deletepublic boolean has12(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length; i++){
if(nums[i]==1){
int[] arr= Arrays.copyOfRange(nums,i+1,nums.length);
for(int j=0; j<arr.length; j++){
if(arr[j]==2)
return true;
}
}
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean flag = false;
for(int i=0;i<nums.length;i++){
if(nums[i]==1){
flag = true;
}
if(flag && nums[i]==2){
return true;
}
}
return false;
}
return Arrays.toString(nums).matches("(.*)1(.*)2(.*)");
ReplyDeletepublic boolean has12(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i] == 1)
while ( i < nums.length - 1) {
i++;
if (nums[i] == 2) return true;
}
}
return false;
}
public boolean has12(int[] nums)
ReplyDelete{
boolean b=false;
boolean c= false;
int k=2;
int l=nums.length;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(nums[i]==1)
{
b=true;
}
if(nums[j]==2)
{
c=true;
}
}
}
if(b==true&&c==true)
{
return true;
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i]==1) {
for (int j = i; j < nums.length; j++) {
if (nums[j]==2) {
return true;
}
}
}
}return false;
}
I had something very similar.
Deletepublic boolean has12(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
for (int j = i; j < nums.length; ++j) {
if (nums[i] == 1 && nums[j] == 2) return true;
}
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteint oneIndex=0;
int c1 =0;
int twoIndex=0;
int c2=0;
for(int i=0; i0 && c2>0 && oneIndex<twoIndex) ;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean flag1 = false, flag2 = false;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1) flag1 = true;
if(nums[i] == 2 && flag1) flag2 = true;
if(flag1 && flag2) return true;
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean one=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==1)
one=true;
if(nums[i]==2 && one==true)
return true;
}return false;
}
public boolean has12(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length-1; i++){
if ((nums[i] == 1 && nums[nums.length-1] ==2))
return true;
}
return false;
}
public boolean has12(int[] nums)
ReplyDelete{
for(int i = 0; i < nums.length; i++)
{
if(nums[i] == 1)
{
for(int j = i; j < nums.length; j++)
{
if(nums[j] == 2) return true;
}
}
}
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteint one = 0;
int two = 0;
for(int i = 0; i < nums.length; i++)
{
if(nums[i] == 1)
{
for(int j = i; j < nums.length; j++)
{
if(nums[j] == 2)
two++;
}
}
one++;
}
if(one >= 1 && two >= 1)
return true;
return false;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean x=false;
for(int i = 0,j=nums.length;i<nums.length;i++)
{
if(nums[i]==1)
j=i;
if(j< i&& nums[i]==2)
x = true;
}
return x;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean a = false;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 1 && nums[nums.length - 1] == 2) {
a = true;
}
}return a;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean x = false;
for (int i = 0; i < nums.length && x == false; i++) {
if (nums[i] == 1) {
for (int j = 0; j < nums.length - i; j++) {
if (nums[i + j] == 2) {
x = true;
}
}
}
}
return x;
}
With Java Stream
ReplyDeletepublic boolean has12(int[] nums) {
boolean result = java.util.stream.IntStream.range(0, nums.length-1)
.anyMatch(i -> nums[i] == 1 && nums[nums.length-1] == 2);
return result;
}
public boolean has12 (int[]nums)
ReplyDelete{
boolean one = false;
boolean two = false;
for (int i = 0, j=nums.length-1; i < nums.length; i++,j--)
{
if (nums[i] == 1)
one = true;
if (j>i && nums[j] == 2)
two = true;
}
return (one==true && two==true);
}
public boolean has12(int[] nums) {
ReplyDeleteint index_two = 0;
int index_one = nums.length;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 2) {
index_two = i;
}
if (nums[i] == 1) {
index_one = i;
}
}
return index_one < index_two;
}