Minimum steps for increasing and decreasing Array to reach either 0 or N
Last Updated :
28 Feb, 2022
Given an integer N and two arrays increasing[] and decreasing[], such that they have elements from 1 to N only. The task is to find the minimum number of steps for each element of the two arrays to reach either 0 or N. A step is defined as follows:
- In one step, all the elements of the increasing[] array increases by 1, and all the elements of the decreasing[] array decreases by 1.
- When an element becomes either 0 or N, no more increase or decrease operation is performed on it.
Examples:
Input: N = 5, increasing[] = {1, 2}, decreasing[] = {3, 4}
Output: 4
Explanation:
Step 1: increasing[] array becomes {2, 3}, decreasing[] = {2, 3}
Step 2: increasing[] array becomes {3, 4}, decreasing[] = {1, 2}
Step 3: increasing[] array becomes {4, 5}, decreasing[] = {0, 1}
Step 4: increasing[] array becomes {5, 5}, decreasing[] = {0, 0}
4 Steps are required for all elements to become either 0 or N. Hence, the output is 4.
Input: N = 7, increasing[] = {3, 5}, decreasing[] = {6}
Output: 6
Approach: The idea is to find the maximum between the steps required by all the elements of the increasing[] array and the decreasing[] array to reach N and 0 respectively. Below are the steps:
- Find the minimum element of the array increasing[].
- The maximum steps taken by all the elements of the increasing[] array to reach N is given by N – min(increasing[]).
- Find the maximum element of the array decreasing[].
- The maximum steps taken by all the elements of the decreasing[] array to reach 0 is given by max(decreasing[]).
- Therefore, the minimum number of steps when all the elements become either 0 or N is given by max(N – min(increasing[]), max(decreasing[])).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minSteps( int N, int increasing[],
int decreasing[], int m1, int m2)
{
int mini = INT_MAX;
for ( int i = 0; i < m1; i++)
{
if (mini > increasing[i])
mini = increasing[i];
}
int maxi = INT_MIN;
for ( int i = 0; i < m2; i++)
{
if (maxi < decreasing[i])
maxi = decreasing[i];
}
int minSteps = max(maxi,
N - mini);
cout << minSteps << endl;
}
int main()
{
int N = 7;
int increasing[] = { 3, 5 };
int decreasing[] = { 6 };
int m1 = sizeof (increasing) / sizeof (increasing[0]);
int m2 = sizeof (decreasing) / sizeof (decreasing[0]);
minSteps(N, increasing, decreasing, m1, m2);
}
|
Java
import java.util.*;
public class GFG {
public static void
minSteps( int N, int [] increasing,
int [] decreasing)
{
int min = Integer.MAX_VALUE;
for ( int i : increasing) {
if (min > i)
min = i;
}
int max = Integer.MIN_VALUE;
for ( int i : decreasing) {
if (max < i)
max = i;
}
int minSteps = Math.max(max,
N - min);
System.out.println(minSteps);
}
public static void main(String[] args)
{
int N = 7 ;
int increasing[] = { 3 , 5 };
int decreasing[] = { 6 };
minSteps(N, increasing, decreasing);
}
}
|
Python3
import sys
def minSteps(N, increasing, decreasing):
Min = sys.maxsize;
for i in increasing:
if ( Min > i):
Min = i;
Max = - sys.maxsize;
for i in decreasing:
if ( Max < i):
Max = i;
minSteps = max ( Max , N - Min );
print (minSteps);
if __name__ = = '__main__' :
N = 7 ;
increasing = [ 3 , 5 ];
decreasing = [ 6 ];
minSteps(N, increasing, decreasing);
|
C#
using System;
class GFG{
public static void minSteps( int N, int [] increasing,
int [] decreasing)
{
int min = int .MaxValue;
foreach ( int i in increasing)
{
if (min > i)
min = i;
}
int max = int .MinValue;
foreach ( int i in decreasing)
{
if (max < i)
max = i;
}
int minSteps = Math.Max(max,
N - min);
Console.WriteLine(minSteps);
}
public static void Main(String[] args)
{
int N = 7;
int []increasing = { 3, 5 };
int []decreasing = { 6 };
minSteps(N, increasing, decreasing);
}
}
|
Javascript
<script>
function minSteps(N, increasing, decreasing, m1, m2)
{
var mini = 2147483647;
var i;
for (i = 0; i < m1; i++)
{
if (mini > increasing[i])
mini = increasing[i];
}
var maxi = -2147483648;
for (i = 0; i < m2; i++)
{
if (maxi < decreasing[i])
maxi = decreasing[i];
}
var minSteps = Math.max(maxi,N - mini);
document.write(minSteps);
}
var N = 7;
var increasing = [3, 5];
var decreasing = [6];
var m1 = increasing.length;
var m2 = decreasing.length;
minSteps(N, increasing, decreasing, m1, m2);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...