Archives

gravatar

Blog # 60 : Thief's Proposal !!


There are 5 thieves. They are all numbered one to five. They have 500 pieces of gold which need to be distributed among themselves. First the number 1 distributes it and after that a voting is done : if he gets majority votes his distribution is carried out else he is killed, if the votes are equal, then also distribution is done his way. If he is killed then next numbered person does the distribution and this goes on till number 5. Each thief values his life more than the gold.
So how does the first thief distribute gold to get maximum and to not get killed also ?

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 59 : Will you meet your friend?


Two friends decide to meet at a cafe between 11 am and 12 pm i.e. one hour. They also decide that the one who reaches before the other waits for 5 minutes and if the other does not arrive he leaves. What is the probability of their meeting ?

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 58 : Run Length Encoding (RLE)


Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. Count of 1 is not indicated. Write a program to find RLE of given string.

Eg.
input = "aabbbcddeef" RLE = "a2b3cd2e2f"
input = "aaabbcddeeeef" RLE = "a3b2cd2e4f"

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 57 : Finding the black sheep [:D]


You have 12 balls. All of them are identical except one, which is either heavier or lighter than the rest. You have a simple two-armed scale, and are permitted three weighing. Can you identify the odd ball, and determine whether it is heavier or lighter than the rest ?

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 56 : Next equi-set digit Number


Given an integer N. Devise an algorithm to find out the smallest integer greater than N having the same number of bits set as that of N. (When a bit is 1, we say it is set)


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.





Courtesy: Vibhaj

gravatar

Blog # 55 : Premutations of every subset


Given a array of characters of size N. Devise an algorithm to generate all possible permutations of given size K (K <= N).

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.




Courtesy: Vibhaj

gravatar

Blog # 54 : Swapping; Not so easy


Give a method to swap two numbers without using additional memory.



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 53 : How to multiply by 7?


Give a fast method to multiply a number by 7.



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 52 : Searching in 2D array

Given 2D array with integers sorted both horizontally and vertically. Devise an efficient algorithm to search for key in the 2D array. What is the complexity ? 

Eg. 

    1 4 7 13 

    2 5 9 15 

    3 6 10 16

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.




Courtesy: Vibhaj

gravatar

Blog # 51 : Minimum & Maximum + Stack


Assume you have infinite memory. Design an algorithm using stacks that can find the minimum and maximum at any point while reading an infinite sequence of numbers in least possible time.

Eg. 5, 9, 2, 35, 23, 1, ...
After 3rd element, the algorithm would tell min = 2, max = 9
After 6th element, algorithm would tell min = 1, max = 35




Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.




Courtesy: Vibhaj

gravatar

Blog # 50 : Thousand Doors and Final combination

A high school has a strange principal. On the first day, he has his students perform an odd opening day ceremony:There are one thousand lockers and one thousand students in the school. The principal asks the first student to go to every locker and open it. Then he has the second student go to every second locker and close it. The third goes to every third locker and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth locker, and so on. After the process is completed with the thousandth student, how many lockers are open ?

gravatar

Blog # 49 : Weighing machine & Denominations of weights

You are given a weighing machine. Find the minimum number of denominations of weights you need to measure any N kgs given that you may use the denominations on any side of the weighing machine.

gravatar

Blog # 48 : Determine the non-existence


Devise an algorithm to find whether a given number N is of the form 2^x + 2^y (^ means exponentiation, x and y are positive).



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.





Courtesy: Vibhaj

gravatar

Blog # 47 : 2^x + 2^y


Devise an algorithm to find whether a given number N is of the form 2^x + 2^y (^ means exponentiation, x and y are positive).



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj, Gurpreet & Ankit

gravatar

Blog # 46 : Find out the matching sock

Fint out the matching socks: Michael have ten pairs of black socks, eight pairs of white socks and seven pairs of green socks. Everything is mixed in a draw. As there is no light he were not able to identify the color of the socks. How many of the socks did he want to take to match one pair ?




p.s. - For Remarks please hit the question name once, given in the question itself. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 45 : Blind Search

A blind search (also called an uninformed search) is a search that has no information about its domain. The only thing that a blind search can do is distinguish a non-goal state from a goal state.

For more read here about Blind Search

Code:
 
#include<iostream>
#include<cstdlib>
#include<map>
#define MAX 50
using namespace std;

map <int,int> mp;

