Lab CS213 - 21 03 2023 PDF
Lab CS213 - 21 03 2023 PDF
Maximum Gap
Given an integer array nums, return the maximum difference between two
successive elements in its sorted form. If the array contains less than two elements,
return 0.
You must write an algorithm that runs in linear time and uses linear extra space.
Input: First line takes a positive integer n as input. Second line takes n space
separated integer as input for the array.
Output: Integer representing maximum difference.
Example
Input: 4
3691
Output:3
Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the
maximum difference 3.
Input: 1
5
Output:0
Explanation: The array contains less than 2 elements, therefore return 0
Algorithm
Use the linear time sorting technique with linear extra space and then find out the
maximum difference between two consecutive elements.
1. The first step is to find the maximum value in nums array, it will
be the threshold to end the loop.
2. Then use the radix sort algorithm to sort based on each digit from Least
Significant Bit(LSB) to Most Significant Bit (MSB),
3. Finally, find the maximum gap from the sorted array.
1. radixSort(arr)
2. max = largest element in the given array
3. d = number of digits in the largest element (or, max)
4. Now, create d buckets of size 0 - 9
5. for i -> 0 to d
6. sort the array elements using counting sort (or any stable sort) according to
the digits at the ith place
Input: First line takes a positive integer n as input. Second line takes n space
separated strings as input for the array.
Output: Sorted array in ascending order.
Example:
Input:12
dab add cab fad fee bad dad bee fed bed ebb ace
Output:ace add bad bed bee cab dab dad ebb fad fed fee
Explanation
The main idea is
● Partition the array into R pieces according to the first character(use key-
indexed counting).
● Recursively sort all strings that start with each character(key-indexed counts
delineate subarrays to sort
Algorithm
radixMSD(int * items, int left, int right, int * scratch, int digit_position)
1. If the digit position is greater than the number of digits in the items,
return.
2. If right <= left, return.
3. Count the number of items for each bucket.
4. Figure out where each bucket should be stored (positions of the first and last
element of the bucket in the scratch array).
5.Copy each item to the corresponding bucket (in the scratch array).
6. Copy the scratch array back into items.
7.For each bucket:
8. • new_left = leftmost position of bucket in items
9. • new_right = rightmost position of bucket in items
10. • radixMSD(items, new_left, new_right, scratch, digit_position+1)
MSD Radix sort takes O(N + MB), where M = length of the longest string and B =
size of radix (B=10 possible numbers or B=256 characters or B=2 for Binary).
#include <stdio.h>
#include <string.h>
int main ()
{
char buf[] ="abc qwe ccd";
int i = 0;
char *p = strtok (buf, " ");
char *array[3];
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, " ");
}
return 0;
}
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;