Sunday, September 28, 2014

Elements before 4 in an int array

Ques:Given a non-empty array of ints, return a new array containing the elements from the original array that come before the first 4 in the original array.

Ans:
 public static int[] pre4(int[] nums) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
                  if (nums[i] == 4) {
                break;
            }
            count++;
        }
        int[] tempArray=new int[count];
        for(int j=0;j<count;j++){
            tempArray[j]=nums[j];
              }
     return tempArray;
    }

Saturday, September 27, 2014

Shifted Array

Ques: Return an array that is "left shifted" by one -- so {6, 2, 5, 3} returns {2, 5, 3, 6}. You may modify and return the given array, or return a new array.

Ans:
public int[] shiftLeft(int[] nums) {
  int var = 0;
       if (nums.length>0){
        var = nums[0];
               for (int i = 1; i < nums.length; i++) {
            if ( i != nums.length - 1) {
      
                nums[i-1] = nums[i];
            }
         
            if (i == nums.length - 1) {
            
                nums[nums.length-2]=nums[nums.length-1];       
                nums[nums.length - 1] = var;
           
              }
          }
        }

        return nums;
}

Arrays: Create arrays dynamically

Ques:Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid.

Ans:
public int[] fizzArray3(int start, int end) {
   int[] temparray=new int[end-start];
        int tot=end-start;
        for(int i=0;i<tot;i++){
                      temparray[i]=start;
                              start++;
        }
        return temparray;
}

Arrays: Numbers in ascending order

Ques:Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.

Ans:
public boolean tripleUp(int[] nums) {
  int count=0;
        for (int i=0;i<nums.length-2;i++ ) {
            if((nums[i+1]-nums[i]==nums[i+2]-nums[i+1])&&(nums[i+1]-nums[i]>0)){
                               count++;
                }
        }
        return count>0;
}

Array Examles same Ends

Comment: The below mentioned example uses some very basic concept of arrays all the while testing the logic. The question is in response to a question posed in battery of questions in codingbat.com

Ques:Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.

Ans:
public boolean sameEnds(int[] nums, int len) {
 int[] hnums = new int[len== 0 ? 2 * len + 1 : 2 * len];
        int[] hrnums = new int[len == 0 ? 2 * len + 1 : 2 * len];
        int count = 0;
              for (int k = 0; k < len; k++) {
            if (nums[k] == nums[k + nums.length - len]) {
                 }
            }

        return count == len ? true : false;
}

Sunday, September 21, 2014

3 appears in the array exactly 3 times

Ques: Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other.

Ans:
package countofthreenotnnext;
public class CountofthreenotnNext {

     public static void main(String[] args) {
             int[] nums = {3,2,3};
        System.out.println("Output=" + haveThree(nums));
    }

    public static boolean haveThree(int[] nums) {
        int count = 0;
        boolean mark = false;
        int[] trackpos = new int[nums.length];
        int pos = 0;
        if (nums.length > 2) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 3) {
                    count++;
                    pos = pos + i;
                    trackpos[count - 1] = i;
                             }
                          }

            for (int i = 0; i < 3; i++) {
            }
            if ((trackpos[1] - trackpos[0]) == 2 && (trackpos[2] - trackpos[1]) == 2) {
                mark = true;
            }

          
        }
     return (count == 3) && mark ? true : false;
    }
}
 

Consetive 3 odd or even values

Ques: Given an array of ints, return true if the array contains either 3 even or 3 odd values all next to each other.

Ans:
package oddorevenin.array;
public class OddorEveninArray {
  public static void main(String[] args) {

        int[] nums = {2, 2, 2, 5, 9};
        System.out.println("Nunber=" + modThree(nums));
    }

    public static boolean modThree(int[] nums) {
        int ecount = 0;
        int ocount = 0;
        int oloop = 0;
        int eloop = 0;
        for (int i = 0; i < nums.length; i++) {
            oloop = i;
            if (nums[oloop] % 2 == 1) {

                for (int j = 0; j < 3; j++) {
                         if (j + i > nums.length - 1) {
                        break;
                    }
                    if (nums[j + i] % 2 == 1) {
                             ocount++;
                        oloop++;
                    }
                    if (ocount == 3) {
                        break;
                    }
                }
                if (ocount < 3) {
                    ocount = 0;
                }
            }
            if (ocount == 3) {
                break;
            }
        }

        /////////////////////////Starting the Even loop
        for (int i = 0; i < nums.length; i++) {
            eloop = i;
            if (nums[eloop] % 2 == 0) {

                for (int j = 0; j < 3; j++) {
                    if (j + i > nums.length - 1) {
                        break;
                    }
                    if (nums[j + i] % 2 == 0) {
                        ecount++;
                        eloop++;
                    }
                    if (ecount == 3) {
                        break;
                    }
                }
                if (ecount < 3) {
                    ecount = 0;
                }
            }
            if (ecount == 3) {
                break;
            }
        }

        return (ocount == 3 || ecount == 3) ? true : false;
    }

}