Question :
write program which takes a single argument. The single argument is a
string s, which contains only non-zero digits.
This function should print the length of longest contiguous substring
of s, such that the length of the substring is 2*N digits and the sum
of the leftmost N digits is equal to the sum of the rightmost N
digits.If there is no such string, your function should print 0.
Sample Test Cases:
Input #00:
123231
Output #00:
6
----------------------------------------------------------------------------------------------------------------------------------------------
my solution is :
public class Str {
public static void main(String args[])
{
String str1 = "123231";
int[] str = new int[str1.length()];
for(int k=0 ;k<str1.length();k++)
{
str[k] =(str1.charAt(k));
}
// int str[] = {1,2,3,2,3,1}; // problem here in
conversion in this manner
int len = str.length;
int sum = 0;
int sum1 =0;
for(int i=0;i<(len/2);i++)
{
sum = sum+str[i];
}
for(int j=(len/2);j<(len);j++)
{
sum1 = sum1+str[j];
}
if((sum==len)&&(sum1==len))
{
System.out.println(len);
}
else
{
System.out.println(0);
}
}
}
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.