Written by @Yusif_M
In this problem there is a sequence of numbers and we have two things to do times.
Compute x as the sum of the first and last elements of .
Remove the first element of and append x to the end of .
There are 2 different ways to solve this problem using deque and using a vector.
Use deque and add sum of first and last element to deque then delete first element from deque. As deque is fast adding and deleting processes won't make time limit problem.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <string>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <unordered_set>
#define ll long long
#define ld long double
#define db double
#define ch char
#define sg string
#define st set
#define stck stack
#define pq priority_queue
#define dq deque
#define vc vector
#define bl bool
#define mp map
#define cp cout.precision
#define ts to_string
#define mst multiset
#define uost unordered_set
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n,k,num,sum,index=0; //index is equal to zero and index will be increased by one and the previous elements will be the elements which should be deleted
scanf("%lld %lld",&n,&k);
vc<ll>nums;
for(int i=0;i<n;i++)
{
scanf("%lld",&num);
nums.push_back(num);
}
for(int i=0;i<k;i++)
{
sum=nums[index]+nums[nums.size()-1],index++; //index is increased and now first element is not 0-th element it the element that seats one that index and previous ones are those which are removed
nums.push_back(sum); //add sum
}
for(int i=k;i<nums.size();i++) //start from m because deleting process has been done m times and the elements that seat on the indexes between 0 and m are deleted
{
printf("%lld ",nums[i]);
}
}Use vector and add sum of firsf and last element to vector but don't remove any element instead when you output, start from -th index while that index is smaller than vector's size.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <string>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <unordered_set>
#define ll long long
#define ld long double
#define db double
#define ch char
#define sg string
#define st set
#define stck stack
#define pq priority_queue
#define dq deque
#define vc vector
#define bl bool
#define mp map
#define cp cout.precision
#define ts to_string
#define mst multiset
#define uost unordered_set
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n,m,num,sum;
scanf("%lld %lld",&n,&k);
dq<ll>nums;
for(int i=0;i<n;i++)
{
scanf("%lld",&num);
nums.push_back(num);
}
for(int i=0;i<k;i++)
{
sum=nums[0]+nums[nums.size()-1];
nums.pop_front(),nums.push_back(sum); //delete first element and add sum
}
for(int i=0;i<nums.size();i++)
{
printf("%lld ",nums[i]);
}
}