Thứ Bảy, 7 tháng 2, 2015

Amazon coding round question for fresher

Question

Given a number A, find the smallest number which has only 1s and 0s as its digits which divisible by the number A. For example: if the given number A is 4, the smallest number with 1s and 0s is which is divisible by 4 is 100.

My solution stratagy:

Am gona take a maximum 8 bit digit to stop my checking process(You may get result between these numbers surely).get remainder for each element and check for individual digits ,if these digits were 0 or 1 proceed further else stop algorithm.




FindNumber.java

package com.shashi.carrercup;

/**
 *
 * @author shashi
 */
public class FindNumber {
   
    public static void main(String args[])
    {
        FindNumber n=new FindNumber();
        System.out.print(n.getNumber(4));
    }
    public  int getNumber(int number)
    {
        //check for invalid condition
        if(number<0)
            return 0;
        //if not do some operations
        //let take maximum 8 bit digit
        int max=11111111;
         int current;
         int c1=0;
        for(int i=2;i<max;i++)
        {
            //take the current number
            boolean check=false;
             current=number*i;
            //copy number
            c1=current;
            while(current>0)
            {
                int rem=current%10;
                if(rem == 0 || rem == 1 )
                {
                    current=current/10;
                    if(current == 0)
                    {
                        check=true;
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            //you got first min if check is true
            if(check)
                break;
        }
         return c1;
    }  
 }
 

result;
-------------
100


 

Shop selling problem

Question:
Mr. Xhas recently shut down his factory and wants to sell off his metal rods to a local businessman. Mr. Octopus has many rods whose length are represented by array - lengths = {lengths[0], lengths[1], lengths[2],...}.The local businessman will only pay for rods that have same length. Let's say Mr. Octopus plans to sell rods of lengthL only. Then he had to cut each rod 0 or more time, so that he can maximize the profit. The remaining rods whose length is not L will be thrown away. Price of N rods of length L will be N × L × unit_price. Also note that for each cut made to a rod, he had to pay cut_cost.

What is the maximum amount of money Mr. Octopus can make? You have to complete the function int maxProfit(int cut_cost, int unit_price, int[] lengths).

My Solution Stratagy:

The problem states we have to find solution in such a way that seller must get maximum profit.you can do this by cutting irons with minimum length(I assume that if we take a minimum length iron ,we can cut  lot number of pieces ,so that seller profit become high)

pseudo code :

1.get the minimum length iron from given array of items

2.cut each and every iron with respect to minimum length iron

3.thrown away if some of irons length not compatable with minimum iron.


Program Solution:

ShopSelling.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package craeercu.com;

/**
 *
 * @author shashi
 */
public class ShopSelling {
   
    //the person gives iron ores ,cuting cost,unit cost return total cosst
    public int totalcost(int unitcost,int cuttngcost,int[]irons)
    {
       
        //to get max profit select a minimum length iron from given itorns
        //get the min length iron
        int shortiron=miniron(irons);
        //get each and every irin from the array of irons and match with shortiron
        //if it is equal  or atleast great
        int numberofirons=0;
        int noofcuttings=0;
        int currentiron;
        int aftercutting;
        for(int i=0;i<irons.length;i++)
        {
            //get the first one
           currentiron=irons[i];
           //check for x length ie short
           if(currentiron>shortiron)
           {
               //cut this till you find some exact length
               while(currentiron>=shortiron)
               {
                   aftercutting=currentiron-shortiron;
                   noofcuttings++;
                   numberofirons++;
                   currentiron=aftercutting;
               }
           }
           if(currentiron==shortiron)
           {
               //for same length irons
          
               numberofirons++;
           }
          
           
        }
        //calclate final result
        int result=(numberofirons*unitcost*shortiron)-(noofcuttings*cuttngcost);
        return result;
    }
    public int miniron(int []irons)
    {
        int min=irons[0];
        for(int i=1;i<irons.length;i++)
        {
            if(irons[i]<min)
                min=irons[i];
        }
        return min;
    }
 
   
}

MainClass

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ShopSelling sell=new ShopSelling();
        int []irons={10,20,30,40};
        int cost=sell.totalcost(10, 1, irons);
        System.out.print(cost);
    }
   
}




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 

Thứ Bảy, 1 tháng 11, 2014

Make Free First Minute call to INDIA & 25 countries



We all use Google Hangout to make free video and voice calls over internet. Google is offering free calls to US from a long time now. Well offer is gone little better till end of 2014. Now you can make free first minute calls to 25 countries including INDIA. Read on for details.







Details about the offer

It will be mentioned on the screen if first minute of call is free. 
There is no