Friday 7 February 2014

Shop Associations -Code Chef Problem

The shop are categorised as type I. The shop from which he has not brought are categorised as type II. Each shop has initially its own association. One assosciation will merge with another assosciation to form a new assosciation if the shops in both assosciations are of same type (i.e either of type I or type II) and there exists any two shops in the merging assosciations(one in each), distance between whome is 1m.help him to maximise the the sum of the prices by shops of asscosciation of type I by helping him choosing the shops to pick for buying the book. You should provide him the desired sum.

Solution:

#include<stdio.h>
#include<iostream>
using namespace std;
int max(int x, int y)
{ return (y > x)? y : x; }

int maxSubArraySum(int *a, int size)
{
   int max_so_far = a[0], i;
   int curr_max = a[0];

   for (i = 1; i < size; i++)
   {
        curr_max = max(a[i], curr_max+a[i]);
        max_so_far = max(max_so_far, curr_max);
   }
   return max_so_far;
}


int main()
{
   int T = 0;
   cin>>T;
   int *a;
   int n = 0;
   
   int max_sum = 0 ;
   for(int i = 0 ; i < T ; i++)
   {
           cin>>n;
           a = new int[n];
           for(int j = 0 ; j < n ; j++)
           {
                   cin>>a[j];
           }
       max_sum = maxSubArraySum(a, n);
   cout<<max_sum<<endl;
   delete [] a;
   }
  
   return 0;
}



Special Digit - Code Chef Problem

You have a number N and you want to calculate how many divisors of N are special.
A number is said to be special if it is possible to remove some digits from it to get a number having 3, 5 or 6 only.For exemple number 38597 is special since it is posible to remove digits 8, 9, 7 to get 35. You can remove some digits but not all digits. You can remove digits from left, right or middle.

Solution:
#include<iostream> #include<cmath> using namespace std; int check(int n) { if( n < 1 ) { return 0; } if( ((n % 10) == 3) || ((n % 10) == 5) || ((n % 10) == 6) ) { return 1; } else { n = n / 10 ; return (check(n)); } } int main() { int T = 0; long long N = 0; cin>>T; long long temp = 0 ; long long count = 0; for(int i = 0 ; i < T ; i++) { cin>>N; count = 0 ; long long j = 1; for(; j <sqrt(N); j++) { if( (N % j) == 0) { count = count + check(j) + check(N/j); } } if(j*j == N) count = count + check(j); cout<<count<<endl; } return 0; }