Saturday, August 8, 2015

Sum of multiple of 3 or 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

1
2
3
4
5
6
7
8
public static void main(String []args){
   int sum = 0;
   for (int x = 0; x < 10; x++) {
      if (x % 3 == 0 || x % 5 == 0)
 sum += i;
   }
   System.out.print(sum);
}

Friday, August 7, 2015

Prime Factor of integers

The prime factors of 13195 are 5, 7, 13 and 29.

Input: 13195

Ouput : 5,7,13,29


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     public static void main(String []args){
         int n = 13195;
         
         isPrimeFactor(n);
         
     }
     
     public static void isPrimeFactor(int n){
         
         int count=0;
         ArrayList arr = new ArrayList();
         for(int x=2;x<=n;x++){
            if(n%x==0){
               arr.add(x);
               
               n = n/x;
               x--;
               count++;      
            }
         }
         
         System.out.print(arr.toString());
     }

Thursday, August 6, 2015

Count two 6's or 7's are next to each other

Given an array of integers, return the number of times that two 6's are next to each other in the array. Also count instances where the second "6" is actually a 7.

function({6, 6, 2}) → 1
function({6, 6, 2, 6}) → 1
function({6, 7, 2, 6}) → 1
function({6, 6, 2, 6, 7}) → 2
function({6, 1}) → 0
function({1, 2, 3, 5, 6}) → 0



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int function667(int[] nums) {
  int count = 0;
  for (int i=0; i < (nums.length-1); i++) {
    if (nums[i] == 6) {
      if (nums[i+1] == 6 || nums[i+1] == 7) {
        count++;
      }
    }
  }
  return count;
}

No triples interger in a row

Given an array of integers, we'll say that a triple is a value appearing 3 times in a row in the array. Return true if the array does not contain any triples.

function({1, 1, 2, 2, 1}) → true
function({1, 1, 2, 2, 2, 1}) → false
function({1, 1, 1, 2, 2, 2, 1}) → false



1
2
3
4
5
6
7
8
public boolean noTriples(int[] nums) {
  for(int x=0;x<nums.length-2;x++){
      if(nums[x]==nums[x+1] && nums[x]==nums[x+2])
        return false;
  }
  
  return true;
}

String Matching 1

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.

Input Output
xxcaazz
xxbaaz
3
abc
abc
2
abc
axc
0


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public int stringMatch(String a, String b) {
  int len = (a.length()<=b.length())? a.length() : b.length();
  int count=0;
  for(int x=0;x<len-1;x++){
    if(a.substring(x,x+2).equals(b.substring(x,x+2)))
      count++;
  }
  
  return count;
}

Sunday, July 19, 2015

Binary Search

Array must be sorted first before searching.

example
Sorted array: L = [1, 3, 4, 6, 8, 9, 11]
   Target value: X = 4
   Compare X to 6. X is smaller. Repeat with L = [1, 3, 4].
   Compare X to 3. X is larger. Repeat with L = [4].
   Compare X to 4. X equals 4, so the position is returned.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class BinarySearch{

     public static void main(String []args){
        // Binary search must be sort first
        int[] array = new int[]{10,20,50,53,68,81,92,105,110,200,280,360,361,386,400};
        
        if(binSearch(array,81) == -1)
            System.out.print("not found");
        else
            System.out.print("found at index "+ binSearch(array,81));
     }
     
     public static int binSearch(int[] arr, int search){
         int first =0;
         int last = arr.length-1;
         int mid=0;
         
         while(first<=last){
             mid = (first+last)/2;
             
             if(arr[mid] == search)
                return mid;
             else if (arr[mid]<search)
                first = mid+1;
             else
                last = mid-1;
         }
         
         return -1;
     }
}

Monday, July 13, 2015

Longest Palindrome in String

Longest Palindromic substring. Given a string, find the longest substring which is palindrome. For example, if the given string is "forgeeksskeegfor", the output should be "geeksskeeg".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class PalinLong{

     public static void main(String []args){
        
        String s = "forgeeksskeegfor";
        String longest="";
        for(int x=0;x<s.length();x++){
            for(int y=x+1;y<=s.length();y++){
                String t = s.substring(x,y);
                if(isPalin(t)){
                    if(t.length()>longest.length()){
                        longest = t;
                    }
                }
            }   
        }
        
        System.out.print(longest);
     }
     
     public static boolean isPalin(String s){
         String rev="";
         
         for(int x=s.length()-1;x>=0;x--){
             rev = rev + s.charAt(x);
         }
         
        if(rev.equals(s))
            return true;
        else
            return false;
     }
}