Opt In Stateful Smart Contract

If the account opts into a stateful smart contract then is it referenced at index 1 of Txn.accounts array?

so to write to its local state i`ll need to write to App.localPut(Int(1),Bytes(“key”),Int(1)) ?

The sender of the transaction (which is also the account that opts in, if it’s an optin call) is at index 0:

App.localPut(Int(0), Bytes("key"), Int(42))

see e.g., PyTeal Examples — PyTeal documentation

what if it is not opt in call , i want to write to local state of a account that has opted in stateful smart contract?

in that case i need to pass that account through transaction call?

App.localPut(Int(0), ...) write in the local storage of the sender of the application call transaction (i.e., the account making the smart contract call).
It does not need to be an optin call, it can be any call, as long as the sender opted in in the past (or it is an optin call).

If you want to write to the local storage of another account than the sender’s one, use foreign accounts.

See Overview - Algorand Developer Portal