Time spent here:

Monday, 20 June 2016

PRINT PATTERN NO:3

Problem statement:
Print the pattern.

Input:
9

Output:


code:

#include <stdio.h>

int main()
{
  int n,i,j;
  scanf("%d",&n);
  int k=(n*2)-1;
   int l=1,r=k,mid=(k/2)+1,m1=mid,m2=mid;
  
  for(i=1;i<=n;i++)
   {    for(j=1;j<=k;j++)
          {
              if(j>=l && j<=r)
                 {
                     
                     if(i<(n/2)+1)
                      {
                          if(i!=0 && (j>m1 && j<m2))
                           {
                               printf(" ");
                           }               
                          else
                            printf("*");
                      }
                      else
                        printf("*");
                 }
               else 
                 printf(" ");
          }
   l=l+1;
   r=r-1;
   if(i<mid)
    {m2++; m1--;}
   printf("\n");
  }  
    return 0;
}


Sunday, 19 June 2016

Find the smallest number where the product of the digits is equal to N.

Problem statement:
Given a number N. Find the smallest number where the product of the digits is equal to N.

E.g.:
if N is 8 then output should be 18 because 1*8=8 and not 24 (2*4) or 42 (4*2)

Input:
8

Output:
18

CODE:

#include <stdio.h>

int main()
{
   int n,i,j,arr[100],k=0;
   scanf("%d",&n);
   if(n<10)
     printf("%d",10+n);
   else
    {
        for(i=9;i>1;i--)
        {
            while(n%i==0)
            {
            
                 n=n/i;
                 arr[k]=i;
                 k++;
            
            }
     
           
        }
    if(n>10)
          printf("No Such Number");
    for(j=k-1;j>=0;j--)
            printf("%d",arr[j]);
     
    }
    return 0;

}

OUTPUT:

REVERSE STRING IN SPECIAL ORDER

Problem statement:
Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’
to ‘Z’), reverse the string in a way that special characters are not affected.

E.g.:
Input:
str = "a,b$c"

Output:
str = "c,b$a"

Note that $ and, are not moved anywhere.
Only sub-sequence "abc" is reversed

Input:
str = "Ab,c,de!$"

Output:
str = "ed,c,bA!$"

CODE:

#include <stdio.h>
#include<string.h>
int main()
{
    char a[100];
    scanf("%s",a);
    int i=0,j=strlen(a)-1;
    while(1)
        {
        char temp;
        if(i>j)
         { break;}
    if((a[i] >= 65 && a[i]<91  || a[i]>=97 && a[i]<=122) && (a[j] >= 65 && a[j]<91
                    || a[j]>=97 &&a[j]<=122))
          {
              temp=a[i];
              a[i]=a[j];
              a[j]=temp;
              j--;i++;
        }
        if(a[i] < 65 || a[i]>91  && a[i]<97 || a[i]>=122)
           i++;
        if ((a[j] < 65 || a[j]>91  && a[j]<97 || a[j]>=122))
          j--;
     
    
    }
    printf("%s",a);
    return 0;

}


Saturday, 18 June 2016

PRINT THE SUM OF DIAGONALS

Problem statement:
Print the sum of diagonals.
 
E.g.:
Consider the following matrix for n=3
7 8 9
6 1 2
5 4 3
the sum of the diagonal elements is 25 
 
Input: 
5
 
Output:
101

Code:
#include <stdio.h>
int main()
{
    int n,sum,count,itr,i,val;
    scanf("%d",&n);
    if(n%2==0)
    {
                val=1;itr=2; sum=0;count=0;
    }
     else
     {
       val=2;itr=3; sum=1;count=1;
     }
     
      
      
        while(itr <= n)
        {
            for(i=0;i<4;i++)
              {
                 count=count+val;
                 sum=sum+count;
              }
        itr=itr+2;val+=2;
        }
        printf("%d\n",sum);
   
    return 0;
}

OUTPUT:


 

FACTOR FUN.......!!!!1

