 GFG App
Open App Browser
Continue

# Minimize given operations required to sort the stream array

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);
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
?>```
Output

`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up