Sunday, 14 April 2013

Google Code Jam Problem C. Fair and Square


Below is my solution for Problem C. Fair and Square

Well I qualified for Google Code Jam 2013 with only 60 points.

* Still to add method to handle Large Input 2.





Input is taken from a file(C-small-practice.in)



Method 1:


/*
* @author niraj.nijju
*/


package gcj.TwelveThirteen;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class SmallOfFairAndSquare {

public static void main(String [] args){
try{
// System.out.println(Double.MAX_VALUE);
// System.exit(0);

  FileInputStream fstream = new FileInputStream("C-small-practice.in");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  strLine = br.readLine();
  int T = Integer.valueOf(strLine);
  System.out.println("T: "+T);
  int i=1;
  while ((strLine = br.readLine()) != null)   {
//   System.out.println (strLine);
  String [] str = strLine.split(" ");
  double x = Double.valueOf(str[0].trim() );
  double y = Double.valueOf(str[1].trim() );
  int count= getFairSqur(x,y);
  System.out.println ("Case #"+i++ +": "+count);
  }
  in.close();
}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  e.printStackTrace();
}
}


public static int getFairSqur(double x, double y){
int count=0;
long x1 = (long)Math.ceil(Math.sqrt(x));
long y1 = (long)Math.sqrt(y);

for(long i=x1; i<=y1; i++ ){
if(checkPalindrom(i)){
long sq=i*i;
if(checkPalindrom(sq)){
// System.out.println("("+i+")");
count++;
}
}
}


return count;
}



    public static Boolean checkPalindrom(long n)
    {
     String normal = String.valueOf(n);
    //reverse the letters in string
    int length = normal.length(); //length of string
    StringBuffer result = new StringBuffer(length);
    int i;
    for(i = length - 1; i>=0; i--)
    {
    result.append(normal.charAt(i));
    }
    long r = Long.valueOf(result.toString());

    if(r==n)
     return true;
    return false;
    }


}




Method 2:





/*

* @author niraj.nijju

*/



package gcj.TwelveThirteen;



import java.io.BufferedReader;

import java.io.DataInputStream;
import java.io.FileInputStream;

import java.io.InputStreamReader;


import org.omg.CORBA.INTERNAL;

public class CopyOfFairAndSquare {

public static long [] array = new long[10000010];
public static void main(String [] args){
fillArray();
try{
// System.out.println(Double.MAX_VALUE);
// System.exit(0);
  FileInputStream fstream = new FileInputStream("C-small-practice.in");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  strLine = br.readLine();
  int T = Integer.valueOf(strLine);
//   System.out.println("T: "+T);
  int i=1;
  while ((strLine = br.readLine()) != null)   {
//   System.out.println (strLine);
  String [] str = strLine.split(" ");
  double x = Double.valueOf(str[0].trim() );
  double y = Double.valueOf(str[1].trim() );
  //int count= getFairSqur(x,y);
  long count=0;
  int xr = (int)Math.sqrt(x);
  int yr = (int)Math.sqrt(y);
  count = array[yr]-array[xr];
  if(xr*xr == (int)x){
  if(checkPalindrom(xr) &&  checkPalindrom(xr*xr)){
  count++;
  }
  }
  
  
  System.out.println ("Case #"+i++ +": "+count);
  }
  in.close();
}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  e.printStackTrace();
}
}

public static void fillArray(){
long count = 0;
for(int i=1;i<array.length;i++){
long j = (long)i;
if(!checkPalindrom(j))
array[i]= count;
else if(!checkPalindrom(j*j))
array[i]= count;
else{
count++;
array[i]= count;
}
}
}

    public static Boolean checkPalindrom(long n)
    {
     String normal = String.valueOf(n);
    //reverse the letters in string
    int length = normal.length(); //length of string
    StringBuffer result = new StringBuffer(length);
    int i;
    for(i = length - 1; i>=0; i--)
    {
    result.append(normal.charAt(i));
    }
    long r=0;
    try{
     r= Long.valueOf(result.toString());
    }catch(Exception e){
     e.printStackTrace();
     System.out.println("i:"+i+"\tr:"+r+"\tn:"+n);
    }

    if(r==n)
     return true;
    return false;
    }


    
    
}



Google Code Jam Problem B. Lawnmower


Below is my solution for Problem B: Lawnmower

Well I qualified for Google Code Jam 2013 with only 60 points.






Google Code Jam Problem A. Tic-Tac-Toe-Tomek


Below is my solution for Problem A: Tic-Tac-Toe-Tomek

Well I qualified for Google Code Jam 2013 with only 60 points.





Sunday, 25 November 2012

write a function to check given string matches with given pattern





public class isMatchingWithWildcard {
public static void main(String [] args){
String pattern = "*abc*def*.doc*";
String str = "adsfabcxyzdefgh.do1docx";
if(isMatching(str, pattern))
System.out.print("Matching");
else
System.out.print("Not Matching");
}
public static Boolean isMatching(String str, String pattern){
int l = pattern.length();
if(pattern.lastIndexOf("*") == l-1)
pattern= (String) pattern.subSequence(0, l-1);
if(pattern.charAt(0) == '*')
pattern= (String) pattern.subSequence(1, pattern.length());
pattern = pattern.replace("*", "__");
String [] patternArray = pattern.split("__");
String sorttenString =str;
for(String aPattern:patternArray){
String [] temp = sorttenString.split(aPattern);
if(temp.length==1 && (aPattern == patternArray[patternArray.length-1] 
&& !sorttenString.toLowerCase().contains(aPattern.toLowerCase())))
return false;
str.substring(temp[0].length()+aPattern.length());
}
return true;
}
}

Let me know your thoughts

Finding Pythagorean triplets in an array.



//   WORKING code in java with  time O(n^2)

import java.util.Arrays;
public class PythagoreanTripletsInArray {
public static double [] getSquare(String [] args){
double [] array = new double[args.length];
for(int i=0;i<args.length;i++){
double j=Integer.parseInt(args[i]);
array[i]=j*j;
}
return array;
}
public static void main(String [] args ){
double [] array =getSquare(args);
Arrays.sort(array);
Boolean flage = true;
for(double aInt : array){
for(int i=0,j=(array.length-1);i<j;){
double sum = array[i]+array[j]; 
if(aInt == sum){
System.out.println(Math.sqrt(aInt)+", "+Math.sqrt(array[i])+", "+Math.sqrt(array[j]));
flage = false;
break;
}
else if(aInt > sum)
i++;
else
j--;
}
}
if(flage)
System.out.print("No such solution exist");
}
}

Let me know your thoughts

Tuesday, 2 October 2012

Re-arrange the odd/even to odd/even places





#include<stdio.h>

void Swap (int A[], int i, int j)
{
    int temp = A[i];
    A[i]=A[j];
    A[j]=temp;
}

int main()
{
    int A[] = {3,1,4,5,7,6,10,8};
    int i=0,j=1,n;
    n=sizeof(A)/sizeof(int);
    while(i<n)
    {
        while(i<n && A[i]%2 == 0)
            i+=2;
        while(j<n && A[j]%2 == 1)
            j+=2;
        if(i<n && j<n)
            Swap(A,i,j);
    }// end of while

    for(i=0;i<n;i++)
        printf(" %d",A[i]);

return 0;
}

Let me know your thoughts