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.
Questions tagged [programming-pearls]
41 questions
40
votes
26 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
- 49,896
- 32
- 148
- 184
23
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,824
- 3
- 23
- 36
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
12
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
- 4,634
- 9
- 40
- 57
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
- 45,281
- 39
- 129
- 237
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,407
- 2
- 21
- 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
- 40,802
- 41
- 107
- 134
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
- 13,865
- 9
- 28
- 33
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
- 39,805
- 37
- 135
- 175
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,449
- 6
- 22
- 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
- 19,515
- 28
- 127
- 217
4
votes
2 answers
Constant time for initialization by using more space-Programming pearls - Column 1
I was reading "Programming Pearls" and I am really confused in one of the solution explanations--problem 9 in column 1.
The question was: When using bitmap data to represent a set of integers, the first phase initializes the set to empty. But…

Fihop
- 3,127
- 9
- 42
- 65
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
- 52,998
- 69
- 209
- 339
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
- 52,135
- 87
- 247
- 375
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