Dpynes
BAN USERI told my friends about this problem. They insisted on a brute force answer. I was the first to get it down. Enjoy the worlds least efficient code. Please show a test case that doesn't work for this. Very very sloppy code here.
#include <array>
using namespace std;
bool sumsToZero(int sum[10]){
int addup=0;
int grid[10] = {0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<10;i++){
grid[i] = 0;
for(int j=0;j<10;j++){
addup=0;
if(i!=j && grid[j]==0){
grid[j]=1;}
else if(i!=j && grid[j]==1){
grid[j]=0;}
for(int x=0; x<10; x++){
if(grid[x]==0){addup +=sum[x];}
if(grid[x]==1){addup -=sum[x];}
}
if (addup == 0){return true;}
if(i>0){
grid[i-1] = 1;}
}
}
return false;
}
int main(){
int x[10]={10,5,2,3,10,4,7,3,5,15};
std::cout<<sumsToZero(x)<<std::endl;
system("pause");
return 0;
}
This is a pretty straight forward question. Let me know if you find anything wrong with this code. C++. Should be pretty easy to following along too.
int x[] = {1,2,3,4,2,3,4,5,6,7,5,4,4,5,4,5,6,7,8,8,6,5,4,3,2,3,4,5,6,7,8,8,3,4,};
std::map<int,int> m;
for(auto i : x){
m[i]+=1;
}
for(int i = 0; i<m.size();i++){
if(m[i]%2==1){
std::cout<<i<<" appears an odd amount of the time in the set."<<std::endl;
}
}
Using Recursion:
This is a pretty straightforward way to get to the solution.
I am exploiting two very important facts. #1 It is sorted zeros than ones. #2 You'll need to study it figure it out.
I ran a few different test cases and it passed all.
This is my first post so please prove me wrong.
int zero_count_recursive(int array[], int low, int high){
int mid = (low+high)/2;
if(array[mid] == 0){
low = mid;
return zero_count_recursive(array, mid, high);
}
else {
if(array[mid-1] == 0){
return mid;}
else{
high = mid;
return zero_count_recursive(array,low, high);
}
}
}
..If you can't figure some ghetto way to solve this problem using iteration...I can't help you. Sorry.
- Dpynes January 09, 2015
Yeah this question is stupid and then I thought well maybe the interview is seeing if you would test that the first box doesn't contain a pearl so test the last box. ....but it explicitly states in the problem that the first pearl is at 1.....Lame question.
- Dpynes January 12, 2015Funny to see people overthinking the algorithm though. ;-)