Java > Array-2 > has12 (CodingBat Solution)

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;
}


22 comments :

  1. boolean hasT = false;

    for(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;

    ReplyDelete
  2. public boolean has12(int[] nums) {

    int i=0;
    int onePos = -1;
    int twoPos=-1;

    for(i=0;ionePos && onePos>=0){
    return true;
    }
    return false;
    }

    ReplyDelete
  3. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
    Replies
    1. this is the correct one, the others are wrong

      Delete
  4. public boolean has12(int[] nums) {

    for(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;
    }

    ReplyDelete
  5. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
  6. return Arrays.toString(nums).matches("(.*)1(.*)2(.*)");

    ReplyDelete
  7. public boolean has12(int[] nums) {

    for (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;
    }

    ReplyDelete
  8. public boolean has12(int[] nums)
    {
    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;
    }

    ReplyDelete
  9. public boolean has12(int[] nums) {
    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;
    }

    ReplyDelete
    Replies
    1. I had something very similar.

      public 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;
      }

      Delete
  10. public boolean has12(int[] nums) {
    int oneIndex=0;
    int c1 =0;
    int twoIndex=0;
    int c2=0;

    for(int i=0; i0 && c2>0 && oneIndex<twoIndex) ;
    }

    ReplyDelete
  11. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
  12. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
  13. public boolean has12(int[] nums) {
    for (int i = 0; i < nums.length-1; i++){
    if ((nums[i] == 1 && nums[nums.length-1] ==2))
    return true;

    }
    return false;
    }

    ReplyDelete
  14. public boolean has12(int[] nums)
    {
    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;
    }

    ReplyDelete
  15. public boolean has12(int[] nums) {

    int 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;
    }

    ReplyDelete
  16. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
  17. public boolean has12(int[] nums) {
    boolean a = false;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 1 && nums[nums.length - 1] == 2) {
    a = true;
    }
    }return a;
    }

    ReplyDelete
  18. public boolean has12(int[] nums) {
    boolean 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;
    }

    ReplyDelete
  19. With Java Stream

    public 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;
    }

    ReplyDelete
  20. public boolean has12 (int[]nums)
    {
    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);

    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact