Longest Even Length Substring
PROBLEM :
For given string ‘str’ of digits, find length of the longest substring of ‘str’, such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case contains a string str of length N.
Output:
Print the resultant substring of length 2k such that sum of left k elements is equal to right k elements and if there is no such substring print 0.
Constraints:
1 = T = 100
1 = N = 100
0 = k = 100
Example:
Input:
2
000000
1234123
Output:
6
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int Longest_Even_Length_Substring(string ) ;
int max(int ,int ) ;
int main()
{
int t,ans ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
ans=Longest_Even_Length_Substring(str) ;
cout<<ans<<endl ;
}
return 0;
}
int Longest_Even_Length_Substring(string str)
{
int len ;
len=str.length() ;
int i,lsum,rsum,l,r,ans ;
ans=0 ;
for(i=0;i<len-1;i++)
{
l=i ;
r=i+1 ;
lsum=0 ;
rsum=0 ;
while(l>=0&&r<len)
{
lsum=lsum+(str[l]-'0') ;
rsum=rsum+(str[r]-'0') ;
if(lsum==rsum)
ans=max(ans,r-l+1) ;
l-- ;
r++ ;
}
}
return ans ;
}
int max(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
For given string ‘str’ of digits, find length of the longest substring of ‘str’, such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case contains a string str of length N.
Output:
Print the resultant substring of length 2k such that sum of left k elements is equal to right k elements and if there is no such substring print 0.
Constraints:
1 = T = 100
1 = N = 100
0 = k = 100
Example:
Input:
2
000000
1234123
Output:
6
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int Longest_Even_Length_Substring(string ) ;
int max(int ,int ) ;
int main()
{
int t,ans ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
ans=Longest_Even_Length_Substring(str) ;
cout<<ans<<endl ;
}
return 0;
}
int Longest_Even_Length_Substring(string str)
{
int len ;
len=str.length() ;
int i,lsum,rsum,l,r,ans ;
ans=0 ;
for(i=0;i<len-1;i++)
{
l=i ;
r=i+1 ;
lsum=0 ;
rsum=0 ;
while(l>=0&&r<len)
{
lsum=lsum+(str[l]-'0') ;
rsum=rsum+(str[r]-'0') ;
if(lsum==rsum)
ans=max(ans,r-l+1) ;
l-- ;
r++ ;
}
}
return ans ;
}
int max(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment