Pages

Wednesday, August 26, 2015

Manny's password Problem

Danny has a possible list of passwords of Manny's facebook account. All passwords length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be in the list.

You have to print the length of Manny's password and it's middle character.

Note : The solution will be unique.

INPUT
The first line of input contains the integer N, the number of possible passwords.
Each of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.

OUTPUT
The first and only line of output must contain the length of the correct password and its central letter.

CONSTRAINTS
1 ≤ N ≤ 100

Sample Input(Plaintext Link)
4
abc
def
feg
cba

Sample Output(Plaintext Link)
3 b



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
import java.io.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
  Scanner scan = new Scanner(System.in);
  int cases = scan.nextInt();
  
  String [] data = new String[cases];
  for(int x=0;x<cases;x++){
   data[x] = scan.next();
  }
  String mid="";
  for(int x=0;x<data.length-1;x++){
   for(int y=1;y<data.length;y++){
    if(palin(data[x]).equals(data[y]))
     mid = data[x].length()+" "+data[x].substring(data[x].length() / 2, data[x].length() / 2 + 1 );
     
   }
  }
  
  System.out.print(mid);
    }
    
    public static String palin(String x){
     String value = "";
     
     value = new StringBuffer(x).reverse().toString();
      
     return value;
    }
}

Tuesday, August 25, 2015

Minimun Shopping Cost (Samu and Shopping Problem)

Samu is in super market and in a mood to do a lot of shopping. She needs to buy shirts, pants and shoes for herself and her family. There are N different shops. Each shop contains all these three items but at different prices. Now Samu has a strategy that she won't buy the same item from the current shop if she had already bought that item from the shop adjacent to the current shop.

Now Samu is confused, because although she want to follow her strategy strictly but at the same time she want to minimize the total money she spends on shopping. Being a good programmer, she asks for your help.

You are provided description about all N shops i.e costs of all three items in each shop. You need to help Samu find minimum money that she needs to spend such that she buys exactly one item from every shop.

Input Format:
First line contain number of test cases T. Each test case in its first line contain N denoting the number of shops in Super Market. Then each of next N lines contains three space separated integers denoting cost of shirts, pants and shoes in that particular shop.

Output Format:
For each test case, output the minimum cost of shopping taking the mentioned conditions into account in a separate line.

Constraints :
1 ≤ T ≤ 10
1 ≤ N ≤ 105
Cost of each item (shirt/pant/shoe) does not exceed 104

Sample Input
1
3
1 50 50
50 50 50
1 50 50

Sample Output
52

Explanation
There are two ways, each one gives 52 as minimum cost. One way is buy shirt from first shop, pant from second shop and shirt from third shop or she can buy shirt from first shop, shoe from second shop and shirt from third shop.

Both ways , cost comes up to 1 + 50 + 1 = 52



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
import java.io.*;
public class HelloWorld{

     public static void main(String []args){
        
        Scanner scan = new Scanner(System.in);
        String line = System.getProperty("line.separator");
        scan.useDelimiter(line);
        
        int cases = scan.nextInt();
        
        int []value = new int[cases];
        
        for(int x=0;x<cases;x++){
            int N = scan.nextInt();
            String []data = new String[N];
            for(int y=0;y<N;y++){
                data[y] = scan.next();
            }
            int temp=0;
            for(int z=0;z<N;z++){
                String []get = data[z].split(" ");
                
                int min = Integer.parseInt(get[0]);
                for(int a=0;a<get.length;a++){
                    if(Integer.parseInt(get[a])<min)
                        min = Integer.parseInt(get[a]);
                }
                
                temp = temp+min;
            }
            
            value[x]= temp;
        }
        
        for(int b=0;b<value.length;b++)
        System.out.println(value[b]);
     }
}

Sunday, August 9, 2015

Square pattern

Input
3   <--- input cases
*
6
$
5
#
4

Output:

******
*     *
*     *
*     *
*     *
******

$$$$$
$      $
$      $
$      $
$$$$$

####
#    #
#    #
####


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
public class squarepattern {
 public static void main(String[] args) {
  Scanner scan = new Scanner (System.in);
  String lineSeparator = System.getProperty("line.separator");
  scan.useDelimiter(lineSeparator);
  int N = 0;
  System.out.println("Enter number of test cases : ");
  N = scan.nextInt();
  String[] huruf = new String[5];
  int[] num = new int[5];
  for (int x=0;x<N;x++){
   huruf[x] = scan.next();
   num[x]=scan.nextInt();}
  for (int x=0;x<N;x++){
   if (num[x] == 1)
    System.out.print(huruf[x]);
   System.out.println();
   if (num[x] == 2){
    for (int row =0;row<num[x];row++){
     for (int col=0;col<num[x];col++){
      System.out.print(huruf[x]);}
     System.out.println();
    }
   }
   if (num[x] > 2){
    for (int row=0;row<num[x];row++){
     for (int col=0;col<num[x];col++){
      if ((row==0) || (row == num[x]-1))
       System.out.print(huruf[x]);
      else{ if ((col==0) || (col==num[x]-1))
        System.out.print(huruf[x]);
       else
        System.out.print(" ");
             }
     }
     System.out.println();
    }
   }
  }
 }//close main
}//close class

