Day Before n Days

Last Updated : 30 Jul, 2026

Given two integers d and n. Where d is the day, out of 7 days of the week, d varies from 0 to 6 as shown below.

  • 0 - Sunday
  • 1 - Monday
  • 2 - Tuesday
  • 3 - Wednesday
  • 4 - Thursday
  • 5 - Friday
  • 6 - Saturday

You have to return the index for the day which is n days before the given day d.

Examples:

Input: d = 4, n = 3
Output: 1
Explanation: 3 days before the 4th is 1.

Input: d = 2, n = 19
Output: 4
Explanation: 19 days before the 2nd is 4.

Try It Yourself
redirect icon

[Naive Approach] Move Back One Day at a Time - O(n) Time and O(1) Space

The idea is to start from the given day d and move one day backward n times. Whenever the current day becomes less than 0, wrap it around to 6, which represents Saturday. After performing this operation n times, the resulting value represents the required day.

C++
#include <iostream>
using namespace std;

int nthDay(int d, int n) {
    // Move one day backward n times.
    for (int i = 0; i < n; i++) {
        d--;

        if (d < 0) {
            d = 6;
        }
    }

    return d;
}

int main() {
    int d1 = 4;
    int n1 = 3;
    cout << nthDay(d1, n1) << "\n";

    int d2 = 2;
    int n2 = 19;
    cout << nthDay(d2, n2) << "\n";

    return 0;
}
C
#include <stdio.h>

int nthDay(int d, int n) {
    // Move one day backward n times.
    for (int i = 0; i < n; i++) {
        d--;

        if (d < 0) {
            d = 6;
        }
    }

    return d;
}

int main() {
    int d1 = 4;
    int n1 = 3;
    printf("%d\n", nthDay(d1, n1));

    int d2 = 2;
    int n2 = 19;
    printf("%d\n", nthDay(d2, n2));

    return 0;
}
Java
class GFG {
    static int nthDay(int d, int n) {
        // Move one day backward n times.
        for (int i = 0; i < n; i++) {
            d--;

            if (d < 0) {
                d = 6;
            }
        }

        return d;
    }

    public static void main(String[] args) {
        int d1 = 4;
        int n1 = 3;
        System.out.println(nthDay(d1, n1));

        int d2 = 2;
        int n2 = 19;
        System.out.println(nthDay(d2, n2));
    }
}
Python
def nthDay(d, n):
    # Move one day backward n times.
    for _ in range(n):
        d -= 1

        if d < 0:
            d = 6

    return d


if __name__ == "__main__":
    d1 = 4
    n1 = 3
    print(nthDay(d1, n1))

    d2 = 2
    n2 = 19
    print(nthDay(d2, n2))
C#
using System;

class GFG
{
    static int nthDay(int d, int n)
    {
        // Move one day backward n times.
        for (int i = 0; i < n; i++)
        {
            d--;

            if (d < 0)
            {
                d = 6;
            }
        }

        return d;
    }

    static void Main()
    {
        int d1 = 4;
        int n1 = 3;
        Console.WriteLine(nthDay(d1, n1));

        int d2 = 2;
        int n2 = 19;
        Console.WriteLine(nthDay(d2, n2));
    }
}
JavaScript
function nthDay(d, n) {
    // Move one day backward n times.
    for (let i = 0; i < n; i++) {
        d--;

        if (d < 0) {
            d = 6;
        }
    }

    return d;
}

// Driver Code
const d1 = 4;
const n1 = 3;
console.log(nthDay(d1, n1));

const d2 = 2;
const n2 = 19;
console.log(nthDay(d2, n2));

Output
1
4

[Expected Approach] Using Modulo - O(1) Time and O(1) Space

The idea is - Since a week contains exactly 7 days, moving backward by every complete group of 7 days brings us back to the same day. Therefore, only n % 7 days affect the answer.

First reduce n using modulo 7. Then subtract it from d. Adding 7 before taking modulo ensures that the result remains non-negative.

C++
#include <iostream>
using namespace std;

int nthDay(int d, int n) {
    
    // Only the remainder after complete 
    // weeks affects the day.
    n %= 7;

    return (d - n + 7) % 7;
}

int main() {
    int d1 = 4;
    int n1 = 3;
    cout << nthDay(d1, n1) << "\n";

    int d2 = 2;
    int n2 = 19;
    cout << nthDay(d2, n2) << "\n";

    return 0;
}
C
#include <stdio.h>

int nthDay(int d, int n) {
    
    // Only the remainder after complete 
    // weeks affects the day.
    n %= 7;

    return (d - n + 7) % 7;
}

int main() {
    int d1 = 4;
    int n1 = 3;
    printf("%d\n", nthDay(d1, n1));

    int d2 = 2;
    int n2 = 19;
    printf("%d\n", nthDay(d2, n2));

    return 0;
}
Java
class GFG {
    static int nthDay(int d, int n) {
        
        // Only the remainder after complete
        // weeks affects the day.
        n %= 7;

        return (d - n + 7) % 7;
    }

    public static void main(String[] args) {
        int d1 = 4;
        int n1 = 3;
        System.out.println(nthDay(d1, n1));

        int d2 = 2;
        int n2 = 19;
        System.out.println(nthDay(d2, n2));
    }
}
Python
def nthDay(d, n):
    
    # Only the remainder after complete
    # weeks affects the day.
    n %= 7

    return (d - n + 7) % 7


if __name__ == "__main__":
    d1 = 4
    n1 = 3
    print(nthDay(d1, n1))

    d2 = 2
    n2 = 19
    print(nthDay(d2, n2))
C#
using System;

class GFG
{
    static int nthDay(int d, int n)
    {
        // Only the remainder after complete
        // weeks affects the day.
        n %= 7;

        return (d - n + 7) % 7;
    }

    static void Main()
    {
        int d1 = 4;
        int n1 = 3;
        Console.WriteLine(nthDay(d1, n1));

        int d2 = 2;
        int n2 = 19;
        Console.WriteLine(nthDay(d2, n2));
    }
}
JavaScript
function nthDay(d, n) {
    
    // Only the remainder after complete 
    // weeks affects the day.
    n %= 7;

    return (d - n + 7) % 7;
}

// Driver Code
const d1 = 4;
const n1 = 3;
console.log(nthDay(d1, n1));

const d2 = 2;
const n2 = 19;
console.log(nthDay(d2, n2));

Output
1
4
Comment