Sunteți pe pagina 1din 8

4th jan

1) count all the possible paths from top left to bottom right of a mXn matrix with the constraints
that from each cell you can either move only to right or down

/ Returns count of possible paths to reach cell at row number m and column
// number n from the topmost leftmost cell (cell at 1, 1)
int numberOfPaths(int m, int n)
{
// Create a 2D table to store results of subproblems
int count[m][n];

// Count of paths to reach any cell in first column is 1


for (int i = 0; i < m; i++)
count[i][0] = 1;

// Count of paths to reach any cell in first column is 1


for (int j = 0; j < n; j++)
count[0][j] = 1;

// Calculate count of paths for other cells in bottom-up manner using


// the recursive solution
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)

// By uncommenting the last part the code calculatest he total


// possible paths if the diagonal Movements are allowed
count[i][j] = count[i-1][j] + count[i][j-1]; //+ count[i-1][j-
1];

}
return count[m-1][n-1];
}

b) http://betterexplained.com/articles/navigate-a-grid-using-
combinations-and-permutations/

2) find repeating and missing element

int findrepeating(int arr[],int n){

std::map<int,int> mp;

int i;

for(i=0;i<n;i++){

if(mp[arr[i]]==1){

return arr[i];

}
else{

mp[arr[i]]=1;

return 0;

int repandmiss(int arr[],int n){

int rep=findrepeating( arr, n);

int sum=0,i;

for(i=0;i<n;i++){

sum=sum+arr[i];

sum=sum-rep;

int sum2=n*(n+1)/2;

int mis=sum2-sum;

cout<<"repeating is "<<rep<<"missing is "<<mis<<"\n";

return 1;

3) array in wave form

http://www.geeksforgeeks.org/sort-array-wave-form-2/

4) dp partition array to maximise sum

// Returns the minimum value of the difference of the two sets.


int findMin(int arr[], int n)
{
// Calculate sum of all elements
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];

// Create an array to store results of subproblems


bool dp[n+1][sum+1];

// Initialize first column as true. 0 sum is possible


// with all elements.
for (int i = 0; i <= n; i++)
dp[i][0] = true;

// Initialize top row, except dp[0][0], as false. With


// 0 elements, no other sum except 0 is possible
for (int i = 1; i <= sum; i++)
dp[0][i] = false;

// Fill the partition table in bottom up manner


for (int i=1; i<=n; i++)
{
for (int j=1; j<=sum; j++)
{
// If i'th element is excluded
dp[i][j] = dp[i-1][j];

// If i'th element is included


if (arr[i-1] <= j)
dp[i][j] |= dp[i-1][j-arr[i-1]];
}
}

// Initialize difference of two sums.


int diff = INT_MAX;

// Find the largest j such that dp[n][j]


// is true where j loops from sum/2 t0 0
for (int j=sum/2; j>=0; j--)
{
// Find the
if (dp[n][j] == true)
{
diff = sum-2*j;
break;
}
}
return diff;
}

5) Do above print all sub arrays


6) Print all combinations of balanced parenthesis

void _printParenthesis(int pos, int n, int open, int close);

/* Wrapper over _printParenthesis()*/


void printParenthesis(int n)
{
if(n > 0)
_printParenthesis(0, n, 0, 0);
return;
}

void _printParenthesis(int pos, int n, int open, int close)


{
static char str[MAX_SIZE];

if(close == n)
{
printf("%s \n", str);
return;
}
else
{
if(open > close) {
str[pos] = '}';
_printParenthesis(pos+1, n, open, close+1);
}
if(open < n) {
str[pos] = '{';
_printParenthesis(pos+1, n, open+1, close);
}
}
}

7) All permutations of list of words

void wordpermute(vector<string> vec,int l,int r){

int i;

if(l==r){

for(i=0;i<vec.size();i++){

cout<<vec[i]<<" ";

cout<<"\n";

else{

for(i=l;i<=r;i++){

// swapvec(vec,l,i);

iter_swap(vec.begin() + l, vec.begin() + i);

wordpermute(vec,l+1,r);

iter_swap(vec.begin() + l, vec.begin() + i);

8) Sub word list

vector<string> getsub(vector<string> vec,int start,int n){


vector<string> sub;

cout<<"in sub"<<"j is "<<start<<"n is "<<n<<"\n";

int i,j=0;

for(i=start;i<n;i++){

sub.push_back(vec[i]);

j++;

cout<<"sub is\n";

for(i=0;i<sub.size();i++){

cout<<sub[i]<<"\n";

return sub;

void wordcombination(vector<string> vec,int n){

int i,j,f;

map<vector<string>,int> mp;

for(i=0;i<n;i++){

for(j=0;j<=i;j++){

cout<<"wordcomb i is"<<i<<"j is "<<j<<"\n";

vector<string> sub=getsub(vec,j,i+1);

auto search=mp.find(sub);

if(search==mp.end()){

mp[sub]++;

printVecStr(sub);

}
A head to tail ordering of strings is the one in which the last
letter of the previous word is the same as the first letter of the
following word. For eg. ‘Geekforgeeks’can be followed by
‘ software’.

map<vector<string>,int> wordpermute(map<vector<string>,int>
mp,vector<string> vec,int l,int r){
int i;
if(l==r){
for(i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
cout<<"\n";
mp[vec]=1;
}
else{
for(i=l;i<=r;i++){
// swapvec(vec,l,i);
iter_swap(vec.begin() + l, vec.begin() + i);
mp=wordpermute(mp,vec,l+1,r);
iter_swap(vec.begin() + l, vec.begin() + i);
}
}
return mp;
}
int checkheadtail(vector<string> vec){
int i,k=0;
for(i=0;i<vec.size()-1;i++){
string str=vec[i];
string str2=vec[i+1];
int l=str.length();
if(str[l-1]==str2[0]){
k=1;
}
else{
k=0;
return 0;
}

}
return k;
}

9) Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car’s gas
tank, when full, holds enough gas to travel ‘d’ miles, and his map gives the distances
between gas stations on his route. The professor wishes to make as few stops as possible
along the way. The problem is to find an efficient method by which Professor Midas can
determine at which gas stations he should stop, and prove that the strategy adopted yields
an optimal solution. [2]
http://people.cis.ksu.edu/~subbu/Papers/Minimum%20Stops.pdf
i ¬1 distance_to_travel ¬ Dist [i] while (Gi+1 != Gn) { distance_to_travel ¬ Dist [i] /*
Greedy loop */ while ((distance_to_travel £ d) AND (Gi+2 exists)) { destination =
Gi+1 distance_to_travel ¬ distance_to_travel + Dist [i+1] i ¬i + 1 } Put ‘destination’ in
Solution set S {} }

S-ar putea să vă placă și