-
Notifications
You must be signed in to change notification settings - Fork 0
/
0And1Matching.c
43 lines (41 loc) · 935 Bytes
/
0And1Matching.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int searchForEqual(char a[],int l){
char*pointer1=a,*pointer2=a;
int count=0;
for(int i=0;i<l-1;i++){
pointer2++;
}
int left=1,right=l;
while(left<right && right-left!=1){
while(*pointer1!='0' && left!=right && right-left!=1){
pointer1++;
left++;
}
while(*pointer2!='1' && left!=right && right-left!=1){
pointer2--;
right--;
}
pointer1++;pointer2--;
left++;right--;
count++;
}
if(left==right){
return left-1;
}
if(right-left==1){
return -1;
}
return -1;
}
int main(){
char*input=(char*)calloc(100000,sizeof(char));
scanf("%s",input);
int length=strlen(input);
int answer;
answer=searchForEqual(input,length);
printf("At index %d",answer);
free(input);
return 0;
}