Problem statement:
To find the factors of the numbers given in an array and to sort the numbers in descending order
according to the factors present in it.

Input:
Given array : 8, 2, 3, 12, 16

Output:
12, 16, 8, 2, 3

Code:
#include <stdio.h>
int main()
{
  int i,j,list[5],count[5],c=0;
  for(i=0;i<5;i++)
    {
        scanf("%d",&list[i]);
        for(j=1;j<list[i]/2;j++)
            {
                if(list[i]%j==0)
                  c++;
            }
            count[i]=c;
            c=0;
       
    }
    for(i=0;i<5;i++)
     {
         for(j=0;j<5;j++)
         {
             if(count[j]<count[j+1])
             {
                 int temp=count[j];
                 count[j]=count[j+1];
                 count[j+1]=temp;
                 temp=list[j];
                 list[j]=list[j+1];
                 list[j+1]=temp;
             }
         }
     }
     for(i=0;i<5;i++)
       printf("%d ",list[i]);
    return 0;

}

PRINT THE PATTERN

Problem statement:
Print the pattern.

Input:
PROGRAM

Output:

P         M
 R      A
   O  R
     G
  O    R
 R       A
P          M

Code:

#include <stdio.h>
#include<string.h>
int main()
{
  char string[100];
   int left,right,length,i,j;
  scanf("%s",string);
  left=0;length=strlen(string);right=length-1;
  for(i=0;i<length;i++)
  {
    
      for(j=0;j<length;j++)
      {
         if(j==left && left==right)
            { printf("%c",string[j]); break;}
          else if(j==left || j==right)
              printf("%c",string[j]);
         else
              printf(" ");
      }
   printf("\n");
   if(i<length/2)
    { left++;right--;}
   else
    { left--;right++;}
  }
  
    return 0;

}

Friday, 17 June 2016

TAXI PROBLEM


PROBLEM STATEMENT:

 Design a Call taxi booking application

-There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s.

-The are 6 points(A,B,C,D,E,F)

-All the points are in a straight line, and each point is 15kms away from the adjacent points.



-It takes 60 mins to travel from one point to another

-Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers.

-For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc.
-All taxi’s are initially stationed at A.

-When a customer books a Taxi, a free taxi at that point is allocated

-If no free taxi is available at that point, a free taxi at the nearest point is allocated.

-If two taxi’s are free at the same point, one with lower earning is allocated

-Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer.

-If no taxi is free at that time, booking is rejected

Design modules for

1)    Call taxi booking
Input 1:
Customer ID: 1
Pickup Point: A
Drop Point: B
Pickup Time: 9

Output 1:
Taxi can be allotted.
Taxi-1 is allotted

Input 2:
Customer ID: 2
Pickup Point: B
Drop Point: D
Pickup Time: 9

Output 1:
Taxi can be allotted.
Taxi-2 is allotted

(Note: Since Taxi-1 would have completed its journey when second booking is done, so Taxi-2 from nearest point A which is free is allocated)

Input 3:
Customer ID: 3
Pickup Point: B
Drop Point: C
Pickup Time: 12

Output 1:
Taxi can be allotted.
Taxi-1 is allotted



2) Display the Taxi details


Taxi No:    Total Earnings:
BookingID    CustomerID    From    To    PickupTime    DropTime    Amount
  
Output:
Taxi-1    Total Earnings: Rs. 400

1     1     A    B    9    10    200
3    3    B    C    12    13    200

Taxi-2 Total Earnings: Rs. 350
2    2    B    D    9    11    350



These were just sample inputs. It should work for any input that they give.

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
struct taxi_Booking{
    int booking_id[10];
    int customer_id[10];
    char source[10],destination[10];
    int pick_time[10],drop_time[10];
    int wages[10],total_fare;
    char location;
    int current_time,num_of_bookings;
  //  void (*booking)(struct taxi_Booking *);
    //void (*allocation)(struct taxi_Booking *);
};
int c_id=1,b_id=1,p_time;
char p_point,d_point;

