Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualAhmedCoeia

Posted 10 December 2012 - 11:12 PM

I wanted to solve the problem here on my own: To find the longest substring with equal sum in left and right in C++ http://stackoverflow.com/questions/8469407/to-find-the-longest-substring-with-equal-sum-in-left-and-right-in-c
The code is here
int getEqualSumSubstring(string s) {
    int i=0,j=i,foundLength=0;
    for(i=0;i<s.length();i++)
    {
	    for(j=i;j<s.length();j++)
	    {
		    int temp = j-i+1;
		    if(temp%2==0)
		    {
			    int leftSum=0,rightSum=0;
			    string tempString=s.substr(i,temp);
			    // printf("%d ",tempString.length());
			    for(int k=0;k<temp/2;k++)
			    {
				    leftSum=leftSum+tempString[k]-48;
				    rightSum=rightSum+tempString[k+(temp/2)]-48;
			    }
			    if((leftSum==rightSum)&amp;&amp;(leftSum!=0))
				    if(tempString.length()>foundLength)
				    foundLength=tempString.length();
		    }
	    }
    }
    return(foundLength);
}
I wanted to know how the calculation of temp = i+j-1 is done ? how on paper or by examples he could get that equation. I tried a lot but I couldn't get it.

#3AhmedCoeia

Posted 10 December 2012 - 11:12 PM

I wanted to solve the problem here on my own: To find the longest substring with equal sum in left and right in C++ http://stackoverflow.com/questions/8469407/to-find-the-longest-substring-with-equal-sum-in-left-and-right-in-c
The code is here
int getEqualSumSubstring(string s) {
    int i=0,j=i,foundLength=0;
    for(i=0;i<s.length();i++)
    {
	    for(j=i;j<s.length();j++)
	    {
		    int temp = j-i+1;
		    if(temp%2==0)
		    {
			    int leftSum=0,rightSum=0;
			    string tempString=s.substr(i,temp);
			    // printf("%d ",tempString.length());
			    for(int k=0;k<temp/2;k++)
			    {
				    leftSum=leftSum+tempString[k]-48;
				    rightSum=rightSum+tempString[k+(temp/2)]-48;
			    }
			    if((leftSum==rightSum)&amp;&amp;(leftSum!=0))
				    if(tempString.length()>foundLength)
				    foundLength=tempString.length();
		    }
	    }
    }
    return(foundLength);
}
I wanted to know how the calculation of temp = i+j-1 is done ? how on paper or by examples he could get that equation. I tried a lot but I couldn't get it.

#2AhmedCoeia

Posted 10 December 2012 - 11:12 PM

I wanted to solve the problem here on my own: To find the longest substring with equal sum in left and right in C++ http://stackoverflow.com/questions/8469407/to-find-the-longest-substring-with-equal-sum-in-left-and-right-in-c
The code is here
int getEqualSumSubstring(string s) {
    int i=0,j=i,foundLength=0;
    for(i=0;i<s.length();i++)
    {
	    for(j=i;j<s.length();j++)
	    {
		    int temp = j-i+1;
		    if(temp%2==0)
		    {
			    int leftSum=0,rightSum=0;
			    string tempString=s.substr(i,temp);
			    // printf("%d ",tempString.length());
			    for(int k=0;k<temp/2;k++)
			    {
				    leftSum=leftSum+tempString[k]-48;
				    rightSum=rightSum+tempString[k+(temp/2)]-48;
			    }
			    if((leftSum==rightSum)&amp;&amp;(leftSum!=0))
				    if(tempString.length()>foundLength)
				    foundLength=tempString.length();
		    }
	    }
    }
    return(foundLength);
}
I wanted to know how the calculation of temp = i+j-1 is done ? how on paper or by examples he could get that equation. I tried a lot but I couldn't get it.

#1AhmedCoeia

Posted 10 December 2012 - 11:00 PM

I wanted to solve the problem here on my own: To find the longest substring with equal sum in left and right in C++ http://stackoverflow.com/questions/8469407/to-find-the-longest-substring-with-equal-sum-in-left-and-right-in-c
The code is here
int getEqualSumSubstring(string s) {
    int i=0,j=i,foundLength=0;
    for(i=0;i<s.length();i++)
    {
    for(j=i;j<s.length();j++)
    {
    int temp = j-i+1;
    if(temp%2==0)
    {
    int leftSum=0,rightSum=0;
    string tempString=s.substr(i,temp);
    // printf("%d ",tempString.length());
    for(int k=0;k<temp/2;k++)
    {
    leftSum=leftSum+tempString[k]-48;
    rightSum=rightSum+tempString[k+(temp/2)]-48;
    }
    if((leftSum==rightSum)&&(leftSum!=0))
    if(tempString.length()>foundLength)
    foundLength=tempString.length();
    }
    }
    }
    return(foundLength);
}
I wanted to know how the calculation of temp = i+j-1 is done ? how on paper or by examples he could get that equation. I tried a lot but I couldn't get it.

PARTNERS