Showing posts with label JAVA BAT : Array 1. Show all posts
Showing posts with label JAVA BAT : Array 1. Show all posts

Sunday, January 16, 2011

JAVA BAT : Array 1 : SameFirstLast

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems.

Same First Last Problem :Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are the same.

sameFirstLast({1, 2, 3}) → false
sameFirstLast({1, 2, 3, 1}) → true
sameFirstLast({1, 2, 1}) → true

To solve this problem first we check weather the array given is of length greater then zero or not. To check this we use the method Array.length (For strings the method is String.length() ). Then we will check for the first and last element, are they same.

So the code for this problem: 


           public boolean sameFirstLast ( int[] nums ) {
 
                   if ( nums.length>0 ){
                             if ( nums[0]==nums[nums.length-1] ){
                                         return true;
                             }else{
                                       
return false;
                            }
                   }
                   else{
                            return false;
                   }

          }

Java Bat : Array 1 : FirstLast6

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems.

First Last 6 Problem : 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.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({3, 2, 1}) → false

In this problem an array of ints is passed to a method firstLast6 and if 6 appears as either the first or last element in the array the firstLast6 method will return true else it will return false.

To check weather first and last element is 6 or not.


                    nums[0]==6 || nums[nums.length-1]==6


So the Answer for this Java Bat Problem:


           public boolean firstLast6( int[] nums ) {
                   
                        if ( nums[0]==6 || nums[nums.length-1]==6 ){
                                      return true;
                        }
                        else{
                                     return false;

                        }
          }

Saturday, January 15, 2011

JAVA BAT : Array 1 Problems

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems. To solve these problems we should use simple array methods for example:

arr[0], arr[1].......a[n] to access the elements of array.
arr.length to get the number of elements in array.
int[] arr = int[n] to initialize array of n integers (n is length for array)
int[] num = {1,2,3,4};  to initialize array in which element of array will be 1,2,3 and 4

Array 1 Problems:
 firstLast6 sameFirstLast makePi
 commonEnd sum3 rotateLeft3
 reverse3 maxEnd3 sum2
 middleWay makeEnds has23
 no23 makeLast double23
 fix23 start1 biggerTwo
 makeMiddle plusTwo swapEnds
 midThree maxTriple frontPiece
 unlucky1 make2 front11