void display(struct taxi_Booking *taxi){
    int i,j;
    printf("\nBooking id    Customer id        From         To         P_time        D_time        Amount\n\n");   
    for(j=1;j<=4;j++){
        if(taxi[j].total_fare){
            printf("Taxi No.: %d     Total Earnings: %d\n",j,taxi[j].total_fare);
            for(i=1;i<=taxi[j].num_of_bookings;i++){
                printf("\n%d    %d    %c    %c    %d    %d    %d",taxi[j].booking_id[i],taxi[j].customer_id[i],taxi[j].source[i],
                taxi[j].destination[i],taxi[j].pick_time[i],taxi[j].drop_time[i],taxi[j].wages[i]);
                printf("\n");
            }
            printf("\n");
        }
    }   
}
void alloc_fun(struct taxi_Booking *taxi,int booking_pos){
            taxi[booking_pos].num_of_bookings++;
            int n=taxi[booking_pos].num_of_bookings;
            taxi[booking_pos].booking_id[n]=b_id-1;
            taxi[booking_pos].customer_id[n]=c_id-1;
            taxi[booking_pos].source[n]=p_point;   
            taxi[booking_pos].destination[n]=d_point;
            taxi[booking_pos].pick_time[n]=p_time;
            taxi[booking_pos].drop_time[n]=p_time+abs(p_point-d_point);
            taxi[booking_pos].wages[n]=(((abs(p_point-d_point)*15)-5)*10)+100;
            taxi[booking_pos].total_fare+=taxi[booking_pos].wages[n];
            taxi[booking_pos].location=d_point;
            taxi[booking_pos].current_time=taxi[booking_pos].drop_time[n];
           
           
}
void initiate(struct taxi_Booking *taxi){
    int i;
    for(i=1;i<=4;i++){
        taxi[i].total_fare=0;
        taxi[i].location='A';
        taxi[i].num_of_bookings=0;
    }
}

void booking_fun(struct taxi_Booking *taxi){
      int min_loc=INT_MAX,min_fare=INT_MAX,cnt=0,booking_pos,i,alloc;
        printf("\nInput: %d",b_id++);
      printf("\nCustomer_id: %d",c_id++);     
      printf("\nPickup Point:");
      scanf("\n%c",&p_point);
      printf("Drop Point:");
      scanf("\n%c",&d_point);
      printf("Pick_Time:");
      scanf("%d",&p_time);

      for(i=1;i<=4;i++){
            //if(taxi[i].current_time<=p_time){
                if(abs(p_point-taxi[i].location)<=min_loc && (abs(p_point-taxi[i].location)+taxi[i].current_time)<=p_time){
                    min_loc=abs(p_point-taxi[i].location);
                    if(taxi[i].total_fare<min_fare){
                            min_fare=taxi[i].total_fare;
                            booking_pos=i;
                    }
                }
            //}
            else{
                cnt++;
            }
      }
      if(cnt<4){
            printf("\nTaxi can be alloted\nTaxi-%d is allocated",booking_pos);
            //taxi[0].allocation=alloc_fun;
            alloc_fun(taxi,booking_pos);
           
      }
      else{
            printf("\nBooking is rejected");
            b_id--;
            c_id--;
      }       
}

int main(){
    struct taxi_Booking taxi[4];
    int c;
    initiate(taxi);
    printf("\n<<<<<<<<<<Welcome To Taxi Booking>>>>>>>>>>>>\n");
    printf("\nTaxi will be booked only for customers to travel between the below stations.\n");
    printf("\n-->Nearby stations are A,B,C,D,E and F.\n");
    printf("-->Taxi fare will be charged Rs.100 for first 5kms and for rest Rs.10 per km will be charged.\n");
    printf("-->Your booking will be rejected if taxies are not available at that time.\n");
    printf("\nNOTE: Station names are CASE_SENSITIVE,So please enter the valid station names.\n");
    printf("\nPick time should be in 24 hours format. It should be greater then 0\n\n");
    while(1){
      printf("\n\nPress 1 to continue or 2 to display taxi details or 0 to Exit:");
      scanf("%d",&c);
      if(c==0){
            exit(0);
      }
      else if(c==1){
              //taxi[0].booking=booking_fun;
            booking_fun(taxi);
      }
      else
            display(taxi);
    }
    return 0;
}

