Minimize given operations required to sort the stream array
Last Updated :
06 Dec, 2022
Given an array stream of n integers in the form of a stream, the task is to find the minimum number of operations that are required to sort the stream (increasing order) following the below steps:
- In one operation you can pick up the first element and then put that element into the xth index.
- All the elements to the left of the xth index shift one position left and the first element go to the next position.
- The position of the elements on the right of the xth position remains unchanged.
- One can choose different x independently for different elements.
Examples:
Input: n = 5, stream[] = {4, 2, 3, 5, 6}
Output: 1
Explanation: In the initial stream we can access only the first element which is 4 in this case and seHend it to x = 2 positions back due to which the number 2 in the stream will come at first position. The new stream becomes {2, 3, 4, 5, 6} which is already sorted in increasing order. Hence we require 1 operation.
Input: n = 4, stream[] = {2, 3, 5, 4}
Output: 3
Explanation: In first step we choose x = 2 for first element and the new stream becomes {3, 5, 2, 4}. In second step we choose x = 2 for first element and the new stream becomes {5, 2, 3, 4}. In the third step we choose x = 4 for first element and the new stream becomes {2, 3, 4, 5} which is sorted. Hence we require 3 operations.
Approach: The given problem can be solved by using a small observation.
Observations:
We always want to pick up the first element and put it in a place such that the suffix of the array remains sorted. Hence we traverse from the last element of the array and find the length of the longest suffix that is sorted in the initial array. Now for each remaining element other than those in the longest sorted suffix we would require an operation. Hence, the final result would be n - length of the longest sorted suffix.
Follow the steps to solve the problem:
- Initialize a variable say count equal to 1. This variable stores the length of the longest sorted suffix.
- Start iterating the array from the second last element.
- Check for conditions whether the current element is less than or equal to the element on its right or not.
- If the condition is true, increment count by 1 (count = count + 1).
- If the condition is false, break from the loop.
- The final result is n - count.
Below is the implementation of the above approach:
C++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum operations to
// stream sort the array
int minOpsToSort(int stream[], int n)
{
// Count variable stores the length of
// the longest sorted suffix which is
// initially 1 because last element
// (a single element) is already sorted
int count = 1;
for (int i = n - 2; i >= 0; i--) {
if (stream[i] <= stream[i + 1]) {
count++;
}
else {
break;
}
}
return n - count;
}
// Drivers code
int main()
{
// Stream array
int stream[] = { 2, 3, 5, 4 };
int n = sizeof(stream) / sizeof(stream[0]);
int minimumOperations = minOpsToSort(stream, n);
// Function Call
cout << minimumOperations;
return 0;
}
Java
public class Globals
{
// Function to find minimum operations to
// stream sort the array
public static int minOpsToSort(int[] stream, int n)
{
// Count variable stores the length of
// the longest sorted suffix which is
// initially 1 because last element
// (a single element) is already sorted
int count = 1;
for (int i = n - 2; i >= 0; i--)
{
if (stream[i] <= stream[i + 1])
count++;
else
break;
}
return n - count;
}
// Drivers code
public static void main(String[] args)
{
// Stream array
int[] stream = {2, 3, 5, 4};
int n = stream.length;
int minimumOperations = minOpsToSort(stream, n);
// Function Call
System.out.print(minimumOperations);
}
}
// This code is contributed by manav23lohani.
Python3
# Python code for the above approach:
# Function to find minimum operations to
# stream sort the array
def minOpsToSort(stream, n):
# Count variable stores the length of
# the longest sorted suffix which is
# initially 1 because last element
# (a single element) is already sorted
count = 1
for i in range(n-2, -1, -1):
if (stream[i] <= stream[i + 1]):
count += 1
else:
break
return n - count
# Drivers code
if __name__ == "__main__":
# Stream array
stream = [2, 3, 5, 4]
n = len(stream)
minimumOperations = minOpsToSort(stream, n)
# Function Call
print(minimumOperations)
# This code is contributed by Rohit Pradhan
C#
// C# code for the above approach:
using System;
public class Globals
{
// Function to find minimum operations to
// stream sort the array
public static int minOpsToSort(int[] stream, int n)
{
// Count variable stores the length of
// the longest sorted suffix which is
// initially 1 because last element
// (a single element) is already sorted
int count = 1;
for (int i = n - 2; i >= 0; i--)
{
if (stream[i] <= stream[i + 1])
count++;
else
break;
}
return n - count;
}
// Drivers code
public static void Main(string[] args)
{
// Stream array
int[] stream = {2, 3, 5, 4};
int n = stream.Length;
int minimumOperations = minOpsToSort(stream, n);
// Function Call
Console.WriteLine(minimumOperations);
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Function to find minimum operations to
// stream sort the array
function minOpsToSort(stream, n)
{
// Count variable stores the length of
// the longest sorted suffix which is
// initially 1 because last element
// (a single element) is already sorted
let count = 1;
for (let i = n - 2; i >= 0; i--) {
if (stream[i] <= stream[i + 1]) {
count++;
}
else {
break;
}
}
return n - count;
}
// Drivers code
// Stream array
let stream = [ 2, 3, 5, 4 ];
let n = stream.length;
let minimumOperations = minOpsToSort(stream, n);
// Function Call
console.log(minimumOperations);
// This code is contributed by akashish__
</script>
PHP
<?php
// Function to find minimum operations to
// stream sort the array
function minOpsToSort($stream, $n)
{
// Count variable stores the length of
// the longest sorted suffix which is
// initially 1 because last element
// (a single element) is already sorted
$count = 1;
for ($i = $n - 2; $i >= 0; $i--) {
if ($stream[$i] <= $stream[$i + 1]) {
$count++;
}
else {
break;
}
}
return $n - $count;
}
// Drivers code
// Stream array
$stream = array(2, 3, 5, 4);
$n = count($stream);
$minimumOperations = minOpsToSort($stream, $n);
// Function Call
echo $minimumOperations;
// This code is contributed by Kanishka Gupta
?>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum operations required to sort the array Given an array arr[], the task is to find the minimum operations required to sort the array in increasing order. In one operation, you can set each occurrence of one element to 0. Examples: Input: item[] = [4, 1, 5, 3, 2]Output: 4Explanation: Set arr[0], arr[1], arr[2], arr[3] = 0. Hence, the minimu
7 min read
Minimum operations required to Sort the Array using following operations Given an array arr[] of size N. Make the array arr[] sorted in non-decreasing order in the minimum number of operations where you can choose any integer x and replace elements arr[i] == x equal to 0 for, 0 ? i ? N - 1. Examples: Input: n = 3, arr[] = {3, 3, 2}Output: 1Explanation: If you choose x =
10 min read
Number of subarrays required to be rearranged to sort the given array Given an array arr[] consisting of the first N natural numbers, the task is to find the minimum number of subarrays required to be rearranged such that the resultant array is sorted. Examples: Input: arr[] = {2, 1, 4, 3, 5}Output: 1Explanation:Operation 1: Choose the subarray {arr[0], arr[3]}, i.e.
6 min read
Minimum increment or decrement operations required to make the array sorted Given an array arr[] of N integers, the task is to sort the array in non-decreasing order by performing the minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.Examples: Input: arr
15+ min read
Minimum number of steps required to obtain the given Array by the given operations Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only. Select any index i and increment all the elements at the indices [i, N - 1] by 1.Select any index i and decrease a
12 min read