Reputation: 57
Codeforces problem 160A-http://codeforces.com/problemset/problem/160/A
I am getting either 1 or 2 as the output for all test cases.I think this is because the for loop at the end of the solution is only running either once or twice only.I am not able to identify why is the loop ending after atmost 2 iterations.What is wrong with my solution.
My solution:
#include<iostream>
using namespace std;
int total(int x,int y,int array[100]) //Function to calculate sum of xth to yth term of array.
{
int z=0;
for(int a=x;a<=y;a++)
{
z+=array[a];
}
return z;
}
int main()
{
int n,coin[],sum1,sum2,i,j,a,temp,noofcoins;
cin>>n;
for(i=0;i<n;i++)
cin>>coin[i];
for(i=0;i<n;i++) //Bubble sorting array in descending order.
{
for(j=0;j<n-i-1;j++)
{
if(coin[j]<coin[j+1])
{
temp=coin[j];
coin[j]=coin[j+1];
coin[j+1]=temp;
}
}
}
noofcoins=0;
sum1=0;
sum2=0;
for(i=0;((i<n)&&(sum1<=sum2));i++)
{
sum1+=coin[i];
sum2=total(i+1,n,coin);
++noofcoins;
}
cout<<noofcoins;
}
Upvotes: 0
Views: 195
Reputation: 217085
following may help:
int twin(std::vector<int> coins)
{
std::sort(coins.begin(), coins.end());
const int total = std::accumulate(coins.begin(), coins.end(), 0);
int left = total;
int right = 0;
for (size_t i = 0; i != coins.size(); ++i)
{
const int value = coins[coins.size() - 1 - i];
left -= value;
right += value;
if (right > left) {
return 1 + i;
}
}
return 0;
}
Upvotes: 0
Reputation: 618
U have declared i as a global variable and then u are using it at two places and then that's certainly causing the problems
Upvotes: 1
Reputation: 1115
First, avoid using global variables. This declaration belongs to the main body
int n,coin[100],sum1,sum2,i,j,temp,noofcoins;
Once you correct it you'll notice the variable i
used in the function total
is the same as the one used in main
. Just initialize it in the for.
for(int i=x;i<y;i++)
Then, the condition in the final for is wrong. It should be:
for(i=0;((i<n)&&(sum1<=sum2));i++)
Upvotes: 1
Reputation: 15232
You have defined i
as a global variable.
You use i
both in the function total
as in main
where you use i
for the loop where you call total
. So after you called total
, i
has become a different value, and the loop in main
will end.
Upvotes: 1
Reputation: 1
1-there's no reason to declare your variables globally, it's better to declare them in your main() function.
2-Your way of bubble sorting seems to be wrong. The correct way is to sort the bubble until there's no swapping required.
bool flag=flase;
while (!flag){
flag=true;
for(i=0;i<n-1;i++){ //Bubble sorting array in descending order.
if (coin[i]<coin[i+1]){
temp=coin[i];
coin[i]=coin[i+1];
coin[i+1]=temp;
flag=false;
}
}
}
3-your way of passing an array to a function is incorrect. Read the part "Arrays as parameters" on this page for the right way.
4-Finally, you might want to use "new" function to create your array. This way it will not be limited to your maximum array size of 100, and also won't waste memory when your array is smaller than 100.
Read here for learning to use "new" function.
Upvotes: 0