Longest Even Length Substring | Practice | GeeksforGeeks

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 string of length N.

Output:
Print length of the longest 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

Example:
Input:
2
000000
1234123
Output:
6
4  
0
pushpendrakadwa20182 months ago
class st:   def substring(self,s):       li=[]       for i in range (0,len(s)):           for j in range(0,len(s)):               a=""               a=a+s[i:j+1]               if a!="":                   p=a                   if len(p)%2==0:                       lsum=0                       rsum=0                       for q in range(0,len(p)):                           if q<len(p)/2:                               lsum=lsum+int(p[q])                           else:                               rsum=rsum+int(p[q])                       if lsum==rsum:                           li.append(a)       if len(li)==0:           return 0       else:           li.sort(key = len)           return len(li[len(li)-1])                      #driver code        if __name__ == '__main__':   T=int(input())   for i in range(0,T):       k=input()       ob=st()       print(ob.substring(k))   
0
lindan1233 months ago
#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--)
    {
	    string str;
	    int left=0;
	    int c=0;
	    int right = 0;
	    int maxx=0;
	    cin>>str;
	    int n = str.length();
	    
	    if(n==1)
	    {
	         cout<<0<<endl;
	    }
	    
	    else{
	    for(int i=0;i<n;i++)
	    {
	        int j = i;
	        int k = i+1;
	        left=0;
	        right=0;
	        
	        while(j>=0 && k<n)
	        {
	            left += str[j]-'0';
	            right += str[k] - '0';
	            if(left==right)
	            {
	                maxx = max(maxx,k-j+1);
	            }
	            j--;
	            k++;
	        }
	    }
	    
	    cout<<maxx<<endl;
	    }
	    
    }
	return 0;
}
Time Taken : 0.0
Cpp
+1
vsdrajput0273 months ago
#include <iostream>#include <string>using namespace std;
int longestEvenSubstring(string str){
   int maxLength = 0;   while (!str.empty())   {       int lsum = 0, rsum = 0;       for (int i = 0; i < str.length() / 2; i++)       {           lsum += int(str[i]) - '0';           rsum = 0;           int j = i + 1;           while ((2 * i + 1 < str.length()) && (j <= 2 * i + 1))           {               rsum += int(str[j]) - '0';               j++;           }           if (lsum == rsum)           {               maxLength = max(maxLength, 2 * (i + 1));           }       }       str = str.substr(1);   }
   return maxLength;}int main(){   int t;   cin >> t;   cin.ignore();   while (t--)   {       string str;       getline(cin, str);       cout << longestEvenSubstring(str) << endl;   }
   return 0;}
0
gauravsingh9118054 months ago
 int n,i,len,ans=0,j;   string s;   cin>>s;   n=s.size();   int pre[n+1];   pre[0]=0;   for(i=0;i<n;i++){       pre[i+1]=pre[i]+(s[i]-'0');   }   len=(n/2)*2;   while(len>=2){       for(i=0;i+len-1<n;i++){           j=i+len-1;           if(pre[(i+j)/2+1]-pre[i]==pre[j+1]-pre[(i+j)/2+1]){               ans=len;break;           }       }       if(ans!=0){           break;       }       len-=2;   }   cout<<ans<<"\n";
+3
parasmovaliya43214 months ago
#include <iostream>using namespace std;
int main(){int t,i;cin>>t;string s;while(t--){    cin>>s;    int n=s.size(),m=0,t1=0,t2=0,k,j;        for(i=1;i<n;i++)    {        t1=0,t2=0;        j=i-1;        k=i;        while(j>=0&&k<=n-1)        {            t1+=s[j]-48;            t2+=s[k]-48;                        if(t1==t2)            m=max(m,k-j+1);            j--;            k++;        }    }        cout<<m<<endl;}}
0
ayannaskar5067
This comment was deleted.
0
kumarsaket6705 months ago
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) { //code  Scanner sc=new Scanner(System.in);  int n=sc.nextInt();  while(n-->0){    int count=0;    String str=sc.next();String nw="";    count = findLength(str);    System.out.println(count);}}static int findLength(String str){   int len = 0;    if(str.length()%2== 0){       len = str.length();   }else{        len = str.length()-1;   }  for(int i = 0; i<str.length(); i++)  {   String s = str.substring(i,i+len);   int leftsum = 0,rightsum = 0;   for(int j = 0 ; j < s.length(); j++){       char a = s.charAt(j);       if(j<s.length()/2){           leftsum += Character.getNumericValue(a);       }else{          rightsum += Character.getNumericValue(a);               }   }   if(leftsum == rightsum){     //  System.out.println(s);       return s.length();   }    if(i+len == str.length() && len >=2){       i = -1;       len = len-2;   }     }  return 0;}}
-5
shindeaniket20720035 months ago
int main() {
+2
rohitpendse1386 months ago
#include <bits/stdc++.h>
#define int long long
using namespace std;

int solve(string &s) {
    int ans = 0;
    int l, r, lsum, rsum;
    int n = s.size();
    for (int i = 0; i < n - 1; i++) {
        l = i, r = i + 1, lsum = 0, rsum = 0;
        while (l >= 0 && r <= n - 1) {
            lsum += s[l] - '0';
            rsum += s[r] - '0';
            if (lsum == rsum) ans = max(ans, r - l + 1);
            l--;
            r++;
        }
    }
    return ans;
}

int32_t main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        cout << solve(s) << '\n';
    }
    return 0;
}
0
shreyanshmishra27 months ago
class Solution {public static void main (String[] args) { //code Solution ob=new Solution(); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-->0) {     int count=0;     String str=sc.next();String nw="";     count=ob.findLength(str);     System.out.println(count); }}static int findLength(String str)   {      int n = str.length();      int maxlen = 0;       for (int i = 0; i < n; i++)       {          for (int j = i + 1; j < n; j += 2)         {              int length = j - i + 1;            int leftsum = 0, rightsum = 0;            for (int k = 0; k < length/2; k++)            {                leftsum += (str.charAt(i + k) - '0');                rightsum += (str.charAt(i + k + length/2) - '0');            }             if (leftsum == rightsum && maxlen < length)                   maxlen = length;       }   }   return maxlen; }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.