TCS Coding Question 1 | Our hoary culture had several great persons ….

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 Jataak is recokened 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 following scheme.

TCS NQT Coding

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.

Test Cases

Case 1

  • Input : 3
  • Expected Output :-20 20

Case 2

  • Input: 4
  • Expected Output: -20 -20

Case 3

  • Input : 5
  • Expected Output : 30 -20

Case 4

  • Input : 7
  • Expected Output : 90 -20
Our hoary culture had several great persons since time (1)

31 comments on “TCS Coding Question 1 | Our hoary culture had several great persons ….”


  • fami

    import java.util.*;

    public class Main {
    public static void main(String[] args) {
    Scanner obj = new Scanner(System.in);
    int num = obj.nextInt();
    int j = 10;
    int x = 0, y = 0;
    int a = 0, b = 0;
    for (int i = 1; i <= num; i++) {
    if (i % 2 != 0 && a + 2 == i) {
    x = x – j;
    a++;
    } else if (i % 2 != 0) {
    x = x + j;
    a++;
    }

    if (i % 2 == 0 && b + 3 == i) {
    y = y – j;
    b++;
    } else if (i % 2 == 0) {
    y = y + j;
    b++;
    }
    j += 10;
    }
    System.out.println(x + "," + y);
    }
    }


  • Syed

    public static void main(String args[]{
    System.out.println(“Enter input”);
    (This is missing in java program)


  • anmolshrivastav.08

    C++ implementation
    #include
    using namespace std;

    int main(){
    int t;
    cin>>t;
    int n = 5, dis = 10;
    int x = 0 , y = 0, j = 0;
    for(int i=0; i<t; i++, j++){

    if(j == 5){
    j = 0;
    }

    if(j == 0){
    x += dis;
    dis += 10;
    }else if(j == 1){
    y += dis;
    dis += 10;
    }else if(j == 2){
    x -= dis;
    dis += 10;
    }else if(j == 3){
    y -= dis;
    dis += 10;
    }else{
    x += dis;
    dis += 10;
    }

    }
    cout<<x<<" "<<y;
    return 0;
    }


  • pankaj

    /******************************************************************************

    Online C++ Compiler.
    Code, Compile, Run and Debug C++ program online.
    Write your code in this editor and press “Run” button to compile and execute it.

    *******************************************************************************/

    #include

    using namespace std;

    int main(){
    int n,x,y;
    cin>>n;
    x=0,y=0;
    for(int i=1; i<=n; i++){
    if(i%4==1){
    x=x+(10*i);
    }
    if(i%4==2){
    y=y+(10*i);
    }
    if(i%4==3){
    x=x-(10*i);
    }
    if(i%4==0){
    y=y-(10*i);
    }
    }
    cout<<x<<" "<<y<<" ";
    }


  • BISWADEEP

    #include
    using namespace std;

    int main()
    {
    int d, n;
    int x, y;

    cin >> n;

    for( x = y = 0, d = 10 ; d <= n*10 ; d += 10 )
    {
    switch( d%40 )
    {
    case 10 : x += d; break;
    case 20 : y += d; break;
    case 30 : x -= d; break;
    case 0 : y -= d; break;
    }
    }

    cout << x << ' ' << y;

    return 0;
    }


  • sri datta

    I have solved in this way 😉
    #include
    using namespace std;
    int right(int a,int b,int c)
    {
    a = a+c;
    b = b;
    return a;
    }
    int upward(int a,int b,int c)
    {
    b = b+c;
    return b;
    }
    int left(int a,int b,int c)
    {
    a = a-c;
    b = b;
    return a;
    }
    int downward(int a,int b,int c)
    {
    b = b-c;
    a = a;
    return b;
    }
    int main()
    {
    int T;
    cout<<"enter the number of test cases want to print"<>T;
    int x {0};
    int y {0};
    int i = 1;
    int p {0};
    while(T)
    {
    if(T>=i)
    {
    p = p+10;
    x = right(x,y,p);

    }
    else
    {
    T = false;
    }
    ++i;
    if(T>=i)
    {
    p = p+10;
    y = upward(x,y,p);

    }
    else
    {
    T = false;
    }
    ++i;
    if(T>=i)
    {
    p = p+10;
    x = left(x,y,p);

    }
    else
    {
    T = false;
    }
    ++i;
    if(T>=i)
    {
    p = p+10;
    y = downward(x,y,p);

    }
    else
    {
    T = false;
    }
    ++i;
    if(T>=i)
    {
    p = p+10;
    x = right(x,y,p);

    }
    else
    {
    T = false;
    }
    ++i;

    }
    cout<<x<<" "<<y<<endl;
    }


  • 50 Ritesh

    THIS IS BEST CODE FOR THE PROBLEM

    #include
    using namespace std;

    int main() {
    int n;
    cin>>n;
    int x=0,y=0,k=10;
    for(int i=1;i<=n;i++){
    if(i%5==1)
    x=x+k;
    else if(i%5==2)
    y=y+k;
    else if(i%5==3)
    x=x-k;
    else if(i%5==4)
    y=y-k;
    else
    x=x+k;
    k+=10;
    }
    cout<<x<<" "<<y;
    }


  • ankit

    x,y=0,0

    n = int(input())

    if n==1:
    x += 10

    elif n==2:
    x += 10
    y +=20

    elif n==3:
    x += 10
    y +=20
    x -= 30

    elif n==4:
    x += 10
    y +=20
    x -= 30
    y -= 40

    elif n==5:
    x += 10
    y +=20
    x -= 30
    y -= 40
    x += 50

    elif n==6:
    x += 10
    y +=20
    x -= 30
    y -= 40
    x += 50
    x += 60

    elif n==7:
    x += 10
    y +=20
    x -= 30
    y -= 40
    x += 50
    x += 60
    y += 70

    print(f'{x} {y}’)


  • Daipayan

    Much Simpler way
    /**
    * prepCodin1
    */
    import java.util.Scanner;

    public class prepCodin1 {
    public static void getDistance(int a) {
    int x = 0;
    int y = 0;
    for (int i = 0; i < a; i++) {
    if ((i) % 5 == 0) {
    x = x + (10 * (i + 1));
    } else if ((i) % 5 == 1) {
    y = y + (10 * (i + 1));
    } else if ((i) % 5 == 2) {
    x = x – (10 * (i + 1));
    } else if ((i) % 5 == 3) {
    y = y – (10 * (i + 1));
    } else {
    x = x + (10 * (i + 1));
    }

    }
    System.out.println(x + " , " + y);
    }

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int testCase = sc.nextInt();
    getDistance(testCase);
    }
    }


  • Debparna

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int x=0;int y=0;int d=10;
    for(int i=1;i<=n;i++){
    if(i%4==1){
    x=x+d;
    d=d+10;
    }else if(i%4==2){
    y=y+d;
    d=d+10;
    }else if(i%4==3){
    x=x-d;
    d=d+10;
    }else if(i%4==0){
    y=y-d;
    d=d+10;
    }
    }
    System.out.println(x+" "+y);
    }
    }


  • Rahul

    n = int(input())
    if n % 4 == 0:
    x = -(n * 5)
    y = x
    elif n % 4 == 1:
    x = (n + 1) * 5
    y = -x + 10
    elif n % 4 == 2:
    x = n * 5
    y = x + 10
    elif n % 4 == 3:
    x = -(n + 1) * 5
    y = -x
    print((x, y))