FastPrepWallet Balance on a Selected Day
Problem · Array

Wallet Balance on a Selected Day

Learn this problem
EasyBharatPe logoBharatPeFULLTIMEPHONE SCREEN

Problem statement

A wallet starts with balance 0. Its daily transactions are stored in an integer array transactions: a positive value is a credit, a negative value is a debit, and zero makes no change.

Given a zero-based index selectedDay, return the wallet balance at the end of that day. Include every transaction from day 0 through selectedDay. Return a 64-bit integer.

Function

balanceOnDay(transactions: int[], selectedDay: int) → long

Examples

Example 1

transactions = [100,-30,20,-10]selectedDay = 2return = 90

After days 0 through 2, the balance is 100 - 30 + 20 = 90.

Example 2

transactions = [-50,80,-20]selectedDay = 0return = -50

Only the first day's debit is included.

Example 3

transactions = [5,0,-2,7]selectedDay = 3return = 10

The full history sums to 5 + 0 - 2 + 7 = 10.

Constraints

  • 1 <= transactions.length <= 5000
  • -10^6 <= transactions[i] <= 10^6
  • 0 <= selectedDay < transactions.length

More BharatPe problems

drafts saved locally
public long balanceOnDay(int[] transactions, int selectedDay) {
    // write your code here
}
transactions[100,-30,20,-10]
selectedDay2
expected90
checking account