void printpath(int start)
{
 if (mp[start]==-1)
 {
  cout<<start<<endl;
  return;
 }
 else
 {
  cout<<start<<"  ";
  printpath(mp[start]);
 } 
}
int main()
{ 
 int n,adj[MAX][MAX],v[MAX];
 int rnd,start,end,temp;
 bool solve=false;
 cout<<"enter the no of nodes : ";
 cin>>n;
 cout<<"enter the adjacency matrix : ";
 for(int i=0;i<n;i++)
  for(int j=0;j<n;j++)
   cin>>adj[i][j];
 for(int i=0;i<n;i++)
  v[i]=0;
 cout<<"enter the start and end node: ";
 cin>>start>>end;
 temp=start;
 v[start]=1;
 mp[end]=-1;
 while(!solve)
 {
  rnd=random()%n;
  //cout<<"reached here";
  //cout<<"  "<<rnd<<endl; 
  if(rnd==end && adj[start][rnd]==1)
  {
   mp[start]=rnd;
   solve=true;
  }
  else
  {
   if(adj[start][rnd]==1 && v[rnd]==0)
   {
    mp[start]=rnd;
    //cout<<mp[start]<<endl;
    v[rnd]=1;
    start=rnd;
   }
   else
    continue;
  }
 } 
 printpath(temp);
 return 0;
}

gravatar

Blog # 44 : Hill Climbing (A local Search)

In computer science, hill climbing is a mathematical optimization technique which belongs to the family of local search. It is an iterative algorithm that starts with an arbitrary solution to a problem, then attempts to find a better solution by incrementally changing a single element of the solution. If the change produces a better solution, an incremental change is made to the new solution, repeating until no further improvements can be found.

For example, hill climbing can be applied to the Traveling Salesman Problem. It is easy to find an initial solution that visits all the cities but will be very poor compared to the optimal solution. The algorithm starts with such a solution and makes small improvements to it, such as switching the order in which two cities are visited. Eventually, a much shorter route is likely to be obtained.

Read more about Hill Climbing Problem

Code:
#include<iostream>
#include<cstdio>
using namespace std;
int calcCost(int arr[],int N){
 int c=0;
 for(int i=0;i<N;i++){
  for(int j=i+1;j<N;j++) if(arr[j]<arr[i]) c++;
 }
 return c;
}

void swap(int arr[],int i,int j){
 int tmp=arr[i];
 arr[i]=arr[j];
 arr[j]=tmp;
}

int main(){
 int N;
 scanf("%d",&N);
 int arr[N];
 for(int i=0;i<N;i++) scanf("%d",&arr[i]);
 int bestCost=calcCost(arr,N),newCost,swaps=0;;
 while(bestCost>0){
  for(int i=0;i<N-1;i++){
   swap(arr,i,i+1);
   newCost=calcCost(arr,N);
   if(bestCost>newCost){
    printf("After swap %d: \n",++swaps);
    for(int i=0;i<N;i++) printf("%d ",arr[i]);
    printf("\n");
    bestCost=newCost;
   }
   else swap(arr,i,i+1);
  }
 }
 printf("Final Ans\n");
 for(int i=0;i<N;i++) printf("%d ",arr[i]);
 printf("\n");
 return 0;
}

gravatar

Blog # 43 : Minimax (Minmax) Search

Minimax (sometimes minmax) is a decision rule used in decision theory, game theory, statistics and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario. Alternatively, it can be thought of as maximizing the minimum gain (maximin). Originally formulated for two-player zero-sum game theory, covering both the cases where players take alternate moves and those where they make simultaneous moves, it has also been extended to more complex games and to general decision making in the presence of uncertainty.

For more please refer Minimax

Code:


#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;

vector <int> vec;

int large(int a,int b)
{
 if(a>b)
  return a;
 else
  return b;
}

int small(int a,int b)
{
 if(a>b)
  return b;
 else 
  return a;
}

void max_min(int p,int q,int *max,int *min)
{
 if(p==q)
 {
  *max=vec[p];
  *min=vec[q];
 }
 
 else if(p==(q-1))
 {
  if(vec[p]>vec[q])
  {
   *max=vec[p];
   *min=vec[q];
  }
  else
  {
   *max=vec[q];
   *min=vec[p];
  }
 }
 
 else
 {
  int m=(p+q)/2;
  int max1,max2,min1,min2;
  
  max_min(p,m,&max1,&min1);
  max_min(m+1,q,&max2,&min2);
  *max=large(max1,max2);
  *min=small(min1,min2);
 }
}

int main()
{
 int a,n=0;
 int min=0,max=0;
 
 cout<<"enter the elements (enter -1 to quit) :  ";
 while(1)
 {
  cin>>a;
  if(a==-1)
   break;
  else
  {
   vec.push_back(a);
   n++;
  }
 }
 max_min(0,n-1,&max,&min);
 
 cout<<"MAX = "<<max<<"\t"<<"MIN = "<<min<<endl;
 
 return 0;
}