Wednesday, 15 June 2016

Print the pattern.

Input:
5
 
Output:
123454321
1234*4321
123***321
12*****21
1*******1
 
code:
 
#include <stdio.h>

int main()
{
   int n,i,j,k,l,m;
   scanf("%d",&n);
   m=2*(n);
   l=n;k=n;
   for(j=1;j<=n;j++){
   for(i=1;i<m;i++)
   {
      if(j==1){
       if(i<=n)
         printf("%d",i);
       else
        printf("%d",m%i);
      }
      else
      {
          if(i>l && i<k || l==k)
            printf("*");
          else
            {
                 if(i<=n)
                    printf("%d",i);
                 else
                     printf("%d",m%i);
            }
      }
   }
   printf("\n");
   l--;k++;
   }
    return 0;
}
 
sample testcase:
 
Output

1234321
123*321
12***21
1*****1
Input

4
 
view this at:https://code.hackerearth.com/97e078w 

Thursday, 2 June 2016

WEEK OF CODE-18 -Ghosts Problem

Do you believe in ghosts? Citizens of Byteland do. They want to estimate the population size of ghosts in their country.

There are  towns in Byteland. Every town consists of  streets. Each street has  houses. Finally, there are apartments in each house. All the towns, streets, houses and apartments are numbered starting from . So, every apartment has a corresponding address described by  numbers.

A ghost lives in a particular apartment only if all the conditions below are true:

The difference between the town number and the street number is divisible by .
The sum of the street number and the house number is divisible by .
The product of the town number and the house number is divisible by .
The greatest common divisor of the town number and the apartment number is .
You are given the numbers , , , and . How many ghosts live in Byteland?

Input Format

The first line contains  space-separated integers: , , , .

Constraints


Output Format

Output the answer to the problem on the first line.

Sample Input

4 4 4 4
Sample Output

8
Explanation

Here are the addresses of all the ghosts:

1 1 4 1
1 1 4 2
1 1 4 3
1 1 4 4
4 1 4 1
4 1 4 3
4 4 1 1
4 4 1 3

CODE:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
     
      Scanner sc=new Scanner(System.in);
       int a,b,c,d,i,j,k,l,m,count=0;
       a=sc.nextInt();
       b=sc.nextInt();
        c=sc.nextInt();
        d=sc.nextInt();
        for(i=1;i<=a;i++)
         {
             for(j=1;j<=b;j++)
                 {
                  for(k=1;k<=c;k++)
                      {
                       for(l=1;l<=d;l++)
                           {
                               if((i-j)%3==0 && (j+k)%5==0 && (i*k)%4==0)
                                {
                                 
                                           int r=0, a1, b1;

        a1 = (i > l) ? i : l;

        b1 = (i < l) ? i: l;


        r = b1;

        while(a1 % b1 != 0)

        {

            r = a1 % b1;

            a1 = b1;

            b1 = r;

        }
                                   if(r==1)
                                     count++;
                               }
                       }
                  }
             }
        }  
       System.out.println(count);
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   
    }

}
Mika is a very rich guy. He has  bags of apples. Each bag contains a positive integer number of apples, the bag with number  containing  apples. Mika decided that the time has come to sell some of his bags. When selling a bag, Mika is automatically selling each apple inside the bag, but he can't take an apple out of the bag and sell it separately. To make this more interesting, Mika added yet another extra condition: he wants the total number of sold apples to be divisible by .

So Mika is wondering what is the maximum number of apples that can be sold? Help him calculate that number.

Input Format

The first line contains integer  (), the number of Mika's bags of apples.

