Thursday 27 October 2016

Jumping On Clouds

Jumping On Clouds

Emma is playing a new mobile game involving  clouds numbered from  to . A player initially starts out on cloud , and they must jump to cloud . In each step, she can jump from any cloud  to cloud  or cloud .
There are two types of clouds, ordinary clouds and thunderclouds. The game ends if Emma jumps onto a thundercloud, but if she reaches the last cloud (i.e., ), she wins the game!







Can you find the minimum number of jumps Emma must make to win the game? It is guaranteed that clouds  and are ordinary-clouds and it is always possible to win the game.
Input Format
The first line contains an integer,  (the total number of clouds). 
The second line contains  space-separated binary integers describing clouds .
  • If , the  cloud is an ordinary cloud.
  • If , the  cloud is a thundercloud.
Constraints
Output Format
Print the minimum number of jumps needed to win the game.
Sample Input 0
7
0 0 1 0 0 1 0
Sample Output 0
4
Sample Input 1
6
0 0 0 0 1 0
Sample Output 1
3
Explanation
Sample Case 0: 
Because  and  in our input are both , Emma must avoid  and . Bearing this in mind, she can win the game with a minimum of  jumps:









Sample Case 1: 
The only thundercloud to avoid is . Emma can win the game in  jumps:






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int c[] = new int[n];
        for(int c_i=0; c_i < n; c_i++){
            c[c_i] = in.nextInt();
        }
         int count=0;
      int i=0;
        while(i<n-1)
               {
            if(i+2<n&&c[i+2]==0)
             { i=i+2;
              count++;}
            else
             {   i++;
               
            count++;
           }
        }
       System.out.println(count);
    }
}

Wednesday 26 October 2016



   SOCKS MERCHANT 


A clothing store has a pile of  loose socks where each sock  is labeled with an integer, , denoting its color. The seller wants to sell as many socks as possible, but his customers will only buy them in matching pairs. Two socks,  and , are a single matching pair if .
The first line contains an integer, , denoting the number of socks.
The second line contains  space-separated integers describing the respective values of .
Sample Input
9
40 20 20 40 40 30 50 40 20
Sample Output
3

explanation :
pairs are :
20-20;
40-40;
40-40;



public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int c[] = new int[n];
        for(int c_i=0; c_i < n; c_i++){
            c[c_i] = in.nextInt();
        }
        int count=0;
        for(int i=0; i<n ; i++)
            {
                for(int j=i+1; j < n && c[i]!=-1;j++){
            if(c[i]==c[j])
               { count++;
                c[i]=-1;
                c[j]=-1;
                break;
            }
                }
        }
        System.out.println(count);
    }
}