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.
Related
I wrote a program that should take an array of numbers and find the array index of the smallest number. However, when I type the numbers with spaces between and then press enter, the program keeps running. What can be the cause? Here is the code:
#include<stdio.h>
//read numbers to an array
//find minimum
//print the index of minimum
double findMinimum(int size,double array[]){
int n;
int minIndex=0;
for(n=1;n<size;size++){
if(array[n]<array[n-1]){
minIndex=n;
}
}
return minIndex;
}
int main(){
setvbuf(stdout,NULL,_IONBF,0);
int size=0;
double inArray[size];
printf("Enter an array of numbers:");
int k=0;
char c;
while(c!='\n'){
c=getchar();
if(c=='\n'){
break;
}
scanf("%lf",&inArray[k]);
k++;
size++;
};
int minIndex=0;
minIndex=findMinimum(size,inArray);
printf("The index of minimum number is %i",minIndex);
return 0;
}
I also took the part of the code that scans numbers to an array. I tried to change the while loop and used "break" statement, but the output gave all numbers in an array except the first one. Here is the code:
#include<stdio.h>
//read numbers to an array
int main(){
setvbuf(stdout,NULL,_IONBF,0);
int size=0;
double inArray[size];
printf("Enter an array of numbers ending with question mark:\n");
int k=0;
char c;
while(1){
c=getchar();
if(c=='?'){
break;
}
scanf("%lf",&inArray[k]);
k++;
size++;
};
int n;
for(n=0;n<size;n++){
printf("%f\n",inArray[n]);
}
return 0;
}
Thanks for help in advance!
for(n=1;n<size;size++){
if(array[n]<array[n-1]){
minIndex=n;
}
}
The issue in your code is you are incrementing size, thats why your loop is not terminating. increment n
Edit
you have initialized your 'n' with 1 , however arrays start with zero index thats why it misses the first element,
for(n=0;n<size;n++){
if(array[n]<array[minIndex]){
minIndex=n;
}
}
try this one :
for(n=1; n < size; n++){
if(array[n]<array[minIndex]){
minIndex=n;
}
}
This is a question from oj PAT
Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.
However, my code always time out, i.e, it takes too long time. How to solve it?
main()
{
int i,*a,n,m,k,data;
scanf("%d%d",&n,&m);
a=malloc(n*sizeof(int));
a[0]=-10001; //
for(i=1;i<=n;i++)
{
scanf("%d",&data);
heapAdjust(a,data);
}
for(i=1;i<=m;i++)
{
scanf("%d",&k);
printf("%d",a[k]);
k=k/2;
while(1)
{
printf(" %d",a[k]);
if(k==1)
break;
k=k/2;
}
printf("\n");
}
free(a);
}
void heapAdjust(int a[],int data) // make heap
{
static int size=0;
int i;
i=++size;
for(;a[i/2]>data;i=i/2)
a[i]=a[i/2];
a[i]=data;
}
You are probably running into an infinite loop because k becomes zero at some point.
Try changing the break condition inside the while(1) loop from k == 1 to k <= 1 and see if that helps.
I tried to solve the Small Factorial problem on SPOJ and got 'Wrong answer'. I wrote the following code :
#include <stdio.h>
int main() {
int t,i, num;
unsigned long long int fact=1;
scanf ("%d", &t);
while (t-- >0) {
fact=1;
scanf ("%d", &num);
for (i=num; i>0; i--) {
fact*=i;
}
printf ("%llu\n",fact);
}
return 0;
}
This code is not finding factorial for large inputs like 100. What are the changes required?
In C and C++ you have the maximum allowed range from -2^63+1 to +2^63-1 which is for long long data type. As you can see this range is still too small to store >20 digits.
You have to use a char array to store single digit per array index.
one way to do this in C++:
#include<stdio.h>
#include<iostream>
int main()
{
int t;
char a[200]; //array will have the capacity to store 200 digits.
int n,i,j,temp,m,x;
scanf("%d",&t); //enter total elements
while(t--)
{
scanf("%d",&n); // enter no. one at a time
a[0]=1; //initializes array with only 1 digit, the digit 1.
m=1; // initializes digit counter
temp = 0; //Initializes carry variable to 0.
for(i=1;i<=n;i++)
{
for(j=0;j<m;j++)
{
x = a[j]*i+temp; //x contains the digit by digit product
a[j]=x%10; //Contains the digit to store at position j
temp = x/10; //Contains the carry value that will be stored on later indeces
}
while(temp>0) //while loop that will store the carry value on array.
{
a[m]=temp%10;
temp = temp/10;
m++; // increments digit counter
}
}
for(i=m-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
return 0;
}
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.
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;
}