0% found this document useful (0 votes)
20 views6 pages

Kth Smallest Element and Array Sorting

The document contains descriptions of 8 programming assignments: 1) Find the Kth smallest element in an array. 2) Sort an array of 0s, 1s and 2s in ascending order. 3) Find a subarray that sums to a given number S. 4) Move all negative elements to the end of the array. 5) Find the union of elements in two arrays. 6) Merge two sorted arrays. 7) Rotate an array of integers d positions to the left. 8) Remove duplicate elements from a sorted array.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Kth Smallest Element and Array Sorting

The document contains descriptions of 8 programming assignments: 1) Find the Kth smallest element in an array. 2) Sort an array of 0s, 1s and 2s in ascending order. 3) Find a subarray that sums to a given number S. 4) Move all negative elements to the end of the array. 5) Find the union of elements in two arrays. 6) Merge two sorted arrays. 7) Rotate an array of integers d positions to the left. 8) Remove duplicate elements from a sorted array.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Assignment1 : Find the Kth smallest element?

Given an array arr[] and an integer K where K is smaller than size of array,


the task is to find the Kth smallest element in the given array. It is given that
all array elements are distinct.
Example 1:
Input:
N=6
arr[] = 7 10 4 3 20 15
K=3
Output : 7
Explanation :
3rd smallest element in the given array is 7.

Example 2:
Input:
N=5
arr[] = 7 10 4 20 15
K=4
Output : 15
Explanation :
4th smallest element in the given array is 15.

Assignment 2 : Sort an array of 0s, 1s and 2s


Given an array of size N containing only 0s, 1s, and 2s; sort the array in
ascending order.
Example 1:
Input:
N=5
arr[]= {0 2 1 2 0}
Output:
00122
Explanation:
0s 1s and 2s are segregated into ascending order.
Example 2:
Input:
N=3
arr[] = {0 1 0}
Output:
001
Explanation:
0s 1s and 2s are segregated into ascending order.

Assignment3 : Subarray with given sum


Given an unsorted array A of size N that contains only non-negative integers,
find a continuous sub-array which adds to a given number S. In case of
multiple subarrays, return the subarray which comes first on moving from left
to right.
Example 1:
Input:
N = 5, S = 12
A[] = {1,2,3,7,5}
Output: 2 4
Explanation: The sum of elements from 2nd position to 4th position is 12.
Example 2:

Input:

N = 10, S = 15

A[] = {1,2,3,4,5,6,7,8,9,10}
Output: 1 5
Explanation: The sum of elements from 1st position to 5th position is 15.

Assignment 4: Move all negative elements to end


Given an unsorted array arr[] of size N having both negative and positive
integers. The task is place all negative element at the end of array without
changing the order of positive element and negative element.
Example 1:
Input :
N=8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output :
1 3 2 11 6 -1 -7 -5
Example 2:
Input :
N=8
arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output :
7 9 10 11 -5 -3 -4 -1

Assignment 5 : Union of two arrays


Given two arrays a[] and b[] of size n and m respectively. The task is to find
union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements
from both the arrays. If there are repetitions, then only one occurrence of
element should be printed in the union.

Example 1:
Input:
5 3
12345
123
Output:
5
Explanation:
1, 2, 3, 4 and 5 are the elements which come in the union set of both arrays. So
count is 5.
Example 2:
Input:
62
85 25 1 32 54 6
85 2
Output:
7
Explanation:
85, 25, 1, 32, 54, 6, and 2 are the elements which come in the union set of both
arrays. So count is 7.

Assignment 6 : Merge two sorted arrays

Given two sorted arrays, the task is to merge them in a sorted manner.
Examples: 
Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8} 
Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}

Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8} 


Output: arr3[] = {4, 5, 7, 8, 8, 9} 

Method :
1. Create an array arr3[] of size n1 + n2.
2. Copy all n1 elements of arr1[] to arr3[]
3. Traverse arr2[] and one by one insert elements of arr3[] to arr1[]. This
step take O(n1 * n2) time.

Assignment 7 : Program for array rotation

Given an array of integers arr[] of size N and an integer, the task is to rotate


the array elements to the left by d positions.

Examples:  
Input: 
arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: 3 4 5 6 7 1 2
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2
Output: 5 6 7 1 2 3 4

Approach 1 (Using temp array): This problem can be solved using the below
idea:
After rotating d positions to the left, the first d elements become the
last d elements of the array
 First store the elements from index d to N-1 into the temp array.
 Then store the first d elements of the original array into the temp array.
 Copy back the elements of the temp array into the original array

