Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active August 20, 2016 11:11
Show Gist options
  • Save rishi93/fef6b7f05bfedd2719f5 to your computer and use it in GitHub Desktop.
Save rishi93/fef6b7f05bfedd2719f5 to your computer and use it in GitHub Desktop.
Complete the sequence - CMPLS
#include <iostream>
#include <algorithm>
using namespace std;
bool ifStop(int arr[], int limit){
int first = arr[0];
for(int i=1; i<limit; i++){
if(arr[i] != first){
return false;
}
}
return true;
}
int main(){
//For Fast IO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,s,c,x;
cin>>t;
for(int testcase=0; testcase<t; testcase++){
cin>>s>>c;
if(s == 1){
cin>>x;
for(int ans=0; ans<c; ans++){
cout<<x<<" ";
}
cout<<"\n";
}
else{
int arr[s][s+c];
for(int i=0; i<s; i++){
fill_n(arr[i],s+c,0);
}
//Fill up the first row with the given numbers
for(int i=0; i<s; i++){
cin>>x;
arr[0][i] = x;
}
for(int i=1; i<s; i++){
for(int j=0; j<s-i; j++){
arr[i][j] = arr[i-1][j+1] - arr[i-1][j];
}
//Once all the differences are the same, then stop
if(ifStop(arr[i],s-i) == true){
//Fill the particular row with the same difference, predict constant
for(int index=1; index<s+c; index++){
arr[i][index] = arr[i][0];
}
//Fill up the previous rows, with the differences from the next row
for(int k=i-1; k>=0; k--){
for(int index=1; index<s+c-k; index++){
arr[k][index] = arr[k+1][index-1] + arr[k][index-1];
}
}
break;
}
}
//Print out the answers from the first row
for(int ans=0; ans<c; ans++){
cout<<arr[0][s+ans]<<" ";
}
cout<<"\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment