1. Kingdom of Raigadh is a beautiful country comprising of a lot of small islands of different areas. All the islands are in a straight row. King Ramchandra getting old and has decided to divide the islands among his two sons - Mohan and Shyam. Luckily, the total number of islands is even. He has also decided a few rules for the division of islands:
i) Mohan and Shyam will be given alternate turns to choose the islands.
ii) They can only choose one island at a time from either the beginning or the end of the row of islands.
iii) Once an island is chosen by someone, it cannot be chosen by other person.
Suppose you are Mohan and you are given the first choice. Find out the maximum area you are sure you can pick.
using c ,c++ or java
Tags: computer-science-puzzle, computers-tricks-tips, puzzles
Permalink Reply by Dixit Rajpara on August 8, 2011 at 6:08pm I have written a solution in C but it can only work truly if there are no two island of same area.
int main()
{
int area[100], no, i, sum=0, flag=0;
//flag stands for turn. o is for Mohan and 1 for Shyam.
int *f, *r;
//*f is the front pointer indicating first island and *r is the rear pointer indicating last island
//Defining total number of islands.
label : printf("Enter the even no. of islands present (max 100) : ");
scanf("%d", &no);
if(no%2!=0 || no>100)
{
printf("Error\n\n");
goto label;
}
for(i=0;i<no;i++) //Get the area of all islands.
{
label1 : printf("\nEnter area of Island %d in integer : ", i+1);
scanf("%d", &area[i]);
if(area[i]==0)
{
printf("\nPlease enter non zero value");
goto label1;
}
}
//Setting the pointers.
f=&area[0];
r=&area[no-1];
for(i=0;i<no;i++) //Start dividing the islands.
{
if(flag==0) //Check the turn.
{
if(*f>*r) //Comparing the area of first and last islands left.
{
sum+=*f; //adding the area of chosen island to total sure area.
f++; //Once the islands area is added removing it from list and moving to next island.
}
else //If the area of rear island is more else part get executed.
{
sum+=*r;
r--;
}
flag=1; //Setting Shyam's Turn.
}
else if(flag==1) //Shyam's turn to choose the island.
{
if(*f>*r)
{
f++;
}
else
{
r--;
}
flag=0;
}
}
printf("\n\nMaximum area sure to pick is : %d", sum);
getch();
return 0;
}
© 2012 Created by Power User.
