easy
1 views

A Sober Walk – TCS NQT Question 1

Calculate the final position of a person who starts at origin and moves in a spiral pattern with increasing distances

Understand the Problem

Problem Statement

Our hoary culture had several great persons since time immemorial and king Vikramaditya’s Nava Ratnas (nine gems) belongs to this ilk. They are named in the following shloka:

धनवंतरी क्षषणकाडमरसिंह राड़ चेठालमदू धटकर्परः कर्मिदाक
ख्यति कराहमिहिरि नृम््ते समाभ्य्म रत्नति वै कस्मस्मिनति तिम्द्म्‌

Among these, Varahamihira was an astrologer of eminence, and his book Brihat Jataka is recokoned as the ultimate authority in astrology.

He was once talking with Amarasimha, another gem among the Nava Ratnas and the author of Sanskrit thesaurus, Amarakosha.

Amarasimha wanted to know the final position of a person, who starts from the origin (0, 0) and travels per the following scheme.

Scheme

  • He first turns and travels 10 units of distance
  • His second turn is upward for 20 units
  • Third turn is to the left for 30 units
  • Fourth turn is the downward for 40 units
  • Fifth turn is to the right(again) for 50 units

… And thus he travels, every time increasing the travel distance by 10 units.

Constraints

  • Number of moves (n) will be between 1 and 1000
  • Starting position is always (0, 0)
  • Direction pattern repeats every 5 moves: Right → Up → Left → Down → Right
  • Distance increases by 10 units after each move
  • Coordinates will fit within standard integer range

Examples

Example 1
Input
3
Output
-20 20
Explanation

Starting from (0,0): 1. Move Right 10 units → (10, 0) 2. Move Up 20 units → (10, 20) 3. Move Left 30 units → (-20, 20)

Example 2
Input
4
Output
-20 -20
Explanation

Continuing from previous: 4. Move Down 40 units → (-20, -20)

Example 3
Input
5
Output
30 -20
Explanation

Continuing from previous: 5. Move Right 50 units → (30, -20)

Solution

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    
    int x = 0, y = 0;
    int distance = 10;
    int direction = 0; // 0: Right, 1: Up, 2: Left, 3: Down
    
    for (int i = 0; i < n; i++) {
        switch (direction) {
            case 0: // Right
                x += distance;
                break;
            case 1: // Up
                y += distance;
                break;
            case 2: // Left
                x -= distance;
                break;
            case 3: // Down
                y -= distance;
                break;
        }
        
        // Move to next direction (cyclic)
        direction = (direction + 1) % 4;
        // Increase distance by 10
        distance += 10;
    }
    
    printf("%d %d\n", x, y);
    return 0;
}
Time:O(n) - The loop runs exactly n times
Space:O(1) - Only uses a constant amount of extra space
Approach:

The C solution uses a straightforward approach with a switch statement to handle each direction. The direction variable cycles through 0-3 representing Right, Up, Left, and Down respectively. For each move, we update the coordinates based on the current direction, then rotate to the next direction and increase the step size by 10.

Key aspects:

  • Uses modulo arithmetic to cycle through directions
  • Updates coordinates directly using += and -= operators
  • Simple loop structure that's easy to understand and debug

Visual Explanation

Loading diagram...