Roman to Decimal

Converting Roman String to Decimal

Input: VI
Output : 6



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class RomanToDecimal{
   public static void romanToDecimal(String romanNumber){
       int decimal = 0;
       int lastNumber = 0;
       String romanNumeral = romanNumber.toUpperCase();
       for (int x = romanNumeral.length() - 1; x >= 0 ; x--){
           char convertToDecimal = romanNumeral.charAt(x);
           switch (convertToDecimal){
               case 'M':
                   decimal = processDecimal(1000, lastNumber, decimal);
                   lastNumber = 1000;
                   break;
               case 'D':
                   decimal = processDecimal(500, lastNumber, decimal);
                   lastNumber = 500;
                   break;
               case 'C':
                   decimal = processDecimal(100, lastNumber, decimal);
                   lastNumber = 100;
                   break;
               case 'L':
                   decimal = processDecimal(50, lastNumber, decimal);
                   lastNumber = 50;
                   break;
               case 'X':
                   decimal = processDecimal(10, lastNumber, decimal);
                   lastNumber = 10;
                   break;
               case 'V':
                   decimal = processDecimal(5, lastNumber, decimal);
                   lastNumber = 5;
                   break;
               case 'I':
                   decimal = processDecimal(1, lastNumber, decimal);
                   lastNumber = 1;
                   break;
           }
       }
       System.out.println("Decimal number : "+decimal);
   }
   
   public static int processDecimal(int decimal, int lastNumber, int lastDecimal){
       if (lastNumber > decimal)
           return lastDecimal - decimal;
       else
           return lastDecimal + decimal;
   }
   
   public static void main(java.lang.String args[]){
      String roman = "VI";
      romanToDecimal(roman);
   }
}

Sum of multiple of 3 or 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

1
2
3
4
5
6
7
8
public static void main(String []args){
   int sum = 0;
   for (int x = 0; x < 10; x++) {
      if (x % 3 == 0 || x % 5 == 0)
 sum += i;
   }
   System.out.print(sum);
}

Friday, August 7, 2015

Prime Factor of integers

The prime factors of 13195 are 5, 7, 13 and 29.

Input: 13195

Ouput : 5,7,13,29


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     public static void main(String []args){
         int n = 13195;
         
         isPrimeFactor(n);
         
     }
     
     public static void isPrimeFactor(int n){
         
         int count=0;
         ArrayList arr = new ArrayList();
         for(int x=2;x<=n;x++){
            if(n%x==0){
               arr.add(x);
               
               n = n/x;
               x--;
               count++;      
            }
         }
         
         System.out.print(arr.toString());
     }

Thursday, August 6, 2015

Count two 6's or 7's are next to each other

Given an array of integers, return the number of times that two 6's are next to each other in the array. Also count instances where the second "6" is actually a 7.

function({6, 6, 2}) → 1
function({6, 6, 2, 6}) → 1
function({6, 7, 2, 6}) → 1
function({6, 6, 2, 6, 7}) → 2
function({6, 1}) → 0
function({1, 2, 3, 5, 6}) → 0



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int function667(int[] nums) {
  int count = 0;
  for (int i=0; i < (nums.length-1); i++) {
    if (nums[i] == 6) {
      if (nums[i+1] == 6 || nums[i+1] == 7) {
        count++;
      }
    }
  }
  return count;
}

No triples interger in a row

Given an array of integers, we'll say that a triple is a value appearing 3 times in a row in the array. Return true if the array does not contain any triples.

function({1, 1, 2, 2, 1}) → true
function({1, 1, 2, 2, 2, 1}) → false
function({1, 1, 1, 2, 2, 2, 1}) → false



1
2
3
4
5
6
7
8
public boolean noTriples(int[] nums) {
  for(int x=0;x<nums.length-2;x++){
      if(nums[x]==nums[x+1] && nums[x]==nums[x+2])
        return false;
  }
  
  return true;
}

String Matching 1

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.

Input Output
xxcaazz
xxbaaz
3
abc
abc
2
abc
axc
0


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public int stringMatch(String a, String b) {
  int len = (a.length()<=b.length())? a.length() : b.length();
  int count=0;
  for(int x=0;x<len-1;x++){
    if(a.substring(x,x+2).equals(b.substring(x,x+2)))
      count++;
  }
  
  return count;
}