Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Array Problem
Roopesh Rana
Ranch Hand
Posts: 49
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Divide an Array in two Parts in such a way so that sum will be equal while we add all the elements of array
/** * @author manoj-sh * */ public class HelloWorld { /** * @param args */ public boolean canCount(int[] nums) { int leftSum = 0, rightSum = 0; int i, j; if(nums.length == 1) return true; for(i=0, j=nums.length-1; i<=j ;){ if(leftSum <= rightSum){ leftSum+=nums[i]; i++; }else{ rightSum+=nums[j]; j--; } } return (rightSum == leftSum); } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); int[] nums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; System.out.println(helloWorld.canCount(nums)); } }
Junilu Lacar
Sheriff
Posts: 17644
300
I like...
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Did you have a question about this code? Also, what does this have to do with Web Services? Next time, please choose an appropriate forum before you post.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Magic Square, array/recursion
How to skip one element in an array?
Simple Sorting Algorithm
Summation of `multiples of 5` which sums to a given target
A Better way of doing this
More...