EV_Minimize Array Cost infosys

PHOTO EMBED

Sun Mar 06 2022 22:14:59 GMT+0000 (Coordinated Universal Time)

Saved by @ayushshuklas

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,min=0,sum=0,k=0;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++)
	{
		cin >> a[i];	
	}
	    for(int i=0;i<n-1;i++)
		sum = sum + pow(abs(a[i] - a[i+1]),2);
		
		min = sum;
	
	    int b[n-1];
	
		for(int i=0;i<n-1;i++)
		{
			b[i] = (a[i] + a[i+1])/2;

		}
		int tt = 1;
	vector<vector<int> > d;
	int qwe=0;
	for(int i=0;i<n-1;i++)
	{
	    vector<int> v;
	for(int j=0;j<n;j++)
	{
	    	if((j == tt) && qwe ==0)
	    	{
			v.push_back(b[tt-1]);
			j--;
			qwe = 1;
	    	}
			else
	        v.push_back(a[j]);
	}
	tt++;
	qwe=0;
	d.push_back(v);
	}
	
			sum = 0;
			for(int i=0;i<n-1;i++)
			{
					for(int j=0;j<=n-1;j++)
					{
					    	sum = sum + pow(abs(d[i][j] - d[i][j+1]),2);
					}
					if(sum < min)
					min = sum;
					
					sum = 0;
			}
			
			cout << min;
	
}
content_copyCOPY

len(arr)-1 Σ(arri-arri-1)² i=1 len(arr) is the size of the array. Insert any integer at any location of the array such that the cost of the array is minimized. Find the minimum possible cost of the array after inserting exactly one element. Example a= [1, 3, 5, 2, 10] The cost of the array before insertion =(1-37+(3-57+(5-2)²+(2-102²=81. Two of many scenarios are shown below. 1. Insert 4 between 3 and 5, cost of array = (1-37+(3-47+(4-57+(5-2)²+(2 107²2² = 79. 2. Insert 6 between 2 and 10, cost of array =(1-37+(3-5)+(5-27²+(2-67² + (6 107² = 49. It can be proven that 49 is the minimum cost possible. Return 49. Function Description Complete the function getMinimumCost in the editor below. mumest fas the following parameter arrfet an array of ietsgers 1 A Returns 10 long in the minimum possible cont of the array after inserting one element Constraints ► Input Format For Custom Testing 21 Sample Case O Sample Input For Custom Testing STDIN FUNCTION → arr = arr [4, 7, 1, 4] Sample Output 36 Explanation The cost of the array before insertion = (4-77 +(7-17 +(1-47²=54. Insert 5 between 4 and 7, cost = (4-5)2+(5-77 +(7-17+(1-47-50. Insert 4 between 7 and 1, cost = (4-7)+(7 - 47² + (4 - 17² + (1 - 47)²=36.

https://ayushshuklas.blogspot.com