Skip to content

Instantly share code, notes, and snippets.

@GnsP
Last active August 29, 2015 14:20
Show Gist options
  • Save GnsP/e77edd056342109de1c2 to your computer and use it in GitHub Desktop.
Save GnsP/e77edd056342109de1c2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector> // a vector is a dynamically resizable array.
// ( Read about it from any standard C++ text book )
#include <algorithm> // include "algorithm" to use pre defined functions for
// commonly used algorithms like search, sort, max_element
// min_element etc
using namespace std;
int main(){
int N;
long long K, max, min;
cin >> N >> K;
vector<long long> arr(N); // allocate a vector of integers of size N
for(int i=0; i<N; i++) cin >> arr[i]; // read the numbers into the vector
// just like you do for an array
// when K==0 just print the array
if( K == 0 ) for(int i=0; i<N; i++) cout << arr[i] << " ";
else if( K & 0x1 ){ // if K is odd ( read about bitwise and operator and find out
// how the K & 0x1 works )
max = * max_element( arr.begin(), arr.end() ); // see the comment at the end
for(int i=0; i<N; i++) cout<< max - arr[i] << " ";
}
else { // if K is even
min = * min_element( arr.begin(), arr.end() );
for(int i=0; i<N; i++) cout<< arr[i]-min << " ";
}
// max_element and min_element take begin and end iterators to the vector and
// return the iterator to the max and min elements respectively. An iterator is
// like a pointer. So the access the number pointer by the iterator we use the '*'
// operator.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment