Tuesday, June 4, 2013

HackerRank: Bot saves princess

Here is the link of the question : Click Here

Download Code: Click Here
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
/* Head ends here */
static void displayPathtoPrincess(int n, String [] grid){
int pos=0;

// System.out.println("n= "+n+" S length = "+grid[1]);
String newArray[] = new String[n*n];
//System.out.println(grid.length);
/*for(int j=0;j{
System.out.println(grid[j]);
}*/
//System.out.println("---------------");
for(int i=0,k=0;i{
for(int j=0;j {
newArray[k] = ""+grid[i].charAt(j);
// System.out.println(grid[i]);
k++;
}
}
// for(int i =0;i // System.out.println(i+" "+newArray[i]);
if(newArray[0].equals("p"))
pos = 0;
else if(newArray[n-1].equals("p"))
pos = n-1;
else if(newArray[n*(n-1)].equals("p"))
pos = n*(n-1);
else if(newArray[n*(n-1) + (n-1)].equals("p"))
pos = (n*(n-1) + (n-1));

int m = ((n*n)+1)/2 -1 ;


m = (n+1)/2;
if(pos==0)
{
for(int i=0;i {
System.out.println("LEFT");
}
for(int i=0;i {
System.out.println("UP");
}
}
if(pos==n-1)
{
for(int i=0;i {
System.out.println("RIGHT");
}

for(int i=0;i {
System.out.println("UP");
}
}
if(pos==n*(n-1))
{
for(int i=0;i {
System.out.println("LEFT");
}

for(int i=0;i {
System.out.println("DOWN");
}
}
if(pos==(n*(n-1) + (n-1)))
{
for(int i=0;i {
System.out.println("RIGHT");
}

for(int i=0;i {
System.out.println("DOWN");
}
}
}
/* Tail starts here */
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m;
m = in.nextInt();
String grid[] = new String[m];
for(int i = 0; i < m; i++) {
grid[i] = in.next();
}

displayPathtoPrincess(m,grid);
}
}


Download Code: Click Here

TechGig Solution - May Edition 2013 - Easy Level - Partition Problem

Partition Problem:
Hi,
I am providing the solution of Partition Problem here. My score in the contest is 100 in java, if you have any better solution of this problem, feel free to share it.

Download Link : Code
Download link : Code
public class Partition {

/**
* @param args
*/
public static final String yes = "Yes";
public static final String no = "No";
public static final String invalid = "Invalid";
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {1, 5, 11,5};

System.out.println(findPartiion(arr,4));

}
// A utility function that returns true if there is a subset of arr[]
// with sun equal to given sum
public static boolean isSubsetSum (int arr[], int n, int sum)
{
System.out.println(n+" "+sum);
// Base Cases
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;

// If last element is greater than sum, then ignore it
if (arr[n-1] > sum)
return isSubsetSum (arr, n-1, sum);

/* else, check if sum can be obtained by any of the following
(a) including the last element
(b) excluding the last element
*/
return isSubsetSum (arr, n-1, sum) || isSubsetSum (arr, n-1, sum-arr[n-1]);
}

// Returns true if arr[] can be partitioned in two subsets of
// equal sum, otherwise false
public static String findPartiion (int arr[], int n)
{
// Calculate sum of the elements in array
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
if(arr[i]<0)
return invalid;
}
// If sum is odd, there cannot be two subsets with equal sum
if (sum%2 != 0)
return no;

// Find if there is subset with sum equal to half of total sum
boolean b = isSubsetSum (arr, n, sum/2);
if(b)
return yes;
else
return no;

}

}


Download Link : Code
Download link : Code

Thursday, May 9, 2013

Techgig: Visit a Colony

Techgig: Visit a Colony
To view this question : Click Here

I have given the solution here, My score in the contest is 100. If you have any solution better than this feel free to share it.

Download Code: Click Here or Click Here

import java.io.*;
public class CandidateCode
{
public static int house_condition(int[] input1,int input2)
{
//Write code here
//Write code here

int houseScore[] = input1;
int houseNo = input2;
char good = 'G';

char bad = 'B';

char houseCondition[] = new char[houseScore.length];

if(input1.length==0)
return -1;
if(houseScore.length==1)
{
if(houseScore[0]==1)
return 1;
else if(houseScore[0]==0)
return -1;
else if(houseScore[0]==2 || houseScore[0]==3)
return -1;
}
else
{
for(int i=0;i{

if(i==0)
{
houseCondition[0]=good;
if(houseScore[i]==1)
houseCondition[i+1]=bad;

if(houseScore[i]==2)
houseCondition[i+1]=good;

if(houseScore[i]==3)
return -1;
continue;
} // END-IF (For first value in an array)

if(i==houseScore.length-1)
{
if(houseCondition[i]==good)
{
if(houseScore[i]==0)
{
return -1;
}
if(houseScore[i]==1)
{
if(houseCondition[i-1]==good)
return -1;
}
if(houseScore[i]==2)
{
if(houseCondition[i-1]==bad)
return -1;
}
if(houseScore[i]==3)
return -1;
}

if(houseCondition[i]==bad)
{
if(houseScore[i]==0)
{
if(houseCondition[i-1]==good)
return -1;
}
if(houseScore[i]==1)
{
if(houseCondition[i-1]==bad)
return -1;
}
if(houseScore[i]==2 || houseScore[i]==3)
return -1;
}
} // END -IF (When i is at last position)


else
{
if(houseCondition[i]==good)
{
if(houseScore[i]==3)
{
if(houseCondition[i-1]==bad)
return -1;
else
houseCondition[i+1]=good;
}

if(houseScore[i]==2)
{
if(houseCondition[i-1]==good)
houseCondition[i+1]=bad;
else
houseCondition[i+1]=good;
}
if(houseScore[i]==1)
{
if(houseCondition[i-1]==good)
return -1;
else
houseCondition[i+1]=bad;
}
if(houseScore[i]==0)
{
return -1;
}
}

if(houseCondition[i]==bad)
{
if(houseScore[i]==3)
return -1;

if(houseScore[i]==2)
{
if(houseCondition[i-1]==bad)
return -1;

else
houseCondition[i+1]=good;
}

if(houseScore[i]==1)
{
if(houseCondition[i-1]==bad)
houseCondition[i+1]=good;

else
houseCondition[i+1]=bad;
}
if(houseScore[i]==0)
{
if(houseCondition[i-1]==good)
return -1;
else
houseCondition[i+1]=bad;
}
}
}
}
}

if(houseCondition[houseNo-1]==good)
return 1;
else
return 0;
}
}



Download Code: Click Here or Click Here