Search This Blog

Saturday, August 17, 2013

Coding Bat Java Array-1 solutions 15 to 27


This post contains the solutions to Java Array-1 section of codingbat.com for problems 15 to 27. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2, java-string1. Solutions to problems 1 to 14 are available in java-array-1.


15.       Problem Name : double23 
Solution:
Hint: Given an int array, return true if the array contains 2 twice, or 3 twice
Constraint: The array will be length 0, 1, or 2. .

       public boolean double23(int[] nums)
       {
                if(nums.length>1)
                {
                       return((nums[0]==2 && nums[1]==2)||(nums[0]==3&&nums[1]==3));
                }
                else
                       return false;
       }


16.       Problem Name : fix23 
Solution:
Hint: Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array. 
       public int[] fix23(int[] nums)
       {
                for(int i=0;i<nums.length-1;i++)
                {
                       if(nums[i]==2 && nums[i+1]==3)
                       {
                       nums[i+1]=0;
                       break;
                       }
                }
                return nums;        
       }


17.       Problem Name : start1 
Solution:
Hint: Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element. 
       public int start1(int[] a, int[] b)
       {     
              int count=0;
                if(a.length>=1)
                {
                 if(a[0]==1)
                        count++;
                }
                if(b.length>=1)
                {
                       if(b[0]==1)
                             count++;
                }
                return count;
       }
18.       Problem Name : biggerTwo 
Solution:
Hint: Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum
Constraint:  In event of a tie, return a. 
       public int[] biggerTwo(int[] a, int[] b)
       {
                if((a[0]+a[1])>=b[0]+b[1])
                       return a;
                else
                       return b;
       }

19.       Problem Name : makeMiddle 
Solution:
Hint: Given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array.
Constraint: The original array will be length 2 or more. 
       public int[] makeMiddle(int[] nums)
       {
              int[] a = new int[2];
                if(nums.length>1)
                {
                       a[1]=nums[nums.length/2];
                       a[0]=nums[nums.length/2-1];
               
                       return a;
                }
                else
                       return nums;
       }


20.       Problem Name : plusTwo 
Solution:
Hint: Given 2 int arrays, each length 2, return a new array length 4 containing all their elements. 
       public int[] plusTwo(int[] a, int[] b)
       {
                int[] c = new int[a.length+b.length] ;
               
                c[0]=a[0];
                c[1]=a[1];
               
                c[2]=b[0];
                c[3]=b[1];
                      
       return c;
       }


21.       Problem Name : swapEnds 
Solution:
Hint: Given an array of ints, swap the first and last elements in the array. Return the modified array.
Constraint: The array length will be at least 1.
       public int[] swapEnds(int[] nums)
       {
                int tmp;
               
                tmp=nums[0];
                nums[0]=nums[nums.length-1];
                nums[nums.length-1]=tmp; 
               
                return nums;
       }
22.       Problem Name : midThree 
Solution:
Hint: Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
Constraint: The array length will be at least 3. 
       public int[] midThree(int[] nums)
       {
                int[] a = new int[3];
                int middle=nums.length/2;
                a[0]=nums[middle-1];
                a[1]=nums[middle];
                a[2]=nums[middle+1];
             
                return a;
                
       }

23.       Problem Name : maxTriple 
Solution:
Hint: Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest
       public int maxTriple(int[] nums)
       {
              int max=0;
                if(nums.length>1)
                {
                      max=nums[0]>nums[nums.length-1]?nums[0]:nums[nums.length-1];
                      max=max>nums[nums.length/2]?max:nums[nums.length/2];
                }
                return max;
       }

24.       Problem Name : frontPiece 
Solution:
Hint: Given an int array of any length, return a new array of its first 2 elements. If the array is smaller than length 2, use whatever elements are present. 
       public int[] frontPiece(int[] nums)
       {
                if(nums.length>1)
                {
                  int[] a = new int[2];
                     a[0]=nums[0];a[1]=nums[1];
                
                     return a;
                }
                else if(nums.length==1)
                {
                     int[] b = new int[1];
                     b[0]=nums[0];
               
                     return b;
                }
                else
                {
                      int[] c=new int[0];
                    
                      return c;
                }
       }

25.       Problem Name : unlucky1
Solution:
Hint: We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array. 
       public boolean unlucky1(int[] nums)
       {
                int i=0;
                if(nums.length>2)
                {
               
                       if(
                                    (nums[0]==1 && nums[1]==3)||
                                    (nums[1]==1 && nums[2]==3)||
                                    (nums[nums.length-1]==3 &&nums[nums.length-2]==1)||
                                    (nums[nums.length-2]==1 && nums[nums.length-3]==3)
                           )
                             return true;
                       else
                             return false;
                }
                else if(nums.length==2)
                {
                       if(nums[0]==1 && nums[1]==3)
                             return true;
                       else
                             return false;
                }
                else

                return false;
       }

26.       Problem Name : make2 
Solution:
Hint: Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b.
Constraint: The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays. 
public int[] make2(int[] a, int[] b)
       {
              int [] empty=new int[0];
              int i=0;
              if(a.length!=0 && b.length!=0)
              {
                 if((a.length<b.length) &&(a.length!=0 && b.length!=0))
                  {
                   int[] c=new int[2];
                   for(i=0;i<a.length;i++)
                    {
                     c[i]=a[i];
                    }
                    if(c[1]==0)
                       c[1]=b[0];
                 return c;
                  }
                  else
                  {
                   int[] arr1=new int[2];
                    for(i=0;i<b.length;i++)
                    {
                    arr1[i]=a[i];
                    }
                    if(arr1[1]==0)
                       arr1[1]=b[0];
                    return arr1;
                  }
              }
              else
              {
                     if(a.length==0)
                     return b;
                     if(b.length==0)
                     {
                           int[] arr2=new int[2];
                           for(i=0;i<2;i++)
                                  arr2[i]=a[i];

                           return arr2;
                     }
              return empty;
              }
       }

27.       Problem Name : front11 
Solution:
Hint: Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array. 
       public int[] front11(int[] a, int[] b)
       {
                int[] twoarray=new int[2];
                int[] onearray=new int[1];
                int[] zeroarray=new int[0];

                if(a.length>0 && b.length>0)
                {
                       twoarray[0]=a[0];
                       twoarray[1]=b[0];
                       return twoarray;
                }
                else
                {
                if(a.length>0)
                       onearray[0]=a[0];
                if(b.length>0)
                       onearray[0]=b[0];
                if(a.length>0 || b.length>0)
                       return onearray;
                else
                       return zeroarray;
                }
                         
       }


Please check my posts for solutions to other sections of codingbat.com.

No comments:

Post a Comment

Labels