Friday, 24 July 2015

Java Programming solution to solve a problem on optimal assignments ,detailed problem statement is given below .....

/*Using the Java language, have the function OptimalAssignments(strArr) read strArr which will represent an NxN matrix and it will be in the following format: ["(n,n,n...)","(...)",...] where the n's represent integers. This matrix represents a machine at row i performing task at column j. The cost for this is matrix[i][j]. Your program should determine what machine should perform what task so as to minimize the whole cost and it should return the pairings of machines to tasks in the following format: (i-j)(...)... Only one machine can perform one task. For example: if strArr is ["(5,4,2)","(12,4,3)","(3,4,13)"] then your program should return (1-3)(2-2)(3-1) because assigning the machines to these tasks gives the least cost. The matrix will range from 2x2 to 6x6, there will be no negative costs in the matrix, and there will always be a unique answer*/


import java.util.*; 
import java.io.*;

class Function {  
  String OptimalAssignments(String strArr) { 
  
    // code goes here   
    /* Note: In Java the return type of a function and the 
       parameter types being passed are defined, so this return 
       call must match the return type of the function.
       You are free to modify the return type. */
       int[][] mat=new int[3][3];
       
       
       String[] arr=strArr.split("\\)");
        
        
        
        String[] a1=arr[0].split("\\(");
        String[] a2=arr[1].split("\\(");
        String[] a3=arr[2].split("\\(");
        
          String[] r1=a1[1].split(",");
          String[] r2=a2[1].split(","); 
          String[] r3=a3[1].split(",");  
          
          for(int k=0;k<3;k++)
            mat[0][k]=Integer.parseInt(r1[k]);


          for(int s=0;s<3;s++)
            mat[1][s]=Integer.parseInt(r2[s]);


          for(int p=0;p<3;p++)
            mat[2][p]=Integer.parseInt(r3[p]);
           
         /* for (int y=0;y<3;y++)
           for (int r=0;r<3;r++)
          System.out.println(mat[y][r]);
          */
          Function d=new Function();
         int[] sum=d.mincost_map(mat,0);
          strArr="\"(1-"+sum[0]+")(2-"+sum[1]+")(3-"+sum[2]+")\"";
    return strArr;
    
  } 
  
  
  public int[] mincost_map(int[][] a,int b){
 int[] c=new int[3];
 Function f=new Function();
 //int[][] arr=new int[b][b]; 
        c[0]=f.find_min(a,0,3);
        c[1]=f.find_min(a,1,c[0]);
        c[2]=f.find_min(a,2,c[1]);
 
 return  c;
  }
  
  public int find_min(int[][] a,int r,int num){
 int sum=a[r][0];
 int q=0;
for(int y=0;y<3;y++){
if(a[r][y]<sum)

if(y==num) 
continue;
else { sum=a[r][y]; q=y;}
}
}
return q;
  }
  
  public static void main (String[] args) throws Exception{  
    // keep this function call here     
    BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
    String str=s.readLine();
    Function c = new Function();
    //String str="[\"(5,4,2)\",\"(12,4,4)\",\"(3,4,13)\"]";
    System.out.print(c.OptimalAssignments(str));
  //Function c=new Function();

   
  
  //System.out.println(c.OptimalAssignments(str));
    
  }   
  
}


Tuesday, 21 July 2015

Java program to find out all possible expressions to insert '+' and '-' in between values 1..to ..9 so that the given sum of the expression remains constant ....like below

/*
Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100

*/


       
import java.util.ArrayList;
import java.util.Iterator;

public class TestIt {

 private static int SUM = 200;
 private static int[] values = {
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9
 };

 static ArrayList add(int digit, String sign, ArrayList branches) {
  for (int i = 0; i < branches.size(); i++) {
   branches.set(i, digit + sign + branches.get(i));
  }

  return branches;
 }

 static ArrayList f(int sum, int number, int index) {

  int digit = Math.abs(number % 10);
  //System.out.println(digit);
  if (index >= values.length) {
   if (sum == number) {
    ArrayList result = new ArrayList();
    result.add(Integer.toString(digit));
    return result;
   } else {
    return new ArrayList();
   }
  }

  ArrayList branch1 = f(sum - number, values[index], index + 1);
  ArrayList branch2 = f(sum - number, -values[index], index + 1);

  int concatenatedNumber = number >= 0 ? 10 * number + values[index] : 10 * number - values[index];
  ArrayList branch3 = f(sum, concatenatedNumber, index + 1);

  ArrayList results = new ArrayList();

  results.addAll(add(digit, "+", branch1));
  results.addAll(add(digit, "-", branch2));
  results.addAll(add(digit, "", branch3));

  return results;
 }

