What is wrong in this code? I'm getting time limit exceeded error. - c

#include<stdio.h>
#include<string.h>
int main() {
int test_case, count, i, j, check, count_1; char a[100005];
scanf("%d", &test_case);
while(test_case--) {
scanf("%s", a);
count=count_1=0;
for(i=0;i<strlen(a);i++) {
if(a[i]=='1') {
count_1++;
}
}
for(i=0;i<strlen(a);i++) {
if(a[i]=='1'&&a[i+1]!='1'&&a[i+1]!='\0') {
count++;
for(j=(i+1);j<strlen(a);j++) {
if(a[j]=='0') {
a[j]='1';
a[j-1]='0';
count++;
}
else {
break;
}
}
}
if(i==(strlen(a)-1)) {
check=0;
for(j=(strlen(a)-1);j>=1;j--) {
if(a[j]=='1'&&a[j-1]=='1') {
check++;
}
else {
break;
}
}
if(check==(count_1-1)) {
break;
}
else {
i=0;
}
}
}
printf("%d\n", count);
}
return 0;
}
This is the question.
I have carried out a few dry runs and I'm getting the results. I'm not sure why I'm getting time limit exceeded error. The code is working for every test case except #2 (that is 1100001).
Please help me I'm stuck on this one for quite a while.

I just needed to change i=0 to i=-1 to avoid infinite looping.

Related

Why is my code giving right output in codeBlocks but wrong output in HackerRank?

I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}

unable to find Prime number using Continue control flow

i wrote continue which brings back control to iteration and until the remainder is not zero so output should be printed. but it is not . why ?
#include<stdio.h>
main()
{
int n,i;
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
{
continue;
printf("prime\n");
}
}
}
Can you spot the difference?
int main(void)
{
int n,i;
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
{
continue;
}
else
{
printf("not prime\n");
return 0;
}
}
printf("prime\n");
}

Run time error in C for prime generator

I tried to solve a prime number generator problem in spoj.com. I solved it and it runs perfectly on my machine. But after submitting the solution to online, it shows run time error. Please help me!
Could anyone take a look into my code and tell where the problem is?
#include <stdio.h>
int primeFactor[350],w=0,z,i;
main()
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
}
primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
Your main function may be returning some indeterminate value because it doesn't have any return statement and the judge may be treating it as Runtime Error.
You will have to return 0 from the main function.
Also, you shouldn't omit the return type of each functions.
Try this:
#include <stdio.h>
int primeFactor[350],w=0,z,i;
int main(void)
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
return 0;
}
int primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
int primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}

Next Palindrome SPOJ (PALIN)

