Getting Runtime Error in competitive algo challenge (SPOJ MAJOR) - c

According to the problem we have to find whether an element occurs more than n/2 times or not and then print Yes or No accordingly.
The numbers can vary from 10^-3 to 10^3.
I took an array count[2005] and then adding 1000 to each input to make 10^-3 equal to 0 i.e, -1000+1000=0 and then storing no.of occurrences of -1000 in count[0] and same for the rest elements. Therefore:
lower limit= -1000+1000=0;
higher limit= 1000+1000=2000;
But still I am getting memory access violation. Here is the link to the original problem: http://www.spoj.com/problems/MAJOR/
#include<stdio.h>
int main()
{
int t,n,a,count[2005],max,check,temp;
scanf("%d",&t);
while(t--)
{
check=0;
scanf("%d",&n);
for(int i=0;i<2005;i++)
count[i]=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
temp=a+1000;
count[temp]++;
if(count[temp]>(n/2))
{
check=1;
max=temp-1000;
break;
}
}
if(check==1)
printf("YES %d\n",max);
else
printf("NO\n");
}
return 0;
}

Problem to your code is you are not reading input fully - SPOJ demands that your program should read input fully, not break in middle.
Solution to your problem: Just take an arr of size (10^6+1) and first read all inputs and then apply algorithm. Remember this if you break while reading input too, it will always be SIGSEGV. So, always read input fully on every programming website.
Here is your modified code and it is ready to get AC:
#include<stdio.h>
int arr[1000002]; // Array of Size (10^6+2)
int main()
{
int t,n,a,count[2005],max,check,temp,i;
scanf("%d",&t);
while(t--)
{
check=0;
scanf("%d",&n);
for(int i=0;i<2005;i++)
count[i]=0;
for(i=0;i<n;i++)
scanf("%d",&arr[i]); // READING INPUT FULLY IN AN ARRAY
for(i=0;i<n;i++)
{
a=arr[i]; // Now, a=arr[i] and previous algorithm applies now
temp=a+1000;
count[temp]++;
if(count[temp]>(n/2))
{
check=1;
max=temp-1000;
break;
}
}
if(check==1)
printf("YES %d\n",max);
else
printf("NO\n");
}
return 0;
}

Related

How to take input multiple times even after the execution of program in C?

so I was writing this program to find the number of times a digit is repeated in a number.How can I ask the user to enter inputs again and again even after the execution at the first time
#include <stdio.h>
int main()
{
int rem,flag,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
return 0;
}
One way to do this would be to wrap your code in a while (1) loop (loop forever). I've added an extra check: if a user enters the number 0, the program will break out of the loop and exit. There has to be some way to stop.
#include <stdio.h>
int main()
{
while (1){
int rem,flag=0,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
if (n==0){
break;
}
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
}
return 0;
}
For a cleaner solution, you may want to create a function for everything within the while loop, and call that function.
Side note: flag has to be set to 0 at the start of the loop.

Program to remove the duplicate elements from an array in c

I've tried to write a program that removes the duplicate values from an array. I've partly managed to do so since my program is able to remove any ONE of the numbers which are repeated TWICE in the array. So the problem is that if a number is repeated thrice only one of the number is removed, i.e. the other two is still left in the array, also if more than one number is repeated even then only the number which comes first in the array is removed. I really cannot understand what's wrong with my code and why is it unable to remove numbers that are repeated more than two times. I've already surfed through the internet regarding this issue and though I got different ways to remove the duplicate elements, I still don't know what's wrong with my code.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20];
printf("Enter n value \n");
scanf("%d",&n);
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++)
a[i]=a[i+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
24
It clearly fails to remove the other repeated element "24". Also if a number was repeated thrice only one of the number would be removed.
for(i=0;i<n;++i) // <-------------------------------------- for i
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++) // <--------------------------- for i
a[i]=a[i+1];
n-=1;
}
}
You are using the same loop variable for two loops, one nested inside the other. This cannot work. Use different variables. Live demo.
The Problem seem to lie in the if condition in second loop.
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
Simply put this piece of code after your if condition
if(a[i]==a[j])
and it will work.
My mistake, at first glence I thought you had problem with n after running this it worked.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20], count;
printf("Enter n value \n");
scanf("%d",&n);
count = n;
int j;
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(j=index;j<n;j++)
a[j]=a[j+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
You should name your iterator variables better so you might not confuse them in nested loops, or as you do, use the same twice in a nested loop.
This skips all variables after your first removal.
and you don't have to do this
if(pos==-1)
return pos;
skip the if as it is not necessary and if at this position posis not -1then you would have no return which would be UB I think.

Removing repetitions of same numbers in an array

Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++

Output coming same every time

I wrote this program which takes n and k as input and then takes an array a[n] as input. The program must give the output as the total no of distinct integers in array a that are less than k and odd. But this program with every input is producing 0 as output.
#include<stdio.h>
int main()
{
long long int n,i,j,k,temp=-1;
scanf("%lld %lld",&n,&k);
long long int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
long long int cnt=0;
for(i=0;i<n;i++)
{
if(a[i]<k)
{
if((a[i]%2)==1)
cnt++;}
}
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
cnt--;
a[j]=temp;
--temp;
}
}
}
printf("%lld",cnt);
return 0;
}
scanf("%d", &a[i]);
Must be:
scanf("%lld", &a[i]);
check out the inner for loop, maybe you need to declare a variable and increment that and write and if statement that says, if variable==1 then print the desired value.
Not sure but might work.

Wrong answer in a codechef c code(codechef-STATUES)

I am trying to solve this question, but on codechef its showing wrong answer,link to the queston is http://www.codechef.com/problems/STATUES/ .On system ,its showing correct answer, after trying a lot, i couldnt find the bug My code is,
#include<stdio.h>
//#include<conio.h>
int main()
{
int a[150];
int n;
int i;
int sum;
int avg;
int count=0;
scanf("%d",&n);
int ans;
int diff;
while(n!=0)
{
sum=0;
ans=0;
count++;
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
for(i=0;i<n;i++)
{ diff=avg-a[i];
if(diff>0)
ans=ans+diff;
}
printf("Set#%d\nThe minimum number of moves is %d.\n",count,ans);
scanf("%d",&n);
}
return 0;
}
Read the condition in the question:
Output a blank line after each test case.
Be careful about the case and punctuation in the above strings. Not adhering to the output format strictly will lead to the Wrong Answer verdict.
There is no blank line after your each output. Also one more for loop is needed to met the condition for exact output format. Add this snippet after while.
for(i=0;i<n;i++)
{
printf("\nSet #%d\nThe minimum number of moves is %d.\n",i,ans);
}
Another problem is
int a[150];
a should not exceed from 50. Change it to
int a[50];
But I woul advice you to use variable length array in this case to save memory.

Resources