 public static void main(String[] args) {

  Iterator < String > itr = f(SUM, values[0], 1).listIterator();
  //for (String string :f(TARGET_SUM, VALUES[0], 1))
  if (itr.hasNext()) {
   System.out.println(itr.next());
  }

 }
}

       
 

Perfect numbers in a given range using Java........

Here is the code ....


       
import java.util.ArrayList;
import java.util.Iterator;

import java.util.List;

public class test5 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List d = getPerfectNumbers(1, 1000);

  Iterator itr = ((ArrayList < Integer > ) d).listIterator();

  while (itr.hasNext())
   System.out.println(itr.next());
 }


 public static java.util.List < Integer > getPerfectNumbers(int from, int to) {
  /*
    return a list of all perfect numbers in the given range inclusively.
    A perfect number is defined as a positive integer which is the sum of its positive divisors not including the number itself.
    For example: 6 is a perfect number because 6 = 1 + 2 + 3 (1, 2, 3 are divisors of 6)
    28 is also a perfect number: 28 = 1 + 2 + 4 + 7 + 14
   */
  int k;
  java.util.List < Integer > l = new ArrayList < Integer > ();
  for (int p = from; p <= to; p++) {
   k = sum(p);
   if (k == p) {
    l.add(p);

   }
  }
  return l;
 }


 public static int sum(int t) {
  int sum = 0;
  for (int i = 1; i < t; i++) {
   if (t % i == 0)
    sum = sum + i;
  }
  return sum;
 }

}

       
 

Java program to find sum of two elements closest to zero from an integer array ...

Here is the code ....


       
public class test3 {

 public static int getSumOfTwoClosestToZeroElements(int[] a) {
  /*
    Please implement this method to
    return the sum of the two elements closest to zero.
    If there are two elements equally close to zero like -2 and 2,
    consider the positive element to be "closer" to zero than the negative one.
   */
  int sum = 0;
  int h = 0;
  int[] d = new int[a.length + 1];
  d[0] = 0;

  for (int i = 1; i < a.length + 1; i++) {
   d[i] = a[h];
   h++;
  }



  int i = partition(d, 0, d.length);
  //System.out.println(i);

  for (int e = 0; e < d.length; e++)
   System.out.print(d[e] + "  ");
  System.out.println();



  int g = min(d, i + 1, d.length - 1);
  int g1 = max(d, 0, i - 1);
  //  System.out.println(g1);

  sum = g + g1;

  return sum;
 }

 public static int partition(int[] s, int i, int l) {
  int p = i;
  int a = i;
  for (int k = i + 1; k < s.length; k++) {
   if (s[p] > s[k]) {
    int temp;
    a++;
    temp = s[a];
    s[a] = s[k];
    s[k] = temp;
   }

  }
  int temp = s[a];
  s[a] = s[p];
  s[p] = temp;

  return a;
 }

 public static int min(int[] a, int f, int c) {
  int i = a[f];
  for (int s = f; s <= c; s++) {
   {
    if (i > a[s]) {
     i = a[s];

    }
   }
  }
  return i;
 }

 public static int max(int[] a, int f, int c) {
  int i = a[f];
  for (int s = f; s <= c; s++) {
   {
    if (i < a[s]) {
     i = a[s];

    }
   }
  }
  return i;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int[] w = {
   1,
   -2,
   3,
   5,
   6,
   7,
   8,
   -3,
   -6,
   2
  };
  System.out.println("The required sum is- " + getSumOfTwoClosestToZeroElements(w));
 }

}

       
 

Java program to check whether a string is palindrome or not ....

Here is the code....


       

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test2 {

 static boolean isPalindrome(String s) {

  int y = 0;
  for (int u = s.length() - 1; u >= 0; u--) {
   if (s.charAt(y) != s.charAt(u))
    return false;
   y++;
  }

  return true;
 }

 public static void main(String args[]) throws Exception {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String str = br.readLine();
  System.out.println(isPalindrome(str));
 }

}