Optimal Array
Last Updated :
10 Apr, 2023
Given a sorted array A[] of length N. For each i(0 ≤ i ≤ n-1), you have to make all the elements of the array from index 0 to i equal, using the minimum number of operations. In one operation you either increase or decrease the array element by 1. the task is to return a list that contains the minimum number of operations for each i, to accomplish the above task.
Note:
- 0-based indexing.
- For each index, you need to consider the same array which was given to you at the start.
Examples:
Input: N = 4, A[] = {1, 6, 9, 12}
Output: 0 5 8 14
Input: N = 7, A[] ={1, 1, 1, 7, 7, 10, 19}
Output: 0 0 0 6 12 21 33
Explanation: Possible operations could be:
{1}->{1}
{1, 1}->{1, 1}
{1, 1, 1}->{1, 1, 1}
{1, 1, 1, 7}->{1, 1, 1, 1}
{1, 1, 1, 7, 7}->{1, 1, 1, 1, 1}
{1, 1, 1, 7, 7, 10}->{5, 5, 5, 5, 5, 5}
{1, 1, 1, 7, 7, 10, 19}->{7, 7, 7, 7, 7, 7, 7}
Approach: This can be solved by the following idea:
We basically apply the concept of median here as there will be less operation required in changing all elements into their median but here we need to focus on one point if we take the median for odd n/2 and even n/2 + n/2+ 1 average then we will require two loops and we run out of the time (TLE) so we will take median as n/2 and subtract it from every value and then all it and it will be our answer or we can say that just add (i-1)th element operation with a value of i index subtracting medium from it.
Steps involved in the implementation of the code:
- Initialize an array of the long type which will store the value of the minimum operation required and name it “ans”.
- Set the 0th index value of ans = 0 because any value is equal to itself.
- Take an integer variable let’s name it “res” initialize it with 1 and use it as the reference index of ans.
- Start the for loop and inside it set the condition ans[res] = ans[i-1]+(a[i]-a[i/2]) which means the operation required to change all values from 0th to i-1 index + value of ith index – median value and assign this value in ans.
- Increment “res” by 1 to go to the next index of ans and store its value in the next iteration.
Below is the code of the above approach:
C++14
#include <iostream>
#include <vector>
using namespace std;
vector< long long > optimalArray( int n, int a[])
{
vector< long long > ans(n);
ans[0] = 0;
int res = 1;
for ( int i = 1; i < n; i++) {
ans[res] = ans[i - 1] + (a[i] - a[i / 2]);
res++;
}
return ans;
}
void printArray(vector< long long > arr, int size)
{
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
cout << endl;
}
int main()
{
int n = 4;
int a[] = { 1, 6, 9, 12 };
vector< long long > ans = optimalArray(n, a);
printArray(ans, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static public long [] optimalArray( int n, int a[])
{
long ans[] = new long [n];
ans[ 0 ] = 0 ;
int res = 1 ;
for ( int i = 1 ; i < n; i++) {
ans[res] = ans[i - 1 ] + (a[i] - a[i / 2 ]);
res++;
}
return ans;
}
static void printArray( long arr[], int size)
{
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
public static void main(String[] args)
{
int n = 4 ;
int [] a = { 1 , 6 , 9 , 12 };
long [] ans = optimalArray(n, a);
printArray(ans, n);
}
}
|
Python3
def optimalArray(n, a):
ans = [ 0 ] * n
ans[ 0 ] = 0
res = 1
for i in range ( 1 , n):
ans[res] = ans[i - 1 ] + (a[i] - a[i / / 2 ])
res + = 1
return ans
def printArray(arr, size):
for i in range (size):
print (arr[i], end = ' ' )
print ()
n = 4
a = [ 1 , 6 , 9 , 12 ]
ans = optimalArray(n, a)
printArray(ans, n)
|
Javascript
function optimalArray(n, a)
{
const ans = new Array(n);
ans[0] = 0;
let res = 1;
for (let i = 1; i < n; i++) {
ans[res] = ans[i - 1] + (a[i] - a[Math.floor(i / 2)]);
res++;
}
return ans;
}
function printArray(arr, size) {
for (let i = 0; i < size; i++)
process.stdout.write(arr[i] + " " );
}
const n = 4;
const a = [1, 6, 9, 12];
const ans = optimalArray(n, a);
printArray(ans, n);
|
C#
using System;
using System.Collections.Generic;
public class Program {
static List< long > OptimalArray( int n, int [] a)
{
List< long > ans = new List< long >(n);
ans.Add(0);
int res = 1;
for ( int i = 1; i < n; i++) {
ans.Add(ans[i - 1] + (a[i] - a[i / 2]));
res++;
}
return ans;
}
static void PrintArray(List< long > arr, int size)
{
for ( int i = 0; i < size; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
static void Main()
{
int n = 4;
int [] a = { 1, 6, 9, 12 };
List< long > ans = OptimalArray(n, a);
PrintArray(ans, n);
}
}
|
Time Complexity: O(n), where n is the size of the array ‘a’, because the code contains a single for-loop that iterates n-1 times. Therefore, the time complexity is linear in terms of the input size.
Auxiliary Space: O(n), because it creates a vector ‘ans’ of size n to store the minimum operations required to make all elements equal. Therefore, the space complexity is also linear in terms of the input size.
Share your thoughts in the comments
Please Login to comment...