David Billa
BAN USER- 0of 0 votes
AnswersN players with weights are given as input. divide them into 2 teams with equal weights. while dividing , ensure that difference in number of players in each team is minimum. output the difference in number of players in 2 teams.
- David Billa in India
input weights - 3 2 1
output - 1| Report Duplicate | Flag | PURGE
Athena Health Software Engineer / Developer Algorithm - 0of 0 votes
Answersa string contains oly 3 characters in a string -'a','b','c' . if in the string, 2 different chars occur together they are replaced with the third char. operation is repeated till no reduction is possible. the string must be evaluated from left to right always.
- David Billa in India
input - abbc
output- 1| Report Duplicate | Flag | PURGE
Athena Health Software Engineer / Developer Algorithm - 0of 0 votes
AnswersSpiral traversal of a two dimensional array of m rows and n columns. For example, a 2d array with elements a[][3] = { 1,2,3, 4,5,6 ,7,8,9} , the output must be 1 2 3 6 9 8 7 4 5.
- David Billa in India| Report Duplicate | Flag | PURGE
Chronus Software Engineer / Developer Algorithm
I am evaluating from left to right here. For this given string, unfortunately evaluation from right side also reduces the same way :)
- David Billa February 01, 2013the algo is wrong. it will just output x.
- David Billa February 01, 2013@Sudhindra,
one's complement of 9 is 6.
Implemented Alex's algorithm. Code works. Thanks.
#include<iostream>
#include<math.h>
using namespace std;
//one's complement
bool check_power_2(int n)
{
if( (n & (n-1))== 0)
return true;
else
return false;
}
int next_power(int n)
{
int c =0;
while(n>0)
{
n = n >> 1;
c++;
}
return pow(2,c);
}
int main()
{
int n;
cin >> n;
if (n == 0)
cout << 1 <<endl;
else if(check_power_2(n))
cout << n-1 << endl;
else
cout << next_power(n) - (n+1) << endl;
return 0;
}
- David Billa February 01, 2013cccccb
cccca
cccb
cca
cb
a
output - 1
The trick is to have another 2d array with same m rows and rows to record whether the element has been visited. The C++ code for the same,
#include<iostream>
using namespace std;
bool allvisited(int visited[][3])
{
//bool visited = false;
for(int i=0 ; i<3 ; i++)
{
for(int j=0 ; j <3; j++)
if(visited[i][j] == 0)
return false;
}
return true;
}
int main()
{
int mat[3][3] = { 1,2,3,
4,5,6,
7,8,9 };
int visited[3][3] = {0};
int currenti,currentj,tempi,tempj,lefttoright,toptobottom;
int m=3 , n=3;
currenti=currentj=lefttoright=toptobottom = tempi = tempj =0;
while(!allvisited(visited))
{
if(!lefttoright)
{ tempj = currentj;
while(currentj < n)
{
if(visited[currenti][currentj] == 0)
{
visited[currenti][currentj] = 1;
cout << mat[currenti][currentj] << " ";
tempj = currentj;
}
currentj ++ ;
}
currentj = tempj ; //resetting
}
else
{
tempj = currentj;
while(currentj >= 0)
{
if(visited[currenti][currentj] == 0)
{
visited[currenti][currentj] = 1;
cout << mat[currenti][currentj] << " ";
tempj = currentj;
}
currentj -- ;
}
currentj = tempj ; //resetting
}
if(!toptobottom)
{ tempi = currenti;
while(currenti < m)
{
if(visited[currenti][currentj] == 0)
{
visited[currenti][currentj] = 1;
cout << mat[currenti][currentj] << " ";
tempi = currenti;
}
currenti ++ ;
}
currenti = tempi ; //resetting
}
else
{ tempi = currenti;
while(currenti >= 0)
{
if(visited[currenti][currentj] == 0)
{
visited[currenti][currentj] = 1;
cout << mat[currenti][currentj] << " ";
tempi = currenti;
}
currenti -- ;
}
currenti = tempi ; //resetting
}
lefttoright = !lefttoright;
toptobottom = !toptobottom;
}
cout << endl << "All visited\n";
return 0;
}
- David Billa January 26, 2013
RepMariaHobbs, Consultant at Adobe
Hi, I am Maria Hobbs from NewYork.Teach career development courses for designated areas. Develop, evaluate and revise course materials ...
You mean like a multi-map . I am not understanding the question here.
- David Billa February 01, 2013