Assignment 8 : Remove duplicates from sorted array

Given a sorted array, the task is to remove the duplicate elements from the
array.
Examples: 
Input : arr[] = {2, 2, 2, 2, 2}
Output : arr[] = {2} new size = 1

Input : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}


Output : arr[] = {1, 2, 3, 4, 5} new size = 5

How to Solve

Method 1: (Using extra space) 


1. Create an auxiliary array temp[] to store unique elements.
2. Traverse input array and one by one copy unique elements of arr[] to
temp[]. Also keep track of count of unique elements. Let this count be j.
3. Copy j elements from temp[] to arr[] and return j
 

Common questions

Powered by AI

The concept of array unions exemplifies fundamental set operations in computer science, where distinct elements from two datasets are combined to form a new set, reflecting the theoretical union operation. An effective method for calculating the union of two arrays involves using hash sets or dictionaries to efficiently track seen elements, allowing the creation of a union in O(n + m) time complexity where n and m are the sizes of the arrays. This prevents duplication and ensures only unique elements are included .

The problem of array rotation provides insights into circular array handling, a common theme in data structures where the logical continuation of data structures forms a circular loop. Challenges with larger datasets include managing computational limits and ensuring efficiency with operations such as index recalculations to avoid overflow errors. Additionally, strategies to minimize extra space usage and adapt the operations into in-place versions are vital to handling scalability .

Set operations like union significantly impact efficiency in algorithm design, particularly when dealing with unsorted datasets. The efficiency stems from leveraging data structures like hash sets to ensure that operations remain constant average time O(1) for insertions and checks, leading to an overall time complexity of O(n + m) for union construction. This stands in stark contrast to naive comparisons which could potentially reach O(n*m) complexities. Efficient designs emphasize these optimized operations to make solutions scalable and performant, especially crucial with increasing data volumes .

Removing duplicates from a sorted array typically involves a two-pointer technique that effectively compares each element with its successor, copying only unique elements to a new position. This approach preserves data integrity by maintaining the order and reducing the dataset size to only necessary elements. The efficiency gains are significant, as the operation completes in linear time O(N) without requiring additional space beyond a few variables, directly enhancing performance in memory-constrained environments .

Merging two sorted arrays is more efficient than merging unsorted ones because the inherent order of the elements allows a linear scan, reducing time complexity to O(n1 + n2) where n1 and n2 are the sizes of the two arrays. The typical method used is the two-pointer technique, which simultaneously traverses both arrays and places elements into a new result array in sorted order. This is faster than the O((n1 + n2) * log(n1 + n2)) required for sorting unsorted arrays .

The identification of the Kth smallest element in an array is crucial in algorithmic problem-solving as it allows for efficient data queries such as finding percentiles and order statistics, which are key in data analysis and decision-making processes. An effective method for this task, especially when the elements are distinct, is Quickselect, a selection algorithm that is based on partitioning the elements around a pivot, similar to quicksort, but focusing only on one partition that contains the Kth element .

To find a subarray with a given sum in an array of non-negative integers, the sliding window (or two-pointer) technique is highly effective. This strategy involves maintaining a window of elements and adjusting its boundaries to either increase or decrease the sum dynamically, operating in O(N) time complexity. The brute-force method, which involves checking all possible subarrays, is impractical due to its O(N^2) time complexity, which becomes infeasible as the size of the array increases .

Maintaining order while moving all negative elements to the end of an array is significant as it preserves the original sequence of positive and negative numbers, which can be important for subsequent operations or analyses that rely on order. An efficient algorithm for this task employs the two-pointer approach with O(N) complexity, avoiding unnecessary shifts and preserving relative ordering without additional space .

The algorithmic approach to rotating an array to the left by 'd' positions involves several methods, with one intuitive strategy being the use of a temporary array to hold elements temporarily while they are repositioned. This method incurs O(N) time complexity due to the need to copy elements multiple times and typically uses O(d) additional space, where N is the total number of elements. An alternative in-place rotation method could optimize space usage to O(1) by reversing sections of the array through strategic swaps, aligning both time efficiency and space constraints .

Sorting an array containing only 0s, 1s, and 2s is a classic problem known as the Dutch National Flag problem, which highlights the efficiency of linear time solutions in managing simple yet critical tasks. The optimal strategy involves using a three-way partitioning technique that requires only two passes through the array, resulting in an O(N) time complexity, where N is the number of elements in the array. This method effectively segregates the values with minimal swaps and checks .

You might also like