Count of Maximum , Passing Test cases but WA - c++

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;

Related

is there other way to fill in array in C++ without vector

So I have array A and B, two of them contain random numbers and I need to write in the C array initially even numbers of A and B and then odd. I have made this wtih vector but I wonder if there is other way to do it like in Javascript there are methods like .unshift(), .push() etc
#include<iostream>
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
vector<int>C;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 == 0)
{
C.push_back(B[i]);
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C.push_back(A[i]);
}
if (B[i] % 2 != 0)
{
C.push_back(B[i]);
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
I would suggest interleaving A and B initially:
for (int i = 0; i < n; i += 1)
{
C.push_back(A[i]);
C.push_back(B[i]);
}
And then partitioning C into even and odd elements:
std::stable_partition(C.begin(), C.end(), [](int i) { return i % 2 == 0; });
vector::push_back is the simplest way to have a collection that grows as you add things to the end.
Since you have fixed size for A and B, you could make them primitive arrays instead, which is what you have done. But for C you don't know how long it will be, so a collection that has a changeable size is appropriate.
You can use std::array, if you know the size you need in compile time. You can then add using an iterator.
#include<vector>
using namespace std;
int main() {
const int n = 4;
int A[n];
int B[n];
std::array<int, n+n>C; // <-- here
auto C_it = C.begin(); // <-- here
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
*C_it++ = A[i]; // <-- here
}
if (B[i] % 2 == 0)
{
*C_it++ = B[i];
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
*C_it++ = A[i];
}
if (B[i] % 2 != 0)
{
*C_it++ = B[i];
}
}
cout << endl;
for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
}
Alternatively if you want to be more safe you can hold the next unwritten index and access elements with C.at(last++) = A[i], which checks for out-of-bounds and throws an exception instead of UB.
well you don't to change much.
first of declare C array as int C[n+n]; and declare a variable for incrementing through c array as int j=0;
and in if statements of loops do this C[j]=A[i]; j++; for first if and C[j]=B[i]; j++; for the second if statements
int main() {
const int n = 4;
int A[n];
int B[n];
int C[n+n];
int j=0;
for (int i = 0; i < n; i++)
{
A[i] = rand() % 10;
cout << A[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
B[i] = rand() % 30;
cout << B[i] << " ";
}
for (int i = 0; i < n; i += 1)
{
if (A[i] % 2 == 0)
{
C[j]=A[i];
j++;
}
if(B[i]%2==0){
C[j]=B[i];
j++;
}
}
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
C[j]=A[i];
j++;
}
if (B[i] % 2 != 0)
{
C[j]=B[i];
j++;
}
}
j=0;
cout << endl;
for (int i = 0; i < C[].lenght(); i++)
cout << C[i] << " ";
}

Frequency of arrays in C++

I'm trying to write a code which works like this:
it first gets an int from the user which presents the number of array indexes the user would like to own (n), then the code creates an array with n number of indexes. It then has to print the frequency of every entered number.
Running my below code results in a segmentation fault. Any solution will in advance be appreciated.
My code:
int main()
{
int n, index;
cin>>n;
int ar[n];
int freq[100]={0};
for (int i = 0; i < n; i++)
{
cin>>ar[i];
}
for (int i = 0; i < 100; i++)
{
index=ar[i];
freq[index]++;
}
for (int i = 0; i < 100; i++)
{
cout<<freq[i]<<' ';
}
return 0;
}
NOTE: 1<=n<=100
I would do something like this:
int main ()
{
int n, input;
while (n > 100 || n < 1)
cin >> n;
// indexes 0 - 99
int freq[100] = { 0 };
for (int i = 0; i < n; i++)
{
cout << "number: ";
cin >> input;
if (input > 0 && input <= 100)
freq[input - 1]++;
}
for (int i = 0; i < n; i++)
if (freq[i] != 0)
cout << i + 1 << "-->" << freq[i] << &endl;
return 0;
}
try it here

Smallest composite number in array

I am totally new to programming and I am bit stuck on my code. I wrote code where I wanted to find smallest composite number in array(using only low-level arrays). When I wrote down like size of array 3 and enter 1 2 77, than it throws out random 16. Can you explain why is this happening and perhaps give some solution how to fix this.
#include<iostream>
using namespace std;
int fun(int n)
{
int arr[n];
int mini = arr[0];
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
{
for (int j = 2; j < arr[i]; j++)
{
if (arr[i] % j == 0)
{
if (mini > arr[i])
{
mini = arr[i];
}
else
{
mini = mini;
}
break;
}
}
}
return mini;
}
int main()
{
int n;
cout << "Size of array: ";
cin >> n;
cout << "Write " << n << " numbers: " << fun(n) << endl;
return 0;
}

How do I put any value X after all the minimums in an array?

If I enter an array , at first the code finds the minimums then I want to put zeroes after all the minimums . For example
given an array = 1,1,3,1,1
As we see 1s are the minimum so the result should be = 1,0,1,0,3,1,0,1,0
CODE
#include <pch.h>
#include <iostream>
int main()
{
int min = 10000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] > min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the
i++; // `for loop k` to be precise)
n++;
}
std::cout << array[i] << ", 0";
}
return 0;
}
But my answer doen't put zeroes exactly after minimums
There are few issues in your code, first of all your min is wrong. I have fixed your code with comments on fixes I have made. Please take a look :
#include "stdafx.h"
#include <iostream>
int main()
{
int min = 10000;
bool found = 0;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min) //< instead of >
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min)
{
for (int k = n; k > i; k--)
{
array[k] = array[k - 1];
}
array[i + 1] = 0;
i++; //increment i here because you don't want to consider 0 that you have just added above.
n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
}
}
//print the array separately
for (int i = 0; i < n; i++)
{
std::cout << array[i];
if (i != n - 1)
{
std::cout << ",";
}
}
return 0;
}
The first issue was in the calculation of min: < instead of >.
Another problem if that you are modifyng the paramers iand ninside the loop. This is rather dangerous and implies to be very cautious.
Another issue was that it should be i++; n++; instead of i--,n--;
Here is the code:
// #include <pch.h>
#include <iostream>
int main()
{
int min = 1000000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the)
i++;
n++;
}
}
for (int i = 0; i < n; i++) {
std::cout << array[i] << " ";
}
std::cout << "\n";
return 0;
}

This Code is giving the wrong output can't figure out where I made a mistake

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;
}