Given a number N and an array A of N numbers. Print the smallest possible result of Ai + Aj + j - i , where 1 ≤ i < j ≤ N. Using C++.
#include <bits/stdc++.h>
using namespace std;
int input(int a[50], int n) {
int i, j, sum = 0, min_sum = INT_MAX;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= n; j++) {
sum += (a[i] + a[j] + j - i);
min_sum = min(min_sum, sum);
sum = 0;
}
}
return min_sum;
}
int main() {
int k, n, i;
cin >> k;
for (i = 0; i < k; i++) {
cin >> n;
int arr[n];
cout << input(arr, n) << " "<<endl;
}
return 0;
}
Comments
Post a Comment