Pancake sorting Problem

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

This problem is based on pancake sorting.

Given an unsorted array, we need to write a function which uses only flip operation to sort the array. flip is the operation which reverses the array.

We need to write a algorithm which as time complexity of O(nlogn), if we use pancake sorting algorithm the time taken will will be O() beacause in a loop we will find maximum element which takes O(n) time.

To reduce the time complexity we will use insertion sort, binary search and flip operation.

The main idea is to find the smallest element ie, arr[j] which is greater than arr[i] in arr[0,..i-1] using binary search. Now, we need to insert arr[i] just before arr[j] by using flip opoerations. 

Time Complexity: O(nlogn)


Till the end of the array

1. Find the index of smallest element which is greater than arr[i] in arr[0..i-1] ie, j = ceilSearch(arr, 0, i-1, arr[i]), Here 0 is the starting index, (i-1) is the ending index

        ceilSearch Algorithm:

      The smallest number which is greater than the given number is known as the ceiling of that number

        a. If the given element(arr[i]) is less than or equal to the first element in the array of given size, then return the index of the first element

        b. If the element is greater than last element, then return -1 ie, there is no greater element

        c. If the element is same as the mid element, then return the mid index

        d. If the element is greater than mid element, then either the next element to the mid element is the ceiling of the arr[i], or  ceiling lies in  arr[mid+1....high]

        e. If the element is less than mid element, then either the previous element to the mid     element is the ceiling of the arr[i], or ceiling lies in arr[low....mid-1]

2. Now, we need to insert the element arr[i] before arr[j] using flip operations

     a. flip the elements from index 0 to index j-1

     b. flip the elements from index 0 to index i-1

     c. flip the elements from index 0 to index i

     d. flip the elements from index 0 to index j

3. Print the sorted array.

The above algorithm can be clearly explained in the below diagram

C++ Program

#include <bits/stdc++.h>
using namespace std;

int flip(int arr[], int m) // Flipping the array till the index m
	int t;
	for(int i = 0; i< m; ++i)
		t = arr[i];
		arr[i] = arr[m];
		arr[m] = t;
//It is a binary search based function
//The array passed to ceilSearch will be sorted
//This function will return the smallest element index
//which is greater than x
int ceilSearch(int arr[], int low, int high, int x)
	int mid = (low+high)/2;
	if (x<=arr[low])//if x is less than the first element
		return low;
	if (x>arr[high])//if there is no element higher than x
		return -1;
	if (arr[mid]==x)//if x is the mid element
		return mid;
	if (arr[mid]<x) //if x is greater than mid element
		if (mid +1 <= high && x <= arr[mid +1])
			return mid +1;
			return ceilSearch(arr, mid+1, high, x);
	if (mid-1 >= low && x > arr[mid-1]) //if x is less than mid element
		return mid;
		return ceilSearch(arr, low, mid -1, x);
void pancakeSort(int arr[], int n)
	//start from the second element and insert arr[i]
	//in already sorted array arr[i-1] ie, similar to insertion sort
	for (int i = 1; i < n; ++i)
		int j = ceilSearch(arr, 0, i-1, arr[i]);
		//If there is a greater element  
		if (j != -1)
			//Using flip operations insert arr[i] before arr[j]
			flip(arr, j-1);
			flip(arr, i-1);
			flip(arr, i);
			flip(arr, j);
	//Printing the sorted array
	cout<<"The sorted array is [ ";
	for (int i = 0; i < n; ++i)
		cout<<arr[i]<<" ";
int main()
	int arr[]= {4, 1, 10, 23, 5};  //creating an array
    int n = sizeof(arr)/sizeof(arr[0]); //size of the array
    return 0;
Try It


C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top