Questions tagged [programming-pearls]

Programming pearls are unique problems or solutions that might puzzle a programmer, they have grown from real problems that have irritated real programmers, just as natural pearls grow from grains of sand that irritate oysters.

Notable Resources

40 questions
36
votes
24 answers

Fastest algorithm for circle shift N sized array for M position

What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3]. Thanks a lot.
Arsen Mkrtchyan
  • 47,086
  • 29
  • 143
  • 178
16
votes
1 answer

Why is modulus operator slow?

Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations (+, -, *) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why…
AV94
  • 1,654
  • 2
  • 19
  • 32
13
votes
2 answers

"Programming Pearls" binary search help

I just can't seem to understand how this would work. Question: Given a sequential file that contains at most four billion 32 bit integers in random order, find a 32-bit integer that isn't in the file (and there must be at least one…
bbbb
  • 133
  • 4
11
votes
10 answers

How to find the subarray that has sum closest to zero or a certain value t in O(nlogn)

Actually it is the problem #10 of chapter 8 of Programming Pearls 2nd edition. It asked two questions: given an array A[] of integers(positive and nonpositive), how can you find a continuous subarray of A[] whose sum is closest to 0? Or closest to a…
Peiti Li
  • 3,386
  • 7
  • 29
  • 45
11
votes
4 answers

Effcient way to find longest duplicate string for Python (From Programming Pearls)

From Section 15.2 of Programming Pearls The C codes can be viewed here: http://www.cs.bell-labs.com/cm/cs/pearls/longdup.c When I implement it in Python using suffix-array: example = open("iliad10.txt").read() def comlen(p, q): i = 0 for x…
Hanfei Sun
  • 39,245
  • 33
  • 107
  • 208
11
votes
6 answers

How do the bit manipulations in this bit-sorting code work?

Jon Bentley in Column 1 of his book programming pearls introduces a technique for sorting a sequence of non-zero positive integers using bit vectors. I have taken the program bitsort.c from here and pasted it below: /* Copyright (C) 1999 Lucent…
ardsrk
  • 2,357
  • 2
  • 19
  • 33
8
votes
4 answers

Programming Pearls: find one integer appears at least twice

It's in the section 2.6 and problem 2, the original problem is like this: "Given a sequential file containing 4,300,000,000 32-bit integers, how can you find one that appears at least twice?" My question toward this exercise is that: what is the…
Haiyuan Zhang
  • 36,164
  • 38
  • 101
  • 131
7
votes
2 answers

Bit Mask usage in the program below from Programming Pearls

I started reading "Programming Pearls" today and while doing it's exercise I came across this question "How would you implement your own bit vector?". When I looked at the solution it was like this: #define BITSPERWORD 32 #define SHIFT 5 #define…
test123
  • 12,405
  • 7
  • 24
  • 32
6
votes
6 answers

Find a missing 32bit integer among a unsorted array containing at most 4 billion ints

This is the problem described in Programming pearls. I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks. EDIT: I can understand binary search in general. I just can not understand how to apply…
pierrotlefou
  • 36,417
  • 33
  • 130
  • 168
6
votes
4 answers

Longest Non-Overlapping Substring

I wonder if anyone knows the (optimal?) algorithm for longest recurring non-overlapping sub string. For example, in the string ABADZEDGBADEZ the longest recurring would be "BAD". Incidentally if there is no such result, the algorithm should alert…
Brandon Pelfrey
  • 2,051
  • 5
  • 19
  • 22
4
votes
2 answers

Error in qsort function in Programming Pearls?

is it just me or this code in Programming Pearls is wrong (quicksort wants 2 const voids, no?) If so, is my solution right? Apologies, just learning... int wordncmp(char *p, char* q) { int n = k; for ( ; *p == *q; p++, q++) if (*p == 0…
Dervin Thunk
  • 17,583
  • 26
  • 110
  • 202
3
votes
1 answer

What is the best way to do java coding for these type of byte level operations?

I am reading on some problems that are about optimization approaches. In the problem how to sort numbers in a specific range the solution is to use a bitmap. And if a number can appear e.g. up to 10 times use to use half bytes to map the numbers and…
Cratylus
  • 49,824
  • 60
  • 195
  • 327
3
votes
7 answers

Debugging and Binary Search

"Programming Pearls" in the column 2 ("AHA! Algorithm") talks about how binary search aids in various processes like sorting, tree traversals. But it mentions that binary seach can be used in "program debugging". Can somebody please explain how this…
unj2
  • 47,759
  • 80
  • 235
  • 362
3
votes
4 answers

Maximum Countiguous Negative Sum or Mnimum positive subsequence sum problem

We all heard of bentley's beautiful proramming pearls problem which solves maximum subsequence sum: maxsofar = 0; maxcur = 0; for (i = 0; i < n; i++) { maxcur = max(A[i] + maxcur, 0); maxsofar = max(maxsofar, maxcur); } What if we add an…
atuls
  • 61
  • 6
3
votes
2 answers

Programming Pearls: Finding the missing integer in a file of 4 billion integers

Question: The input is on a sequential file. The file contains at most 4Billion integers. Find a missing integer. Solution as per my understanding: make two temporary files one with leading 0 and the other with leading 1 one of the two MUST( 4.3B…
1
2 3