Transform and Conquer

Sample Program

Find Largest / Smallest

public class FindLargestSmallestNumberMain {
	
 public static void main(String[] args) {
	
	//array of 10 numbers
	int arr[] = new int[]{12,56,76,89,100,343,21,234};
		
	//assign first element of an array to largest and smallest
	int smallest = arr[0];
	int largest = arr[0];
		
	for(int i=1; i< arr.length; i++)
	{
	if(arr[i] > largest)
		largest = arr[i];
	else if (arr[i] < smallest)
		smallest = arr[i];
		
	}
	System.out.println("Smallest Number is : " + smallest);
	System.out.println("Largest Number is : " + largest); 
}

}

Finding Largest / Smallest sorted data

class FindSmallSorted {	
   public static void main(String[] args) {
		int[] nums={6,-1,-2,-3,0,1,2,3,4};
		Arrays.sort(nums);
		for (int i = 0; i < nums.length; i++) {
			System.out.print(" "+ nums[i]);
		}
		System.out.println();
		System.out.println("Minimum = " + nums[0]);
		System.out.println("Maximum = " + nums[nums.length-1]);
	}
}

Linear Search

public class LinearSearchExample{    
    public static int linearSearch(int[] arr, int key){    
        for(int i=0;i<arr.length;i++){    
            if(arr[i] == key){    
                return i;    
            }    
        }    
        return -1;    
    }    
    public static void main(String a[]){    
        int[] a1= {30,20,10,50,40,90};    
        int key = 50;    
        System.out.println(key+" is found at index: "+linearSearch(a1, key));    
    }    
}   

Searching

import java.util.*;
class Untitled{
  public static void main(String[] args){
    //array
    int[] a = {3,2,4,3,7,9,8,10};
    boolean ans = contains(a,100);
    if (ans){
      System.out.println("Number found ");
    }else{
      System.out.println("Number not found ");
    }
  }
  public static boolean contains(int[] a, int b){
    for (int i:a){
      System.out.println("Comparing with "+i);
      
      if (i==b){
        return true;
      }
    }
    return false;
  }

}

import java.util.*;
class Untitled {
	public static void main (String[] args){
		int[] a = {1,2,3,4,7,8,9,10,11};
		boolean ans = contains(a, 5);
		if (ans){
			System.out.println("Number found");
		}else {
			System.out.println("Number not found");
		}
	}
	public static boolean contains(int [] a, int b){
		for (int i:a){
			System.out.println("Comparing with "+i);
			if (i==b){
				return true;
			}else if (i>b){
				return false;
			}
		}
		return false;
	}
}

Sample Gausian Elimination


x + y + z = 10				1	1	1	|	10
3x + 2z = 19				3	0	2	|	19
3x + 2y + 4z = 27			3	2	4	|	27


1. Swap rows
2. Multiply a row a constant  (not zero)
3. Add or subtract row or multiply

1	1	1	|	10			3	2	4	|	27
3	0	2	|	19			3	2	4	|	27		
1	1	1	|	10			3	0	2	|	19

1	1	1	|	10			1	1	1	|	10
3	0	2	|	19		   3(2)0(2)2(2) |   19(2)
3	2	4	|	27			3	2	4	|	27

R2-R1
1	1	1	|	10			1	1	1	|	10
1	3	2	|	19		   1-1 3-1 2-1	| 	19-10
3	2	4	|	27			3	2	4	|	27
		
							1	1	1	|	10
							0	2	1	|	9
							3	2	4	|	27
							
									Augmented matrix
							
		x + y + z = 10				1	1	1	|	10
		x + 3y +2z = 19				1	3	2	|	19
		3x + 2y + 4z = 27			3	2	4	|	27		
		
									R2 - R1
									1	1	1	|	10
									0	2	1	|	9
									3	2	4	|	27
		
									R3 - 3R1
									1	1	1	|	10
									0	2	1	|	9
									0	-1  1	|   -3
									
									R2 + 2R3
									1	1	1	|	10
									0	0	3	|	3
									0	-1  1	|   -3
									
									SWAP R2 AND R3
									1	1	1	|	10
									0	-1  1	|   -3
									0	0	3	|	3