Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given an array A (may contain duplicates) of N elements and a positive integer K. The task is to count the number of elements which occurs exactly floor(N/K) times in the array.
i tried to compare the starting element of array with next elements in the array and increment count if they are equal and if count is equal to floor(N/K) then increment the total number of elements which repeat exactly floor(N/K) times . But i don't get the correct the output. can you please correct me where i'm wrong with my logic.
int countSpecials(int arr[], int sizeof_array, int K){
int f = floor(sizeof_array/K), count = 1,p=0;
// Your code here
for (int i=0;i<sizeof_array;i++){
if (arr[i]>0)
{
for(int j=i+1;j<sizeof_array;j++)
{
if(arr[i]==arr[j])
count++;
}
if (count==f)
{
p++;
}
}
}
return p;
}
Use a std::map to count the number of occurances of each array element, then iterate the map checking which items have the count value you are looking for.
#include <map>
#include <algorithm>
int countSpecials(int arr[], int sizeof_array, int K)
{
int f = floor(sizeof_array/K);
std::map<int, int> m;
for (int i = 0; i < sizeof_array; i++)
m[arr[i]]++;
return std::count_if(m.begin(), m.end(),
[=](const std::map<int, int>::value_type &elem){ return (elem.second == f); }
);
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Suppose, an array can hold 10 elements and I want it to fill it with random integers ranging from 1 to 5. In C++, it can be easily done by using rand().
But I want to limit the number of occurrences of the digit 5 to TWO times. [ At the end, the array must contain two 5s. ]
So, is there any function in C++ to set this constraint?
This is what I've done so far.
for(int i=0; i<10; i++)
{ num=rand()%5+1;
if(num==5)
{ if(count<2)
{ count++;
arr[i]=num;
}
else
{ i--;
continue;
}
}
else
arr[i]=num;
}
If you have any ideas, feel free to share it with me. Thanks.
Just keep track of if you've already generated a 5, in a custom generator
class arunAJs_generator {
int number_of_fives=0;
public:
int operator()() {
if (number_of_fives == 2) {
//return random number 1-4
return rand() % 4 + 1;
} else {
//return random number 1-5
int value = rand() % 5 + 1;
if (value == 5)
++number_of_fives;
return value;
}
}
};
int main() {
arunAJs_generator gen;
int values[10];
std::fill(std::begin(values), std::end(values), gen);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hi everyone!!! I was writing contest and actually this is not difficult question but I stuck on it. Please help, here my code that passed only three tests and failed on fourth test. Answer must not use function and pointers. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int max=a[0],min=[0],ind1=0,ind2=0;
for(int i=0;i<n;i++){
if(a[i]>=max) {
max=a[i];
ind2=i;
}
if(a[i]<=min) {
min=a[i];
ind1=i;
}
}
int sum=0;
if(ind1<ind2)
for(int i=ind1+1;i<ind2;i++){
if(a[i]%2==0) sum++;
}
else if(ind1>ind2)
for(int i=ind2+1;i<ind1;i++){
if(a[i]%2==0) sum++;
}
cout<<sum;
}
It looks like you're trying to count the number of even elements between the min and max element in the array?
It may have failed the last test because you set initial min/max to garbage. In the case where a[0] is initialised to zero, and all the elements in the array are negative, the reported max will be incorrect, for example. You need to set max to the minimum integer and vice versa.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int sumTo(int value)
{
int total(0);
for (int count=1; count <= value; ++count)
total += count;
return total;
}
Where do I add cout<<total; to print the results? I have tried to add the print function several locations without success -- either getting nothing or a large number (maybe a number previously stored in the location). Have debugger with the result showing total as 15 when value is equal to 5.
In addition, I have added a void function to print, but still no correct results.
Need help with the proper placement of the print function to yield the correct answer?
This should work :
#include <iostream>
int sumTo(int value)
{
int total = 0;
for (int count=1; count <= value; ++count)
total += count;
return total;
}
int main(void)
{
std::cout << sumTo(12);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just start to learn programming and here is a problem about fibonacci sequence:
It is asked to make a function with parameter k(means k-order of fibonacci) and parameter n(means the n th member of sequence) and use that function to get the m th member of a k-order sequence.
f0 = 0, f1 =0, ....fk-2 =0, fk-1=1;
when n=k, k+1,...
fn= fn-1+fn-2+....+fn-k; n=k,k+1,....
(letters and numbers on right-side of f are subscripts which mean the n th member, n-1 th member..)
following is my code:
int Fibonacci(int k, int n){
int result=0;
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>i-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
new version
int result=0;
int Fibonacci(int k, int n){
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>n-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
my question is why this code cannot come out correct answer? there should be some problems in the loop but I cannot find them. Will someone help me?
for(int i=n-1; i>i-k-1;--i)
i will pretty much always be greater than i-k-1
Maybe you mean for(int i=n-1; i>n-k-1;--i), but you should check.