Thứ Sáu, 26 tháng 12, 2014

Constructing Bridges Problem

Constructing Bridges: 
A River that cuts through a set of cities above and below it. Each city above the river is matched with a city below the river. 
Construct as many Non-Crossing Bridges as possible. 
Input: 
Above Bank >> 7 4 3 6 2 1 5 
Below Bank >> 5 3 2 4 6 1 7 
are given in pairs: (7,5) (4,3) (3,2) (6,4) (2,6) (1,1) (5,7) 
Output: 
(1,1) (3,2) (4,3) (6,4) (7,5)



Solution
------------


import java.util.HashMap;
import java.util.Map;


/**
 * @author shashi
 *
 **/
public class RiverBridge {

public static void main(String []args)
{
//get the above and below banks
int aboveBank[]={7 ,4, 3, 6, 2, 1, 5};
int belowBank[]={ 5, 3, 2, 4, 6, 1, 7};

//now create a Map for storing Pairs
Map<Integer, Integer> myMap=new HashMap<Integer, Integer>();
//base condition
if(aboveBank.length !=belowBank.length)
{
//River flow is not correct way
System.out.print("Your river is not flowing in correct way");
}
//if not
for(int i=0;i<aboveBank.length-1;i++)//you can also use below.length
{
if(aboveBank[i]>=belowBank[i])
{
//pair for bridge is founded so add to map
myMap.put(aboveBank[i], belowBank[i]);
}
}
//now print map elements
for(Map.Entry<Integer, Integer> entry:myMap.entrySet())
{
int key=entry.getKey();
int val=entry.getValue();
System.out.print("("+key+","+val+")"+"  " );
}

}
}


output:
-----------
(1,1)  (3,2)  (4,3)  (6,4)  (7,5)  

Thứ Tư, 24 tháng 12, 2014

Watch Indian Cricket Sports HD Legally in USA



Cricket Fever is always on high. In USA getting a HD cricket telecast is not so easy. Few of the matches are available on ESPN/Cricinfo if you have AT&T Uverse or Comcast services but not always. If you need a cheap, reliable and high quality option then this article is for you. Read on for details.




Willow TV has rights to telecast most of the cricket matches. Willow TV is available for $

Thứ Sáu, 19 tháng 12, 2014

MergeSort

MergeSort program in java

/* Program written by shashi kumar*/
package sortingPackag;

public class MergeSort {

public static void main(String []args)
{

//get The elements for merge sort
int ele[]={11,4,55,6,99,78,0,99,-99};
int re[]=new int[ele.length];
re=merge_sort(ele);
//print res
for(int i=0;i<re.length;i++)
{
System.out.print(re[i]+" ");
}


}
public static int [] merge_sort(int data[])
{
if(data.length<=1)
{
return data;
}
int midpoint=data.length/2;

int left[]=new int[midpoint];
int right[];
int res[]=new int[data.length];
if(data.length%2==0)
{
//even so
right=new int[midpoint];

}
else
{
right=new int[midpoint+1];
}
//copy data values form data to left and right
for(int i=0;i<midpoint;i++)
{
left[i]=data[i];
}
//copy to right
int x=0;
for(int j=midpoint;j<data.length;j++)
{
if(x<data.length)
{
right[x]=data[j];
x=x+1;
}
}
left=merge_sort(left);
right=merge_sort(right);
res=merge(left,right);
return res;
}
public static int [] merge(int left[],int right[])
{
int result[]=new  int[left.length+right.length];
int l=0,r=0,k=0;
while(l<left.length && r<right.length)
{
if(left[l]<right[r])
{
//get minimum from both arrays
result[k]=left[l];
k=k+1;
l=l+1;

}
else
{
result[k]=right[r];
r=r+1;
k=k+1;
}
}
//if not both
while(l<left.length)
{
result[k]=left[l];
l=l+1;
k=k+1;

}
while(r<right.length)
{
result[k]=right[r];
k=k+1;
r=r+1;
}
return result;
}
}


output:
---------------------------

-99 0 4 6 11 55 78 99 99

Thứ Năm, 18 tháng 12, 2014

Algorithm:

write an algorithm or a java program to complete the given task.

problem:

          for example consider an array of elements 1,4,3,8,22,10 .Now  you have to sort the array in a way that its even postioned values will be in decreasing order ,its odd positined values must be in increasing order .For given problem the solution will be 1 22 3 10 4 8 .


solution:

//algoritm written by shashi kumar

import java.util.Arrays;


public class TecnicsProgram {

public static void main(String args[])
{

//given data  is 1,4,3,8,22,10
int givenData[]={1,4,3,8,22,10};
//sort the data next from given arry of data
Arrays.sort(givenData);

//check weather data is sorted or not
/*for(int i=0;i<givenData.length;i++)
{
System.out.print(givenData[i]+" ");
}
*/
//now take first index and last index values of givenData
int firstIndex=0,lastIndex=givenData.length-1;
//define a new arry for result

int res[]=new int[givenData.length];
for(int index=0;index<givenData.length;index++)
{
//loop through each and every element of given array
if(index%2==0)
{
//even position so put min element
if(firstIndex>lastIndex)
break;
res[index]=givenData[firstIndex];
firstIndex++;
}
else
{
//it is an even index so put max element
if(lastIndex<firstIndex)
break;
res[index]=givenData[lastIndex];
lastIndex--;
}
}

//now print res array
for(int x=0;x<res.length;x++)
{
System.out.print(res[x]+" ");
}
}


}

output:
1 22 3 10 4 8