While solving the NEXT PALINDROME question on SPOJ. I have considered for every possible case and wrote the code. But it is showing WA(Wrong Answer). I am not getting my mistake or any possible case where my code won't work.
Below is my code.
Please help me out.
Thank You.
#include<stdio.h>
#include<string.h>
char K[10000000];
int a[10000000];
int main()
{
int t,length,i,count,j,rem,carry;
scanf("%d",&t);
while(t--)
{
scanf("%s",K);
length=strlen(K);
count=0;
for(i=0;i<length;i++)
{
a[i]=K[i]-'0';
if(a[i]==9)
count++;
}
if(length==2 && count!=length)
{
if(a[0]<=a[1])
{
a[1]=++a[0];
}
else
{
a[1]=a[0];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
if(count==length)
{
if(length==1)
{
printf("11\n");
continue;
}
memset(a,0,sizeof(a));
a[0]=10;
a[length-1]=1;
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
i=0;
j=length-1;
count=0;
while(j-i!=1 && j-i!=0)
{
if(a[i]>a[j])
{
a[j--]=a[i++];
count++;
}
else
{
if(a[i]<a[j])
count--;
a[j--]=a[i++];
}
}
if(count==0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
{
if(a[i]<a[j] || count<0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
a[j]=a[i];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}

Implementing MiniMax Algorithm in Tic Tac Toe for C

I'm trying to make an unbeatable AI for a tic tac toe game, much like the one at http://perfecttictactoe.herokuapp.com/. However, since I'm still learning C, its a bit simpler as the user always goes first and there's no GUI, and ASCII art is used for the graphics instead. I've looked over various implementations of the MiniMax algorithm, as it seems to be the most recommended way to solve the problem, according to this site as well as many others. I'm having a bit of a problem, as the algorithm does not seem to be working. Instead, it steadily moves left to right beginning in the square (1,1) ([0,0] in the array) and going down to the next row once it reaches the end of the row. I've tried a lot of things, from meticulously combing over my code to rewriting it from scratch, but nothing seems to work. I'm hoping the wonderful community here can help me out!
Thanks in advance!
Code:
#include <stdio.h>
#include "simpio.h"
#include "stdlib.h"
void print(char board[3][3]);
char didwin(char board[3][3]);
void AImove(char board[3][3], int *rows, int *columns);
int MinMax(char board[3][3], int player);
int main(void)
{
int i, i2, player, rows=0,columns=0;
char board[3][3], tempboard[3][3], winner;
string name;
printf("The computer will now play tic-tac-tow with you.\n");
printf("The computer will use \"O\" and you will use \"X\".\n");
printf("You will go first. To begin, please enter your name: ");
name=GetLine();
while(true)
{
for(i=0;i<3;i++)
{
for(i2=0;i2<3;i2++)
{
board[i][i2]='\0';
}
}
print(board);
for(i=0;i<9&&winner==0;i++)
{
printf("\n");
player=i%2+1;
if(player==1)
{
printf("\n%s, what row is your square in?\t", name);
rows=GetInteger()-1;
printf("\n%s, what column is your square in?\t", name);
columns=GetInteger()-1;
}
else if(player==2)
{
printf("\nThe computer will move now.\n");
for(i=0;i<3;i++)
{
for(i2=0;i2<3;i2++)
{
tempboard[i][i2]=board[i][i2];
}
}
AImove(tempboard, &rows, &columns);
}
if(rows<0||rows>3||columns<0||columns>3||board[rows][columns]=='X'||board[rows][columns]=='O')
{
if(player==1)
{
printf("The space is already taken or out of bounds, please try again");
}
i--;
}
else
{
if(player==1) board[rows][columns]='X';
else board[rows][columns]='O';
print(board);
}
winner = didwin(board);
}
if(winner!='\0')
{
if(winner=='X') printf("\n\nYou won!");
else printf("\n\nThe computer won! Try harder next time.");
break;
}
else
{
printf("No winner this round. Try again.");
break;
}
}
}
void print(char board[3][3])
{
int i,i2;
printf("\n");
for(i=0;i<3;i++)
{
for(i2=0;i2<3;i2++)
{
if(board[i][i2]=='\0')
{
printf(" ");
}
else
{
printf(" %c ", board[i][i2]);
}
if(i2<2) printf("|");
}
if(i<2)
{
printf("\n-----------\n");
}
}
}
char didwin(char board[3][3])
{
int i,i2;
char temp;
char winner = '\0';
for(i=0;i<3;i++)
{
temp=board[i][0];
for(i2=0;i2<3;i2++)
{
if(board[i][i2]!=temp)
{
temp='\0';
}
}
if(temp!='\0')
{
winner=temp;
}
}
for(i=0;i<3;i++)
{
temp=board[0][i];
for(i2=0;i2<3;i2++)
{
if(board[i2][i]!=temp)
{
temp='\0';
}
}
if(temp!='\0')
{
winner=temp;
}
}
temp=board[0][0];
for(i=0;i<3;i++)
{
if(board[i][i]!=temp)
{
temp='\0';
}
}
if(temp!='\0')
{
winner=temp;
}
temp=board[0][2];
for(i=0;i<3;i++)
{
if(board[i][2-i]!= temp)
{
temp='\0';
}
}
if(temp!='\0')
{
winner=temp;
}
return winner;
}
void AImove(char tempboard[3][3], int *rows, int *columns)
{
int points=-1, temppoints, i, i2;
for(i=0;i<3;i++)
{
for(i2=0;i2<3;i2++)
{
if(tempboard[i][i2]=='\0')
{
tempboard[i][i2]='O';
temppoints=MinMax(tempboard, 1);
if(temppoints>points)
{
points=temppoints;
*rows=i;
*columns=i2;
}
tempboard[i][i2]='\0';
}
}
}
}
int MinMax(char tempboard[3][3], int player)
{
int winner, points, temppoints, i, i2;
winner=didwin(tempboard);
if(winner=='X') return(0);
else if (winner=='O') return(999);
else
{
for(i=0;i<3;i++)
{
for(i2=0;i2<3;i2++)
{
if(tempboard[i][i2]='\0')
{
if (player==1)
{
tempboard[i][i2]='X';
}
else
{
tempboard[i][i2]='O';
}
if (player==1)
{
temppoints=MinMax(tempboard, 2);
}
else
{
temppoints=MinMax(tempboard, 1);
}
if(temppoints>points)
{
points=temppoints;
}
tempboard[i][i2]='\0';
}
}
}
}
return(points);
}
Code does assignment rather than comparison in MinMax(). With points not initialized and returned, nothing but UB.
int points
...
// if(tempboard[i][i2]='\0') { // never true
if (tempboard[i][i2] == '\0') {
points == ...
...
return points;
BTW: suggest explicitly returning a value from main()
The key error of your code is that the algorithm "MinMax" has not been implemented right. Note that based on your evaluation, player1 is the Min player who is trying to make his points as low as possible while player2 is the Max player who is trying to make his points as high as possible. So you should discuss these two cases respectively as follows:
if (player==1)
{
temppoints=MinMax(tempboard, 2);
}
else
{
temppoints=MinMax(tempboard, 1);
}
if(player==1 && temppoints<points) //HERE
{
points=temppoints;
}
else if(player==2 && temppoints>points) //HERE
{ //HERE
points=temppoints; //HERE
} //HERE
Besides the above error, you may also pay attention to some inadvertent errors such as changing if(tempboard[i][i2]='\0') to if(tempboard[i][i2]=='\0') in the MinMax function.

Resources