This code is supposed to calculate the frequency of maximum number in an array I.E the number of times the highest number in the array has occured unfortunately this code does not display any output:-
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int birthdayCakeCandles(int n, int a[]){
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j+1]> max){
max = a[j+1];
j++;
}
}
int seen[n];
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < n;j++)
if(a[j] == a[i] && a[i] == max)
count += 1;
seen[j] = 1;
}
}
return count;
}
int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
cin >> a[i];
}
int result = birthdayCakeCandles(n, a);
cout << result << endl;
return 0;
}
Your program never stops, because your maximum finding loop is for n > 0 endless. Your loop in birthdayCakeCandles should be changed to:
while (j < n)
{
if (a[j + 1] > max)
{
max = a[j + 1];
}
j++;
}
Also consider using more readable coding style and please read this.
In addition to the bug found by vasek, you made at least another mistake in the (overcomplicated) following loops, where you are trying to count the occurences of the maximum value.
// I've kept OP's indentation on purpose...
int seen[n]; // <-- Variable Length Arrays are not standard in C++
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < n;j++)
if(a[j] == a[i] && a[i] == max)
count += 1;
seen[j] = 1; // <-- misleading indentation, this is always executed
// no matter what the condition is
}
}
While all you need to do, once you have found the maximum value, is:
int count = 0;
for( int i = 0; i < n; ++i ) {
if( a[i] == max )
++count;
}
As a matter of fact (unless you want to create a function operating on an array for other reasons), you don't need any array (or std::vector) at all to complete your assignment. This code will perform the same task:
#include <iostream>
#include <limits>
int main()
{
int n;
std::cin >> n;
int x,
max = std::numeric_limits<int>::min();
int count = 0;
for ( int i = 0;
i < n && std::cin >> x;
++i )
{
if ( x >= max )
{
if ( x > max )
{
max = x;
count = 1;
}
else
{
++count;
}
}
}
std::cout << count << '\n';
}
Related
#include<iostream>
using namespace std;
int main(){
int n=5;
int i = 2;
for (i; i <= n; i++)
// for all num to n
{
int j = 2;
bool divide = false;
for (j; j <= n - 1; j++)
// for checking each num
{
if (i % j == 0)
{
divide = true;
break;
}
}
if (divide == false)
{
cout << i << " ";
}
}
return 0;
}
my Q is that
//please tell me why it is not working
//it is expected to give ans 2,3,5 which it is not giving why???
maybe I found the issue.
I think that the problem here is:
for (j; j <= n - 1; j++)
Here you did j<=n-1;
So to fix this just do:
for(j; j < i; j++){
//this should fix
So everything should look like this:
#include<iostream>
using namespace std;
int main() {
int n = 5;
int i = 2;
//check prime numbers starting from i and max n using for loop
for (i = 2; i <= n; i++) {
bool divide = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
divide = true;
break;
}
}
if (!divide) {
//!divide is equal to divide=false
cout << i << " ";
}
}
}
I am trying to learn programming and in our school we have exercises which are automatically checked by a bot. The time limit is 1 second and the memory limit is 1024 mb.
I've tried sorting the array in an ascending order and then multiplicating the 2 highest numbers but that was too slow(my sorting algorithm could be slow so if possible suggest a sorting algorithm.)
This is the fastest way that I've been able to do:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int Maksimumas(int n, int X[]);
ofstream fr("U1rez.txt");
ifstream fd("U1.txt");
int main()
{
int n, A[100000], B[100000], maxi=0;
fd >> n;
for (int i = 0; i < n; i++) {
fd >> A[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
B[j] = A[i] * A[j];
}
maxi = Maksimumas(n, B);
A[i] = B[maxi];
}
maxi = Maksimumas(n, A);
fr << A[maxi];
fr.close();
return 0;
}
int Maksimumas(int n, int X[])
{
int maxi = 0;
for (int i = 0; i < n; i++) {
if (X[maxi] < X[i]) {
maxi = i;
}
}
return maxi;
}
n is the size of the array for anyone wondering.
You don't need to sort the entire array - you just need the two largest positive numbers and the two smallest negative numbers. Everything in between is inconsequential.
Instead, you can go over all the input and keep track of the two largest positive numbers and two smallest negative numbers.; At the end of the iteration, multiply each pair (if found), and compare the results.
// fd is opened like the original question, n is the number of elements to read
// that part is omitted for brevity
int max = -1;
int preMax = -1;
int min = 1;
int preMin = 1;
int temp;
for (int i = 0; i < n; i++) {
fd >> temp;
if (temp > preMax) {
if (temp > max) {
preMax = max;
max = temp;
} else {
preMax = temp;
}
} else if (temp < preMin) {
if (temp < min) {
preMin = min;
min = temp;
} else {
preMin = temp;
}
}
}
int result = -1;
if (preMax >= 0) {
result = preMax * max;
}
if (preMin <= 0) {
int tempResult = preMin * min;
if (tempResult > result) {
result = tempResult;
}
}
return result;
Getting error in this code, even though it passed the basic test cases. But still, it gives the wrong answer.
Cannot find the test case where it fails, Solution to Codechef Count of maximum problem. I think a part of the code is making it fail for a certain test case(s).
Can anyone help me find the error in this code, please?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int k;
cin >> k;
for (int j = 0; j < k; j++)
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int maxCount = 0;
int number;
int index = 0;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int l = 0; l < n; l++)
{
if (a[i] == a[l])
{
count++;
if (count > maxCount)
{
maxCount = count;
index = i;
}
if (count == maxCount) {
(a[i] > a[index]) ? number = a[index] : number = a[i];
}
}
}
}
cout << number << " " << maxCount << endl;
}
}
Your number variable is redundant. You need to track theindex of the elements in the array.
That means, change this line
(a[i] > a[index]) ? number = a[index] : number = a[i];
to
(a[i] > a[index]) ? index = index : index = i;
and print
std::cout << a[index] << " " << maxCount << std::endl;
The code should display the number of times maximum number in the array has occurred so for the following input (3,2,1,3) the output should be '2' as '3' is the maximum number and occurs twice. I wanted to use functions, I know there is an easier way to solve it but i just want to know the problem in my code:-
#include<iostream>
using namespace std;
int frequency(int n, int a[]) {
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j+1]> max){
max = a[j+1];
}
j++;
}
int seen[n];
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < n;j++)
if(a[j] == a[i] && a[j] == max)
count += 1;
seen[j] = 1;
}
}
return count;
}
int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
cin >> a[i];
}
int result = frequency(n, a);
cout << result << endl;
return 0;
}
For starters the C++ Standard does not support Variable Length Arrays. So instead of an array you should use some container as for example std::vector<int>.
Moreover the program has undefined behavior because at least in this loop
while(j<n){
if(a[j+1]> max){
max = a[j+1];
}
j++;
}
there is an attempt to access memory beyond the array in the expression a[j+1].
And at last the function always returns 0 because the variable count in the outermost scope of the function is set to zero and is never changed.
A general approach can be written using iterators.
For example
#include <iostream>
#include <iterator>
#include <vector>
template <typename InputIterator>
size_t count_maximum_value( InputIterator first, InputIterator last )
{
size_t count = 0;
if ( first != last )
{
++count;
typename std::iterator_traits<InputIterator>::value_type max = *first;
while ( ++first != last )
{
if ( max < *first )
{
max = *first;
count = 1;
}
else if ( not ( *first < max ) )
{
++count;
}
}
}
return count;
}
int main()
{
size_t n = 0;
std::cout << "Enter the number of integers: ";
std::cin >> n;
if ( n )
{
std::vector<int> v( n );
std::cout << "Enter " << n << " integers: ";
for ( size_t i = 0; i < n; i++ ) std::cin >> v[i];
std::cout << "The maximum value is encountered "
<< count_maximum_value( v.begin(), v.end() )
<< " time(s)"
<< std::endl;
}
return 0;
}
The program output might look the following way
Enter the number of integers: 4
Enter 4 integers: 3 2 1 3
The maximum value is encountered 2 time(s)
You are comparing a[j+1] with max. I suggest you use a[j]. You seem to be skipping the first number in your input.
Start with count = 1 instead of count = 0 and delete the redeclaration of count in the loop.
You're starting with 0, then you add 1 when you find the second entry that matches it. That's why you're off by one.
Check the comments
#include<iostream>
using namespace std;
int frequency(int n, int a[]) {
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j]> max){ // 1. j+1 crosses lenth of array
max = a[j];
}
j++;
}
int seen[n];
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
//int count = 0; //2. re declaration and you are not returning this
//for(int j = i; j < n;j++) //3. No need of extra loop it make count extra because you are starting at j=i
//if(a[j] == a[i] && a[j] == max)
if(a[i] == max)
count += 1;
seen[j] = 1;
}
}
return count;
}
int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
cin >> a[i];
}
int result = frequency(n, a);
cout << result << endl;
return 0;
}
I cannot understand/think of a case where my code fails to give correct output.
Link to the problem: http://www.spoj.pl/problems/MKBUDGET/
The problem clearly has a DP solution. I am posting my solution below:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<vector <int> > opt;
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++) //num of months
for(int j = A[i]; j <= max_a; j++) //num of workers for ith month >=A[i] and <= max_a
{
opt[i][j] = opt[i-1][A[i-1]] + j*sal + (A[i] > A[i-1] ? (A[i]-A[i-1])*hire : (A[i-1] - A[i])*fire);
for(int k = A[i-1]; k <= max_a; k++)
opt[i][j] = min(opt[i][j], opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
int ans(vector<int> A, int n, int max_a)
{
int ret = opt[n-1][A[n-1]];
for(int i = A[n-1]; i <= max_a; i++)
ret = min (ret, opt[n-1][i]);
return ret;
}
int main()
{
vector<int> A;
int n, hire, fire, sal,max_a, c = 1;
while(1)
{
cin >> n;
if(n == 0)
break;
A.clear();
opt.clear();
max_a = 0;
cin >> hire >> sal >> fire;
A.resize(n);
for(int i = 0; i < n; i++)
{cin >> A[i];
max_a = max(max_a,A[i]);
}
opt.resize(n);
for(int i = 0; i < n; i++)
opt[i].resize(max_a + 2);
compute_opt(A,n,hire,fire,sal,max_a);
cout << "Case " << c << ", cost = $" << ans(A,n,max_a) << endl;
c++;
}
return 0;
}
I am getting correct answers for the two sample test cases but I get a WA when I submit. Any help ?
OK, so your problem is that you disallow the case where you hire any number of employees between A[i] and A[i - 1]. Maybe it's a good idea to fire some unneeded employees, but not all. That's why you get WA. I modified your code and got it accepted:
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
// Fill all disallowed entries with infinity
for (int i = 0; i < A[0]; ++i)
opt[0][i] = 1000000000;
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++)
for(int j = 0; j <= max_a; j++)
{
// No need for special case handling,
//just check all previous numbers of employees
opt[i][j] = 1000000000;
if (A[i] > j) continue;
for(int k = 0; k <= max_a; k++)
opt[i][j] = min(opt[i][j],
opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
By the way, there's a "greedier" solution than the one you have that does not depend on the number of employees being small (so that the table can be allocated).