|
|
#1 (permalink) |
|
International Coach
Join Date: Apr 2009
Location: India
Posts: 10,305
|
The Official Programming Thread
For any programming related problems you might come across..
I'm struggling with one right now. I'm given a binary string and I need to find the largest substring with an equal number of 1s and 0s in it. |
|
|
|
|
|
#4 (permalink) |
|
International Captain
Join Date: Jul 2010
Location: Hyderabad India
Posts: 5,148
|
Well, I'll just check all substrings which are nC2 in number. I will start from the largest and go down to smaller lengths in each iteration. So I check in this order :
1 to n 1 to n - 1 2 to n 1 to n - 2 and so on Not the smartest way to do it but would it for you. |
|
|
|
|
|
#6 (permalink) | |
|
The Wheel is Forever
Join Date: Feb 2006
Location: USA
Posts: 36,546
|
So dire. I was a programmer for five years - the best day of my life will be the day I forget the Big O notation.
__________________
Quote:
-My beliefs summarized in words much more eloquent than I could come up with How the Universe came from nothing |
|
|
|
|
|
|
#7 (permalink) |
|
International Captain
Join Date: Jul 2010
Location: Hyderabad India
Posts: 5,148
|
Ya that was the lamest approach
![]() Here is another: convert 1s and 0s into 1s and -1s. Now do two steps: For the string from 1 to n create an array of partial sums indexed from 0 to n, with ith element in the array representing sum of binaries from 1 to i (0th element being left as 0). Now in this array of partial sums find duplicates that are farthest apart. If ith and jth partial sums are identical then your substring from i+1 to j has equal 0s and 1s First step is going to be order n. Depending on how complex second step is, this may or may not be a better approach ![]() EDIT: something tells me the two can be done simultaneously. Will get back if I can think. Last edited by 8ankitj; 29-04-2011 at 02:14 PM. |
|
|
|
|
|
#8 (permalink) |
|
International Captain
Join Date: Jul 2010
Location: Hyderabad India
Posts: 5,148
|
We don't have to store partial sums actually. We can create a 2 dimensional array that stores the first and last occurrences of each possible partial sum result and also the difference between the two. As we run down the string calculating partial sums we just have to update the start and end point of that partial sum result.
For example, consider a string of length 15: 1 -1 -1 1 1 -1 1 -1 -1 1 1 1 -1 1 1 You will get a 2-d array like this: a(-1) = (3,9,6) a(0) = (0,10,10) a(1) = (1,13,12) a(2) = (12,14,2) a(3) = (15,15,0) This indicates that partial sum -1 occurs at 3rd and 9th positions which are 6 places apart and so on. a(1) has the largest difference (of 12). Therefore the desired largest substring is from 2 to 13. Creating those 2-D array and determining the max of differences are both order n. So overall algorithm is also order n (unless I have missed something). Last edited by 8ankitj; 29-04-2011 at 02:23 PM. |
|
|
|
|
|
#9 (permalink) | ||||
|
Global Moderator
Join Date: Feb 2010
Location: A Blood Rainbow
Posts: 26,764
|
Doing a course on scientific computing aws. Useful, easy, and so ****ing boring. Ugh. Don't want to see another error formula in my life. And order(h to the whatever the **** it is) can go die in a fire.
__________________
+ and the buzz surrounds it does + * * * in which cribb demonstrates the power of the jinx Quote:
|
||||
|
|
|
|
|
#10 (permalink) |
|
International Coach
Join Date: Apr 2009
Location: India
Posts: 10,305
|
When our professor droned on about computational complexity, and how to find the order(n to whatever the **** it is)
for months on end, we used to struggle to see the point. For example, when they taught us about Strassen's Algorithm for Matrix Multiplication, one guy in class stood up and asked, why the hell are we learning about an algorithm that reduces the complexity from O(N^3) to to O(N^2.8), whoop de ****ing do. And here Intel are slaving their asses off to build better and better processors with higher clock speeds and multi-core architectures, blah blah. What's the point of all their hard work?But yeah, these tiny details become vitally important when the problem sizes become large. Last edited by vcs; 30-04-2011 at 02:17 AM. |
|
|
|
|
|
#12 (permalink) |
|
Cricketer Of The Year
Join Date: Jan 2011
Location: UK
Posts: 8,286
|
Hey guys, out of curiosity if I were to start my education as a programmer, what language would you recommend that I study first, or would I have to learn something else before trying to understand a programming language (I don't know anything about this field). I would like to learn visual basic because I want to become good at excel. Would I have to also improve my maths in order to do this or can I get by without. And how do I go about doing this, i.e. do I start reading particular books (what books) or take a particular course. Ideally would like someone from UK to answer this because they would understand the education system, but ideas will be welcomed from anyone who knows about the area.
I recently completed my degree in Accounting and would like to one day specialise in programming accounting software, so it would be of course useful to learn how to programme. Thanks Last edited by Agent Nationaux; 30-04-2011 at 10:48 AM. |
|
|
|
|
|
#13 (permalink) | |
|
International Coach
Join Date: Apr 2009
Location: India
Posts: 10,305
|
Quote:
Once you've decided to get started, you can pick up any basic book on C or one of the vast tutorials available on the net, and try writing some simple programs to get acquainted with what is available to you, and how to translate the problem that you are trying to solve into the language. Getting to a stage where you can write simple programs is not difficult at all. Then you can let your own interest take you as deep as you wish to explore! |
|
|
|
|
|
|
#15 (permalink) | |
|
International 12th Man
Join Date: Jun 2009
Location: USA
Posts: 1,537
|
Quote:
|
|
|
|
|
|
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The Official Gareth Hopkins isn't terrible thread | Athlai | Cricket Chat | 154 | 26-11-2010 06:21 AM |
| The Official Bring Back Fiery Thread! | cover drive man | Off Topic | 26 | 29-08-2010 01:53 AM |
| Finally ! A Last Word Thread | SJS | Cricket Chat | 22 | 01-01-2010 07:42 PM |