Kth Smallest Element in a sorted array formed by reversing subarrays from a random index
Last Updated :
07 Sep, 2021
Given a sorted array arr[] of size N and an integer K, the task is to find Kth smallest element present in the array. The given array has been obtained by reversing subarrays {arr[0], arr[R]} and {arr[R + 1], arr[N - 1]} at some random index R. If the key is not present in the array, print -1.
Examples:
Input: arr[] = { 4, 3, 2, 1, 8, 7, 6, 5 }, K = 2
Output: 2
Explanation: Sorted form of the array arr[] is { 1, 2, 3, 4, 5, 6, 7, 8 }. Therefore, the 2nd smallest element in the array arr[] is 2.
Input: arr[] = { 10, 8, 6, 5, 2, 1, 13, 12 }, K = 3
Output: 5
Naive Approach: The simplest approach to solve the problem is to sort the given array arr[] in increasing order and print the Kth smallest element in the array.
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Alternative approach of the above solution: We can sort the array without using any sorting technique which will surely reduce the time complexity. We can just find the pivot point P in the array(around which the rotation occurs) using binary search and just reverse the two subarrays [0, P + 1] and [P + 1, N] using std::reverse() function in C++.
Reversing the subarrays: After finding the pivot point P, just find the reverse of the two subarrays as following:
std::reverse(arr, arr + P + 1);
std::reverse(arr + P + 1, arr + N);
And thus we get the sorted array and we can print the Kth smallest element as arr[K-1].
C++
// C++ program for the above approach.
#include <bits/stdc++.h>
using namespace std;
/* Function to get pivot. For array 4, 3, 2, 1, 6, 5
it returns 3 (index of 1) */
int findPivot(int arr[], int low, int high)
{
// base cases
if (high < low)
return -1;
if (high == low)
return low;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (mid < high && arr[mid] < arr[mid + 1])
return mid;
if (mid > low && arr[mid] > arr[mid - 1])
return (mid - 1);
if (arr[low] <= arr[mid])
return findPivot(arr, low, mid - 1);
return findPivot(arr, mid + 1, high);
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 10, 8, 6, 5, 2, 1, 13, 12 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
// Function Call
int P = findPivot(arr, 0, N - 1);
// reversing first subarray
reverse(arr, arr + P + 1);
// reversing second subarray
reverse(arr + P + 1, arr + N);
// printing output
cout << arr[K - 1];
return 0;
}
// This code is contributed by Pranjay Vats
Java
// Java program for the above approach.
import java.util.*;
class GFG{
// Function to get pivot. For array 4, 3, 2, 1, 6, 5
// it returns 3 (index of 1)
static int findPivot(int arr[], int low, int high)
{
// Base cases
if (high < low)
return -1;
if (high == low)
return low;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (mid < high && arr[mid] < arr[mid + 1])
return mid;
if (mid > low && arr[mid] > arr[mid - 1])
return (mid - 1);
if (arr[low] <= arr[mid])
return findPivot(arr, low, mid - 1);
return findPivot(arr, mid + 1, high);
}
static void reverse(int str[], int start, int end)
{
// Temporary variable to store character
int temp;
while (start <= end)
{
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 10, 8, 6, 5, 2, 1, 13, 12 };
int N = arr.length;
int K = 3;
// Function Call
int P = findPivot(arr, 0, N - 1);
// Reversing first subarray
reverse(arr, 0, P);
// Reversing second subarray
reverse(arr, P, N - 1);
// Printing output
System.out.print(arr[K - 1]);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach.
# Function to get pivot. For array 4, 3, 2, 1, 6, 5
# it returns 3 (index of 1)
def findPivot(arr, low, high):
# Base cases
if (high < low):
return -1
if (high == low):
return low
mid = int((low + high) / 2) #low + (high - low)/2;
if (mid < high and arr[mid] < arr[mid + 1]):
return mid
if (mid > low and arr[mid] > arr[mid - 1]):
return (mid - 1)
if (arr[low] <= arr[mid]):
return findPivot(arr, low, mid - 1)
return findPivot(arr, mid + 1, high)
def reverse(Str, start, end):
while (start <= end):
# Swapping the first and last character
temp = Str[start]
Str[start] = Str[end]
Str[end] = temp
start+=1
end-=1
# Given Input
arr = [ 10, 8, 6, 5, 2, 1, 13, 12 ]
N = len(arr)
K = 3
# Function Call
P = findPivot(arr, 0, N - 1)
# Reversing first subarray
reverse(arr, 0, P)
# Reversing second subarray
reverse(arr, P, N - 1)
# Printing output
print(arr[K - 1])
# This code is contributed by decode2207.
C#
// C# program for the above approach.
using System;
class GFG {
// Function to get pivot. For array 4, 3, 2, 1, 6, 5
// it returns 3 (index of 1)
static int findPivot(int[] arr, int low, int high)
{
// Base cases
if (high < low)
return -1;
if (high == low)
return low;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (mid < high && arr[mid] < arr[mid + 1])
return mid;
if (mid > low && arr[mid] > arr[mid - 1])
return (mid - 1);
if (arr[low] <= arr[mid])
return findPivot(arr, low, mid - 1);
return findPivot(arr, mid + 1, high);
}
static void reverse(int[] str, int start, int end)
{
// Temporary variable to store character
int temp;
while (start <= end)
{
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
static void Main() {
// Given Input
int[] arr = { 10, 8, 6, 5, 2, 1, 13, 12 };
int N = arr.Length;
int K = 3;
// Function Call
int P = findPivot(arr, 0, N - 1);
// Reversing first subarray
reverse(arr, 0, P);
// Reversing second subarray
reverse(arr, P, N - 1);
// Printing output
Console.Write(arr[K - 1]);
}
}
// This code is contributed by divyesh072019.
JavaScript
<script>
// Javascript program for the above approach.
/* Function to get pivot. For array 4, 3, 2, 1, 6, 5
it returns 3 (index of 1) */
function findPivot(arr, low, high)
{
// base cases
if (high < low)
return -1;
if (high == low)
return low;
let mid = parseInt((low + high) / 2, 10); /*low + (high - low)/2;*/
if (mid < high && arr[mid] < arr[mid + 1])
return mid;
if (mid > low && arr[mid] > arr[mid - 1])
return (mid - 1);
if (arr[low] <= arr[mid])
return findPivot(arr, low, mid - 1);
return findPivot(arr, mid + 1, high);
}
function reverse(i, j, arr)
{
let y = j - 1;
for(let x = i; x < j; x++)
{
arr[x] = arr[y];
y--;
}
}
// Given Input
let arr = [ 10, 8, 6, 5, 2, 1, 13, 12 ];
let N = arr.length;
let K = 3;
// Function Call
let P = findPivot(arr, 0, N - 1);
// reversing first subarray
reverse(0, P + 1, arr);
// reversing second subarray
reverse(P + 1, N, arr);
// printing output
document.write(arr[K - 1]);
// This code is contributed by divyeshrabadiya07.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is based on the observation that the Rth element is the smallest element because the elements in the range [1, R] are reversed. Now, if the random index is R, it means subarray [1, R] and [R + 1, N] are sorted in decreasing order. Therefore, the task reduceS to finding the value of R which can be obtained using binary search. Finally, print the Kth smallest element.
Follow the steps below to solve the problem:
- Initialize l as 1 and h as N to store the boundary elements index of the search space for the binary search.
- Loop while the value of l+1 < h
- Store the middle element in a variable, mid as (l+h)/2.
- If arr[l] ≥ arr[mid]. If it is true then check on the right side of mid by updating l to mid.
- Otherwise, update r to mid.
- Now after finding R, if K ≤ R, then the answer is arr[R-K+1]. Otherwise, arr[N-(K-R)+1].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the Kth element in a
// sorted and rotated array at random point
int findkthElement(vector<int> arr, int n, int K)
{
// Set the boundaries for binary search
int l = 0;
int h = n - 1, r;
// Apply binary search to find R
while (l + 1 < h)
{
// Initialize the middle element
int mid = (l + h) / 2;
// Check in the right side of mid
if (arr[l] >= arr[mid])
l = mid;
// Else check in the left side
else
h = mid;
}
// Random point either l or h
if (arr[l] < arr[h])
r = l;
else
r = h;
// Return the kth smallest element
if (K <= r + 1)
return arr[r + 1 - K];
else
return arr[n - (K - (r + 1))];
}
// Driver Code
int main()
{
// Given Input
vector<int> arr = { 10, 8, 6, 5, 2, 1, 13, 12 };
int n = arr.size();
int K = 3;
// Function Call
cout << findkthElement(arr, n, K);
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
class GFG{
// Function to find the Kth element in a
// sorted and rotated array at random point
public static int findkthElement(int arr[], int n, int K)
{
// Set the boundaries for binary search
int l = 0;
int h = n - 1, r;
// Apply binary search to find R
while (l + 1 < h)
{
// Initialize the middle element
int mid = (l + h) / 2;
// Check in the right side of mid
if (arr[l] >= arr[mid])
l = mid;
// Else check in the left side
else
h = mid;
}
// Random point either l or h
if (arr[l] < arr[h])
r = l;
else
r = h;
// Return the kth smallest element
if (K <= r + 1)
return arr[r + 1 - K];
else
return arr[n - (K - (r + 1))];
}
// Driver Code
public static void main(String args[])
{
// Given Input
int []arr = { 10, 8, 6, 5, 2, 1, 13, 12 };
int n = arr.length;
int K = 3;
// Function Call
System.out.println(findkthElement(arr, n, K));
}
}
// This code is contributed by SoumikMondal
Python3
# Python program for the above approach
# Function to find the Kth element in a
# sorted and rotated array at random point
def findkthElement(arr, n, K):
# Set the boundaries for binary search
l = 0
h = n-1
# Apply binary search to find R
while l+1 < h:
# Initialize the middle element
mid = (l+h)//2
# Check in the right side of mid
if arr[l] >= arr[mid]:
l = mid
# Else check in the left side
else:
h = mid
# Random point either l or h
if arr[l] < arr[h]:
r = l
else:
r = h
# Return the kth smallest element
if K <= r+1:
return arr[r+1-K]
else:
return arr[n-(K-(r+1))]
# Driver Code
if __name__ == "__main__":
# Given Input
arr = [10, 8, 6, 5, 2, 1, 13, 12]
n = len(arr)
K = 3
# Function Call
print(findkthElement(arr, n, K) )
C#
using System.IO;
using System;
class GFG {
// Function to find the Kth element in a
// sorted and rotated array at random point
public static int findkthElement(int[] arr, int n,
int K)
{
// Set the boundaries for binary search
int l = 0;
int h = n - 1, r;
// Apply binary search to find R
while (l + 1 < h) {
// Initialize the middle element
int mid = (l + h) / 2;
// Check in the right side of mid
if (arr[l] >= arr[mid])
l = mid;
// Else check in the left side
else
h = mid;
}
// Random point either l or h
if (arr[l] < arr[h])
r = l;
else
r = h;
// Return the kth smallest element
if (K <= r + 1)
return arr[r + 1 - K];
else
return arr[n - (K - (r + 1))];
}
static void Main()
{
// Given Input
int[] arr = { 10, 8, 6, 5, 2, 1, 13, 12 };
int n = arr.Length;
int K = 3;
// Function Call
Console.WriteLine(findkthElement(arr, n, K));
}
}
// This code is contributed by abhinavjain194.
JavaScript
<script>
// Function to find the Kth element in a
// sorted and rotated array at random point
function findkthElement(arr, n, K) {
// Set the boundaries for binary search
var l = 0;
var h = n - 1,
r;
// Apply binary search to find R
while (l + 1 < h) {
// Initialize the middle element
var mid = parseInt((l + h) / 2);
// Check in the right side of mid
if (arr[l] >= arr[mid]) l = mid;
// Else check in the left side
else h = mid;
}
// Random point either l or h
if (arr[l] < arr[h]) r = l;
else r = h;
// Return the kth smallest element
if (K <= r + 1) return arr[r + 1 - K];
else return arr[n - (K - (r + 1))];
}
// Given Input
var arr = [10, 8, 6, 5, 2, 1, 13, 12];
var n = arr.length;
var K = 3;
// Function Call
document.write(findkthElement(arr, n, K));
</script>
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Similar Reads
Search an element in a sorted array formed by reversing subarrays from a random index Given a sorted array arr[] of size N and an integer key, the task is to find the index at which key is present in the array. The given array has been obtained by reversing subarrays {arr[0], arr[R]} and {arr[R + 1], arr[N - 1]} at some random index R. If the key is not present in the array, print -1
8 min read
Length of smallest subarray to be removed such that the remaining array is sorted Given an array arr[] consisting of N integers, the task is to print the length of the smallest subarray to be removed from arr[] such that the remaining array is sorted. Examples: Input: arr[] = {1, 2, 3, 10, 4, 2, 3, 5}Output: 3Explanation:The smallest subarray to be remove is {10, 4, 2} of length
8 min read
Print n smallest elements from given array in their original order We are given an array of m-elements, we need to find n smallest elements from the array but they must be in the same order as they are in given array. Examples: Input : arr[] = {4, 2, 6, 1, 5}, n = 3 Output : 4 2 1 Explanation : 1, 2 and 4 are 3 smallest numbers and 4 2 1 is their order in given arr
5 min read
Lexicographically smallest Permutation of Array by reversing at most one Subarray Given an array arr[] of size N which is a permutation from 1 to N, the task is to find the lexicographically smallest permutation that can be formed by reversing at most one subarray. Examples: Input : arr[] = {1, 3, 4, 2, 5}Output : 1 2 4 3 5Explanation: The subarray from index 1 to index 3 can be
8 min read
Find the Kth occurrence of an element in a sorted Array Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at
15+ min read
Find the Kth smallest element in the sorted generated array Given an array arr[] of N elements and an integer K, the task is to generate an B[] with the following rules: Copy elements arr[1...N], N times to array B[].Copy elements arr[1...N/2], 2*N times to array B[].Copy elements arr[1...N/4], 3*N times to array B[].Similarly, until only no element is left
8 min read
Sum of minimum element of all subarrays of a sorted array Given a sorted array A of n integers. The task is to find the sum of the minimum of all possible subarrays of A. Examples: Input: A = [ 1, 2, 4, 5] Output: 23 Subsequences are [1], [2], [4], [5], [1, 2], [2, 4], [4, 5] [1, 2, 4], [2, 4, 5], [1, 2, 4, 5] Minimums are 1, 2, 4, 5, 1, 2, 4, 1, 2, 1. Sum
4 min read
Sum of minimum element of all sub-sequences of a sorted array Given a sorted array A of n integers. The task is to find the sum of the minimum of all possible subsequences of A.Note: Considering there will be no overflow of numbers. Examples: Input: A = [1, 2, 4, 5] Output: 29 Subsequences are [1], [2], [4], [5], [1, 2], [1, 4], [1, 5], [2, 4], [2, 5], [4, 5]
4 min read
Kâth Smallest/Largest Element in Unsorted Array | Worst case Linear Time Given an array of distinct integers arr[] and an integer k. The task is to find the k-th smallest element in the array. For better understanding, k refers to the element that would appear in the k-th position if the array were sorted in ascending order. Note: k will always be less than the size of t
15 min read
Print X array elements closest to the Kth smallest element in the array Given two integers K, X, and an array arr[] consisting of N distinct elements, the task is to find X elements closest to the Kth smallest element from the given array. Examples: Input: arr[] = {1, 2, 3, 4, 10}, K = 3, X = 2Output: 2 3Explanation: Kth smallest element present in the given array is 3
15+ min read