Language: C++

#include"iostream"
#include"vector"
#include"algorithm"
using namespace std;
 
int main()
{
	int n, k;
	cin >> n >> k;
	vector<int> x(n);
	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
	}
 
	int ans = 2*x[n-1]-2*x[0];
	for (int i = 0; i < n - k + 1; i++)
	{
		if (x[i] < 0 && x[i + k - 1] < 0)
		{
			ans = min({ ans,(-1)*x[i]) });
		}
		else if (x[i] > 0 && x[i + k - 1] > 0)
		{
			ans = min({ ans,x[i + k - 1] });
		}
		else
		{
			ans = min({ ans,2 * x[i + k - 1] - x[i],x[i + k - 1] - 2*x[i] });
		}
	}
	cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:21:50: error: expected '}' before ')' token
   21 |                         ans = min({ ans,(-1)*x[i]) });
      |                                   ~              ^
a.cc:21:51: error: expected ';' before '}' token
   21 |                         ans = min({ ans,(-1)*x[i]) });
      |                                                   ^~
      |                                                   ;
a.cc:21:53: error: expected primary-expression before ')' token
   21 |                         ans = min({ ans,(-1)*x[i]) });
      |                                                     ^
a.cc:23:17: error: 'else' without a previous 'if'
   23 |                 else if (x[i] > 0 && x[i + k - 1] > 0)
      |                 ^~~~
a.cc:23:28: error: 'i' was not declared in this scope
   23 |                 else if (x[i] > 0 && x[i + k - 1] > 0)
      |                            ^
a.cc:25:34: error: no matching function for call to 'min(<brace-enclosed initializer list>)'
   25 |                         ans = min({ ans,x[i + k - 1] });
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 1 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   candidate expects 2 arguments, 1 provided
a.cc:29:34: error: no matching function for call to 'min(<brace-enclosed initializer list>)'
   29 |                         ans = min({ ans,2 * x[i + k - 1] - x[i],x[i + k - 1] - 2*x[i] });
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   candidate expects 2 arguments, 1 provided
a.cc: At global scope:
a.cc:32:9: error: 'cout' does not name a type
   32 |         cout << ans << endl;
      |         ^~~~
a.cc:33:1: error: expected declaration before '}' token
   33 | }
      | ^