Saturday 31 December 2016

MARS EXPLORATION


MARS EXPLORATION:

Sami's spaceship crashed on Mars! She sends  sequential SOS messages to Earth for help.
Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, , determine how many letters of Sami's SOS have been changed by radiation.
Input Format
There is one line of input: a single string, .
Note: As the original message is just SOS repeated  times, 's length will be a multiple of .
Constraints
  •  will contain only uppercase English letters.
Output Format
Print the number of letters in Sami's message that were altered by cosmic radiation.
Sample Input 0
SOSSPSSQSSOR
Sample Output 0
3
Sample Input 1
SOSSOT
Sample Output 1
1
Explanation
Sample 0
 = SOSSPSSQSSOR, and signal length . Sami sent  SOS messages (i.e.: ).
Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
We print the number of changed letters, which is .
Sample 1
 = SOSSOT, and signal length . Sami sent  SOS messages (i.e.: ).
Expected Signal: SOSSOS 
Received Signal: SOSSOT
We print the number of changed letters, which is .


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);
        String S = in.next();//taking input string
        int n=S.length();  //getting length
        int r=n/3;  //we divide string by length of "SOS" string i.e. 3.
        String w="SOS";
        String q="SOS";
        for(int i=0;i<r-1;i++)
            {
          q=  q.concat(w);//making original string of "SOS" that was send by her
        }
        int count=0;
       for(int j=0;j<q.length();j++)
           {
            if(S.charAt(j)!=q.charAt(j))//comparing original string and the recieved string .
                count++;   //counting number of errors
         
             
        }
        System.out.println(count);//printing errors
    }
}

Wednesday 28 December 2016

Printing Star ,Slash Using wait(),notify() Method

Source Code:

import java.util.*;
class star  //class containing synchronized methods
{
int ch=0;

synchronized void printstar()
{
try{
                       if(ch==1)//star was printed last
{
wait();
}
System.out.print("*");
ch=1;
notify();
}catch(InterruptedException e){System.out.println("error");
 }
}
synchronized  void printslash()
{
try{ if(ch==0)//slash was printed previously
{
wait();
}

          System.out.print("/");
ch=0;//slash printed
notify();
}catch(InterruptedException e){};


}
}


class printx extends Thread  //for printing star
{
star r,s;
printx(star r)
{
this.r=r;
}

public void run()
{
int f=0;
while(f!=10)
{
r.printstar();
f++;
}
}
}

class print extends Thread///for printing slash
{
star r,s;
print(star r)
{
this.r=r;
}

public void run()
{
int f=0;
while(f!=15)
{
r.printslash();
f++;
}
}
}
class Demo
{
public static void main(String args[])
{
star q=new star();
print  p=new print(q);
printx px=new printx(q);
p.start();//thread 1 starts
px.start();//thread 2 starts
}

}



output:*/*/*/*/*/*/*/*/

explanation:we use synchronization here by using wait() and notify() method .
                     when a star is printed we make ch=1.
                      when a slsh is printed we make ch=0.
                     



Tuesday 1 November 2016

Combinational Lock

Consider a -slot combination lock where each slot contains a dial numbered with the ten sequential decimal integers in the inclusive range from  to . In one operation, you can choose a slot and rotate the dial by one click, either in the positive direction (to increase the displayed number by ) or the negative direction (to decrease the displayed number by ). Note that, due to the cyclical nature of the dial, the next number after  is  and the number before  is ). For example, if the number  is currently displayed on the dial, you can rotate the dial to either  (positive direction) or  (negative direction) in a single operation.
combination lock
Given the initial configuration of numbers in each slot and some desired configuration of numbers, determine the minimum number of operations you must perform to change the lock's slots to the desired configuration.
Input Format
The first line contains  space-separated integers denoting the current slot configuration.
The second line contains  space-separated integers denoting the desired slot configuration.
Constraints
  • Each number in a slot is .
Output Format
Print a single integer denoting the minimum number of moves to change this configuration to the correct one.
Sample Input
1 2 9 5 7
1 3 2 0 7  
Sample Output
9
Explanation
We perform the following operations on each slot:
  • Slot : We rotate the dial  times because this slot is already displaying the desired number (i.e., ).
  • Slot : We rotate the dial  time, changing it from .
  • Slot : We rotate the dial  times, changing it from .
  • Slot : We rotate the dial  times, changing it from .
  • Slot : We rotate the dial  times because this slot is already displaying the desired number (i.e., ).
Finally, we sum the number of operations performed at each slot: . Thus, we print  as our answer.

Source Code:

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int anticlock(int v,int h);

int clockwise(int f,int g);

int main() {

    int count=0,d,f;

    int a[5],b[5],i,j;

    for(i=0;i<5;i++)

        scanf("%d",&a[i]);

    for(i=0;i<5;i++)

        scanf("%d",&b[i]);

    for(i=0;i<5;i++)

        {

       if(a[i]>b[i])

            d=a[i]-b[i];

    else if(a[i]<b[i])

        d=b[i]-a[i];

        else 

        d=0;

if(d>=5)

 f=anticlock(a[i],b[i]);

else

 f=clockwise(a[i],b[i]);

count=count+f;

    }

    printf("%d\n\n",count);

    return 0;

}

int anticlock(int v,int h)

{

    int count=0;

    if(v>h)

    {

        while(v!=h)

        {v=(v+1)%10;

        

            count++;

        }

        return count;

    }

    else

    { while(v!=h)

        {

            h=(h+1)%10;

        

            count++;

        }

    

     return count;

    }

}

int clockwise(int f,int g)

{ int count=0;

    if(f<g)

    {

        while(f!=g)

        {

            f=f+1;

            count++;

        }

        return count;

        }

        else

        {

            while(f!=g)

        {

            g=g+1;

            count++;

        }

        


        return count;

        }

    }