DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Programming Questions
*****************************************************
#Post#: 18--------------------------------------------------
Marketing Problem. Solve it and code it.
By: kranthipls Date: August 13, 2012, 11:37 pm
---------------------------------------------------------
Problem Statement
You work for a very large company that markets many
different products. In some cases, one product you market
competes with another. To help deal with this situation you have
split the intended consumers into two groups, namely Adults and
Teenagers. If your company markets 2 products that compete with
each other, selling one to Adults and the other to Teenagers
will help maximize profits. Given a list of the products that
compete with each other, you are going to determine whether all
can be marketed such that no pair of competing products are both
sold to Teenagers or both sold to Adults. If such an arrangement
is not feasible your method will return -1. Otherwise, it should
return the number of possible ways of marketing all of the
products.
The products will be given in a String[] compete whose kth
element describes product k. The kth element will be a
single-space delimited list of integers. These integers will
refer to the products that the kth product competes with. For
example:
compete = {"1 4",
"2",
"3",
"0",
""}
The example above shows product 0 competes with 1 and 4, product
1 competes with 2, product 2 competes with 3, and product 3
competes with 0. Note, competition is symmetric so product 1
competing with product 2 means product 2 competes with product 1
as well.
Ways to market:
1) 0 to Teenagers, 1 to Adults, 2 to Teenagers, 3 to Adults, and
4 to Adults
2) 0 to Adults, 1 to Teenagers, 2 to Adults, 3 to Teenagers, and
4 to Teenagers
Your method would return 2.
Definition
Class:
Method:
Parameters:
Returns:
Method signature:
(be sure your method is public)
Constraints
-
-
inclusive.
-
sequence of integers such that:
All of the integers are unique.
Each integer contains no extra leading zeros.
Each integer is between 0 and k-1 inclusive where k is the
number of elements in compete.
-
-
-
the ith element of compete.
Examples
0)
{"1 4","2","3","0",""}
Returns: 2
The example from above.
1)
{"1","2","0"}
Returns: -1
Product 0 cannot be marketed with product 1 or 2. Product 1
cannot be marketed with product 2. There is no way to achieve a
viable marketing scheme.
2)
{"1","2","3","0","0 5","1"}
Returns: 2
3)
{"","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","",""}
Returns: 1073741824
4)
{"1","2","3","0","5","6","4"}
Returns: -1
#Post#: 33--------------------------------------------------
Re: Marketing Problem. Solve it and code it.
By: ravu Date: August 18, 2012, 1:41 am
---------------------------------------------------------
The solution boils down in finding the number of components in a
graph. The answer is nothing but 2 ^(no. of components). But, if
there is an odd cyle in a component, then we should return -1.
So, in finding a component we have to do depth first search and
while doing so we should check whether there is an odd cycle.
The following is the code which does that-
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <string.h>
#include <stdlib.h>
//#include <cstlib.h>
#define MAXROWS 50
#define MAXCOLS 50
//Tenager = 1
//Adults = 2
#define tr(container,it) for(typeof(container.begin()) it =
container.begin();it!=container.end();it++)
using namespace std;
class Marketing
{
public:
int NumWays(vector<string> input);
int assignlabel(int vertex,int label);
void printAdjMatrix(int size);
Marketing();
private:
//define a adjacency matrix of size MAXROWSXMAXCOLS
int adj[MAXROWS][MAXCOLS];
int labels[MAXROWS];
};
void Marketing::printAdjMatrix(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
cout<<"value at "<<i<<" "<<j<<"is "<<adj[i][j];
}
cout<<endl;
}
}
Marketing::Marketing()
{
int i,j;
for(i=0;i<MAXROWS;i++)
for(j=0;j<MAXCOLS;j++)
adj[i][j] = 0;
for(i=0;i<MAXROWS;i++)
labels[i] = 0;
}
int Marketing::NumWays(vector<string> input)
{
int GraphSize = input.size();
int index = 0;
tr(input,it)
{
string word;
int vertex;
istringstream stream(*it);
while(stream >> word)
{
cout<<"In the method NumWays"<<word<<"and
index"<<index<<endl;
if(word!="")
{
vertex = atoi(word.c_str());
adj[index][vertex] = 1;
adj[vertex][index] = 1;
word.clear();
}
}
index ++;
stream.clear();
}//Adjacency matrix is formed
//Now traverse
int i,flag,NumWays=1;
cout<<"Graph size is "<<GraphSize<<endl;
for(i=0;i<GraphSize;i++)
{
cout<<"labels["<<i<<"]"<<"is "<<labels[i]<<endl;
if(labels[i] == 0)
{
cout<<"I am in the if part"<<endl;
flag = assignlabel(i,1);
if(flag == 0)
return -1;
else
{
NumWays *=2;
}
}
}
return NumWays;
}
int Marketing::assignlabel(int vertex,int label)
{
int other;
if(label == 1)
other = 2;
else
other = 1;
if(labels[vertex] == 0)
{
labels[vertex] = label;
}
else
if(labels[vertex] == other)
return 0;
else
return 1;
//Now push each of the neighbours into a vector
vector<int> Neighbours;
int i;
for(i=0;i<MAXROWS;i++)
{
if(adj[vertex][i] == 1)
Neighbours.push_back(i);
}
int value;
tr(Neighbours,it)
{
value =
if(!value)
break;
}
cout<<"value is "<<value<<endl;
return value;
}
main()
{
Marketing objMarket;
int numways;
vector<string> input;
// input.push_back("1");
input.push_back("1");
input.push_back("2");
//input.push_back("0");
input.push_back("0");
numways = objMarket.NumWays(input);
objMarket.printAdjMatrix(input.size());
cout<<"Num of ways are "<< numways<<endl;
}
#Post#: 60--------------------------------------------------
Re: Marketing Problem. Solve it and code it.
By: kpr29 Date: August 25, 2012, 5:36 am
---------------------------------------------------------
@Ravu: When you submitting some code as answer, it will be nice
if you submit the code that s implemented by you........
@Kranthi: Very large question....... tough to read.......
#Post#: 63--------------------------------------------------
Re: Marketing Problem. Solve it and code it.
By: kranthipls Date: August 26, 2012, 12:24 am
---------------------------------------------------------
It is a large question. But it will improve your thinking
process when you try to solve it.
Actually the question is not so big. More than half of the
question is examples of input and output.
*****************************************************