Very Easy and Simple Code ... Whole code in no more than 5 line


#1

vector Solution::getRow(int A) {
vector arr={1};
for(int i=0;i<A;i++)
arr.push_back((arr[i]*(A-i))/(i+1));
return arr;
}


#2

what does this statement do?
vector arr={1};
and whats the logic used in the code?


#3

@harshit0001
it is equivalent to
vector arr;
arr.push_back(1);
the first element is initialised to 1
elegent solution by the way
please and thank you


#4

concept of nCr and nCr+1 is used here as elements of the kth row would be [1,nC1,nC2,nC3,…nCr,nCr+1,…1]. and nCr+1=nCr*(n-r)/(r+1). therefore every next element of the row can easily be found by the previous element with the help of this formula.I hope its clear :slight_smile: