Problem-1 Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more. Example firstLast6({1, 2, 6}) → true firstLast6({6, 1, 2, 3}) → true firstLast6({13, 6, 1, 2, 3}) → false
Solution public boolean firstLast6(int[] nums) { return(nums[0]==6 || nums[nums.length-1]==6); }
Problem-2 Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal. Example sameFirstLast({1, 2, 3}) → false sameFirstLast({1, 2, 3, 1}) → true sameFirstLast({1, 2, 1}) → true
Problem-3 Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}. Example: makePi() → {3, 1, 4}
Solution public int[] makePi() { int[] a={3,1,4}; return a; }
Problem-4 Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more. Example: commonEnd({1, 2, 3}, {7, 3}) → true commonEnd({1, 2, 3}, {7, 3, 2}) → false commonEnd({1, 2, 3}, {1, 3}) → true
Solution public boolean commonEnd(int[] a, int[] b) { return (a[0]==b[0] || a[a.length-1]==b[b.length-1]) ; }
Problem-5 Given an array of ints length 3, return the sum of all the elements. Example sum3({1, 2, 3}) → 6 sum3({5, 11, 2}) → 18 sum3({7, 0, 0}) → 7
Solution public int sum3(int[] nums) { int sum=0; for (int i=0; i<nums.length;i++){ sum+=nums[i]; } return sum; }
Problem-6 Given an array of ints length 3, return an array with the elements “rotated left” so {1, 2, 3} yields {2, 3, 1}. Example rotateLeft3({1, 2, 3}) → {2, 3, 1} rotateLeft3({5, 11, 9}) → {11, 9, 5} rotateLeft3({7, 0, 0}) → {0, 0, 7}
Solution public int[] rotateLeft3(int[] nums) { int [] reversed= new int[nums.length]; reversed[nums.length-1]=nums[0]; for (int i=0;i<nums.length-1;i++){ reversed[i]=nums[i+1]; } return reversed; }
Problem-7 Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. Example: reverse3({1, 2, 3}) → {3, 2, 1} reverse3({5, 11, 9}) → {9, 11, 5} reverse3({7, 0, 0}) → {0, 0, 7}
Solution public int[] reverse3(int[] nums) { int [] reversed= new int[nums.length]; for (int i=nums.length-1;i>=0;i–){ reversed[nums.length-1-i]=nums[i]; } return reversed; }
Problem-8 Given an array of ints length 3, figure out which is larger between the first and last elements in the array, and set all the other elements to be that value. Return the changed array. Example maxEnd3({1, 2, 3}) → {3, 3, 3} maxEnd3({11, 5, 9}) → {11, 11, 11} maxEnd3({2, 11, 3}) → {3, 3, 3}
Solution public int[] maxEnd3(int[] nums) { int max=nums[0]; int [] bee=new int[nums.length]; if( nums[0]<nums[nums.length-1]) max=nums[nums.length-1]; else max=max; for(int i=0; i<nums.length; i++){ bee[i]=max; } return bee; }
Problem-9 Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0. Example sum2({1, 2, 3}) → 3 sum2({1, 1}) → 2 sum2({1, 1, 1, 1}) → 2
Solution public int sum2(int[] nums) { int sum=0; if(nums.length==0) sum=sum; else if(nums.length==1) sum=nums[0]; else if(nums.length>1){ for (int i=0;i<2;i++){ sum+=nums[i]; } } return sum; }
Problem-10 Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. Example middleWay({1, 2, 3}, {4, 5, 6}) → {2, 5} middleWay({7, 7, 7}, {3, 8, 0}) → {7, 8} middleWay({5, 2, 9}, {1, 4, 5}) → {2, 4}
Solution public int[] middleWay(int[] a, int[] b) { int[] result={a[1],b[1]}; return result; }
Problem-11 Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more. Example makeEnds({1, 2, 3}) → {1, 3} makeEnds({1, 2, 3, 4}) → {1, 4} makeEnds({7, 4, 6, 2}) → {7, 2}
Problem-12 Given an int array length 2, return true if it contains a 2 or a 3. Example has23({2, 5}) → true has23({4, 3}) → true has23({4, 5}) → false
Solution public boolean has23(int[] nums) { for (int i=0;i<nums.length;i++){ if(nums[i]==2 || nums[i]==3) return true; } return false; }
Problem-13 Given an int array length 2, return true if it does not contain a 2 or 3. Example no23({4, 5}) → true no23({4, 2}) → false no23({3, 5}) → false
Solution public boolean no23(int[] nums) { for (int i=0;i<nums.length;i++){ if(nums[i]==2 || nums[i]==3) return false; } return true; }
Problem-14 Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0’s. Example makeLast({4, 5, 6}) → {0, 0, 0, 0, 0, 6} makeLast({1, 2}) → {0, 0, 0, 2} makeLast({3}) → {0, 3}
Solution public int[] makeLast(int[] nums) { int [] result= new int[2*nums.length]; result[result.length-1]=nums[nums.length-1]; return result; }
Problem-15 Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2. Example double23({2, 2}) → true double23({3, 3}) → true double23({2, 3}) → false
Solution public boolean double23(int[] nums) { int count2=0; int count3=0; for(int i=0;i<nums.length;i++){ if(nums[i]==2) count2++; if(nums[i]==3) count3++; } if(count2==2 || count3==2) return true; return false; }
Problem-16 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. Example fix23({1, 2, 3}) → {1, 2, 0} fix23({2, 3, 5}) → {2, 0, 5} fix23({1, 2, 1}) → {1, 2, 1}
Solution public int[] fix23(int[] nums) { int [] result=new int [3]; for(int i=0;i<3;i++){ if (nums[i]==2 && nums[i+1]==3) nums[i+1]=0; result[i]=nums[i]; } return result; }
Problem-17 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. In event of a tie, return a. Example biggerTwo({1, 2}, {3, 4}) → {3, 4} biggerTwo({3, 4}, {1, 2}) → {3, 4} biggerTwo({1, 1}, {1, 2}) → {1, 2}
Solution public int[] biggerTwo(int[] a, int[] b) { int suma=a[0]+a[1]; int sumb=b[0]+b[1]; int[] max=a; if (suma>=sumb) max=max; else max=b; return max; }
Problem-18 Given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array. The original array will be length 2 or more. Example makeMiddle({1, 2, 3, 4}) → {2, 3} makeMiddle({7, 1, 2, 3, 4, 9}) → {2, 3} makeMiddle({1, 2}) → {1, 2}
Solution public int[] makeMiddle(int[] nums) { int mid=nums.length/2; int [] result={nums[mid-1], nums[mid]}; return result; }
Problem-19 Given an array of ints, swap the first and last elements in the array. Return the modified array. The array length will be at least 1. Example swapEnds({1, 2, 3, 4}) → {4, 2, 3, 1} swapEnds({1, 2, 3}) → {3, 2, 1} swapEnds({8, 6, 7, 9, 5}) → {5, 6, 7, 9, 8}
Solution public int[] swapEnds(int[] nums) { int[] result = new int [nums.length]; result[0]=nums[nums.length-1]; result[result.length-1]=nums[0]; for( int i=1;i<nums.length-1;i++){ result[i]=nums[i]; } return result; }
Problem-20 (Merge two arrays) Given 2 int arrays, each length 2, return a new array length 4 containing all their elements. Example plusTwo({1, 2}, {3, 4}) → {1, 2, 3, 4} plusTwo({4, 4}, {2, 2}) → {4, 4, 2, 2} plusTwo({9, 2}, {3, 4}) → {9, 2, 3, 4}
Solution public int[] plusTwo(int[] a, int[] b) { int [] result=new int[4]; for(int i=0; i<a.length;i++){ result[i]=a[i];} for (int i=0;i<b.length;i++){ result[i+2]=b[i];} return result ; }
Problem-21 Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array. The array length will be at least 3. Example midThree({1, 2, 3, 4, 5}) → {2, 3, 4} midThree({8, 6, 7, 5, 3, 0, 9}) → {7, 5, 3} midThree({1, 2, 3}) → {1, 2, 3}
Solution public int[] midThree(int[] nums) { int mid=(nums.length/2); int[] result=new int[3]; for(int i=0;i<3;i++){ result[i]=nums[i+mid-1]; } return result; }
Problem-22 Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1. Example maxTriple({1, 2, 3}) → 3 maxTriple({1, 5, 3}) → 5 maxTriple({5, 2, 3}) → 5
Solution public int maxTriple(int[] nums) { int [] nums2={nums[0],nums[nums.length-1],nums[(nums.length/2)]}; int max=nums2[0]; for(int i=0;i<nums2.length;i++){ if(nums2[i]>max) max=nums2[i]; else max=max; } return max; }
Problem-23 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. Example frontPiece({1, 2, 3}) → {1, 2} frontPiece({1, 2}) → {1, 2} frontPiece({1}) → {1}
Solution public int[] frontPiece(int[] nums) { int [] result= new int[2]; if(nums.length<2) result=nums; else { for(int i=0;i<2;i++){ result[i]=nums[i]; } } return result; }
Problem-24
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.
Example
unlucky1({1, 3, 4, 5}) → true
unlucky1({2, 1, 3, 4, 5}) → true
unlucky1({1, 1, 1}) → false
Solution public boolean unlucky1(int[] nums) { int limit=nums.length; if(limit<2) return false; for(int i=0;i<limit-1;i++){ if (nums[i]==1 && nums[i+1]==3 && (i==0 || i==1 || i==limit-2 )) return true; } return false; }
Problem-25 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. Example front11({1, 2, 3}, {7, 9, 8}) → {1, 7} front11({1}, {2}) → {1, 2} front11({1, 7}, {}) → {1}
Solution public int[] front11(int[] a, int[] b) { int k=2; if (a.length==0 && b.length==0) k=0; else if(a.length==0 &&b.length!=0)k=1; else if(b.length==0 &&a.length!=0)k=1; else k=2; int [] result=new int[k]; if(a.length==0 &&b.length!=0)result[0]=b[0]; else if(b.length==0 &&a.length!=0) result[0]=a[0]; else if (a.length!=0 && b.length!=0) { result[0]=a[0]; result[1]=b[0]; } return result; }
Problem-26 Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element. Example start1({1, 2, 3}, {1, 3}) → 2 start1({7, 2, 3}, {1}) → 1 start1({1, 2}, {}) → 1
Solution public int start1(int[] a, int[] b) { if (a.length==0 || b.length==0){ if (a.length==0 && b.length==0) return 0; else if((a.length==0 && b[0]==1) || (b.length==0 && a[0]==1)) return 1; } if (a.length!=0 && b.length!=0){ if (a[0]==1 && b[0]==1) return 2; else if((a[0]==1) || (b[0]==1)) return 1; } return 0; }
** All Problems were taken from the website CodingBat