The second line contains  numbers  (), where  is the number of apples in the bag ().

Output Format

In a single line, print the largest total number of apples which can be sold.

Sample Input 1

4
2 2 1 2
Sample Output 1

6
Sample Input 2

5
3 6 9 9 3
Sample Output 2

30
Explanation

In the first sample, the best option is to sell three bags, each containing  apples. In total, Mika will sell  apples.

In the second sample, the number of apples in every bag is divisible by . So, the total sum of apples is divisible by and Mika can sell all his bags.

CODE:

#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 1010;
int n, s, m, a[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s += a[i];
    if (!(s % 3)) m = s;
    for (int i = 1; i <= n; i++) if (!((s - a[i]) % 3)) m = max(m, s - a[i]);
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++) if (!((s - a[i] - a[j]) % 3)) m = max(m, s - a[i] - a[j]);
    printf("%d", m);
    return 0;
}

Project Euler #48: Self powers

The series,

Find the last ten digits of the series,


Note You do not need to print leading zeros. See sample.
Input Format
Input contains an integer

Output Format
Print the answer corresponding to the test case.

Constraints

Sample Input

10
Sample Output

405071317

PYTHON IMPLEMENTATION:

L = int(raw_input())  
d = 10    # last d digits of series
s = sum(pow(n, n,10**d) for n in range(1, L+1))

print  (s% 10**d)

Project Euler #6: Sum square difference

The sum of the squares of the first ten natural numbers is, . The square of the sum of the first ten natural numbers is, . Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .
eg:consider n as 10.
1^2+2^2+3^2+......+10^2=385
(1+2+3+4+...+10)^2=3025
sum difference = 3025-385=2640

Find the difference between the sum of the squares of the first  natural numbers and the square of the sum.

Input Format 
First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .

Output Format 
Print the required answer for each test case.

Constraints 

Sample Input

2
3
10
Sample Output

22
2640

c implementation:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

   long long int i,j,n,t;
    scanf("%lld",&t);
    while(t--)
    {
      scanf("%lld",&n);
      long long int sq=((n*(n+1))*((2*n)+1))/6;
      long long int sum=((n*(n+1)))/2;
      printf("%lld\n",sum*sum-sq);  
    }
    return 0;
}

Project Euler #2: Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

By considering the terms in the Fibonacci sequence whose values do not exceed N, find the sum of the even-valued terms.

Input Format
First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .

Output Format
Print the required answer for each test case.

Constraints


Sample Input

2
10
100
Sample Output

10
44

source code:

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 sc=new Scanner(System.in);
        int t=sc.nextInt();
       while(t!=0)
       {
          BigInteger n=sc.nextBigInteger();
           BigInteger f= BigInteger.valueOf(0);
            BigInteger s= BigInteger.valueOf(1);
            BigInteger sum= BigInteger.valueOf(0);
            BigInteger ans= BigInteger.valueOf(0);
           do{
              if(sum.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(0)))
                   ans=ans.add(sum);
               sum=f.add(s);
               f=s;
               s=sum;
              }while(sum.compareTo(n)!=1);
           System.out.println(ans);
           t=t-1;
       }  
       
       
    }
}

Reverse a 32 bit given number.

Reverse a 32 bit given number.
Input:
The first line of input consists number of the test cases. Each test case contains a single 32 bit integer.

Output:
Print the reverse of integer.

Constraints:
1<=T<=100
0<=x<=4294967295

Example:

Input:
2
1
5

Output:
2147483648
2684354560

Explanation:
In first cases

00000000000000000000000000000001 =1
10000000000000000000000000000000 =2147483648

code:

#include <stdio.h>
#include<math.h>
int main() {
  long long int n,t,sum,count;
  scanf("%lld",&t);
  while(t--)
  {
      scanf("%lld",&n);sum=0;count=1;
      while(n)
      {
       
          if(n%2)
            sum=sum+pow(2,32-count);
          n=n/2; count++;  
      }
  printf("%lld\n",sum);
     
  }
return 0;
}