Food Belt – TCS CodeVita

Restaurants are coming up with new technologies to serve food. The food is being served to customers using food belts. The conveyor belt is used to serve people, where food items are kept on the belt and are then moved ahead. The food belt moves in a manner as shown below.


The above figure shows example for one rotation of food belt. So, the task is to display the food belt after ‘R’ rotations and display the index of food item ‘Fx’ on the food belt. There are 9 food items on the food belt, as depicted in the image above. After 9 rotations every food item will be in its initial position. Thus, one rotation is defined as a food item moving to its next position.

Note: Top left indicates index [0, 0] and bottom right indicates index [m-1, n-1].

Suppose if the food belt is M x N, then the even numbered rows move from left to right and odd numbered rows move from right to left. Consider 0 to be even number.

Constraints:
1 <= M <= 50
1 <= N <= 50
R >= 0

Input:
First line contains two space separated integers M and N which denote the size of the food belt having M rows and N columns.
Next M lines contain N space separated strings which denote the food items.
Next line contains an integer ‘R’ denoting the number of rotations.
Next line contains the food item ‘Fx’ whose index in the food belt after ‘R’ rotations has to be found.
Here x denotes an integer.

Output:
Display the food belt in the first M lines after R rotations and then display the index of the food item ‘Fx’ in the (M+1)th line. The Examples section elaborates the format of printing the index. If no such food item ‘Fx’ is available, then display ‘Not Available’.

Refer the Examples section for better understanding.

Time Limit (secs):
1

Example1:
Input:
44
F1 F2 F3 F4
F5 F6 F7 F8
F9 F10 F11 F12
F13 F14 F15 F16
2
F2
Output
F14 F13 F1 F2
F7 F8 F4 F3
F6 F5 F9 F10
F15 F16 F12 F11
[0,3]
Explanation:
After two rotations of the food belt, the food items on food belt are as follows:
F14 F13 F1 F2
F7 F8 F4 F3
F6 F5 F9 F10
F15 F16 F12 F11
And the index of ‘F2’ on the food belt is [0,3].

Example2:
Input:
1 20
F19 F11 F7 F10 F12 F14 F16 F3 F20 F6 F8 F5 F2 F13 F9 F17 F18 F4 F15 F21
Output
F14 F16 F3 F20 F6 F8 F5 F2 F13 F9 F17 F18 F4 F15 F21 F19 F11 F7 F10 F12
[0, 19]
Explanation:
Initially, ‘F12’ is at [0,4]. Hence after fifteen rotations it will be at [0, 19].
The entire food belt after 15 rotations is depicted as below:
F14 F16 F3 F20 F6 F8 F5 F2 F13 F9 F17 F18 F4 F15 F21 F19 F11 F7 F10 F12

#include <iostream>
#include <vector>
using namespace std;

void transform(vector<string> &v, vector<vector<string>> belt, int n, int m) {
    for (int i = 0; i < m; i++) {
        if (i % 2 == 0) {
            for (int j = 0; j < n; j++) {
                v.push_back(belt[i][j]);  // Add food items from left to right in even rows.
            }
        } else {
            for (int j = n - 1; j >= 0; j--) {
                v.push_back(belt[i][j]);  // Add food items from right to left in odd rows.
            }
        }
    }
}

void rotate(vector<string> &vect, int r, int m, int n) {
    r %= m * n;

    // Rotate the food belt by reversing sections of the vector.
    int i = m * n - r, j = m * n - 1;
    while (i < j) {
        swap(vect[i++], vect[j--]);
    }

    i = 0, j = m * n - r - 1;
    while (i < j) {
        swap(vect[i++], vect[j--]);
    }

    i = 0, j = m * n - 1;
    while (i < j) {
        swap(vect[i++], vect[j--]);
    }
}

tuple<int, int> display(vector<string> vect, int n, int m, string ask) {
    tuple<int, int> loc(-1, -1);

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            int cord = i * n + ((i % 2 == 1) ? (n - 1 - j) : j);

            if (vect[cord] == ask) {
                get<0>(loc) = cord / n;
                get<1>(loc) = (i % 2 == 1) ? (n - 1 - cord % n) : (cord % n);
            }

            cout << vect[cord] << " ";
        }
        cout << "n";
    }

    return loc;
}

int main() {
    int m, n, r;
    cin >> m >> n;
    string ask;
    vector<vector<string>> matrix(m, vector<string>(n));

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> matrix[i][j];
        }
    }

    cin >> r >> ask;
    vector<string> v;
    transform(v, matrix, n, m);

    rotate(v, r, m, n);

    tuple<int, int> loc(-1, -1);
    loc = display(v, n, m, ask);

    if (get<0>(loc) == -1 && get<1>(loc) == -1) {
        cout << "Not Available";
    } else {
        cout << "[" << get<0>(loc) << ", " << get<1>(loc) << "]";
    }

    return 0;
}
import java.util.Scanner;

public class FoodBelt {
    public static void transform(String[] foodBelt, String[][] belt, int n, int m) {
        int index = 0;
        for (int i = 0; i < m; i++) {
            if (i % 2 == 0) {
                for (int j = 0; j < n; j++) {
                    foodBelt[index++] = belt[i][j];
                }
            } else {
                for (int j = n - 1; j >= 0; j--) {
                    foodBelt[index++] = belt[i][j];
                }
            }
        }
    }

    public static void rotate(String[] foodBelt, int r, int m, int n) {
        r %= (m * n);
        String temp;
        for (int i = 0, j = m * n - 1; i < j; i++, j--) {
            temp = foodBelt[i];
            foodBelt[i] = foodBelt[j];
            foodBelt[j] = temp;
        }
        for (int i = 0, j = m * n - r - 1; i < j; i++, j--) {
            temp = foodBelt[i];
            foodBelt[i] = foodBelt[j];
            foodBelt[j] = temp;
        }
        for (int i = 0, j = m * n - 1; i < j; i++, j--) {
            temp = foodBelt[i];
            foodBelt[i] = foodBelt[j];
            foodBelt[j] = temp;
        }
    }

    public static int[] display(String[] foodBelt, int n, int m, String ask) {
        int[] loc = new int[] { -1, -1 };
        int index = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int cord = i * n + ((i % 2 == 1) ? (n - 1 - j) : j);
                if (foodBelt[cord].equals(ask)) {
                    loc[0] = cord / n;
                    loc[1] = (i % 2 == 1) ? (n - 1 - cord % n) : (cord % n);
                }
                System.out.print(foodBelt[cord] + " ");
            }
            System.out.println();
        }
        return loc;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        String[][] belt = new String[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                belt[i][j] = scanner.next();
            }
        }

        int r = scanner.nextInt();
        String ask = scanner.next();

        String[] foodBelt = new String[m * n];
        transform(foodBelt, belt, n, m);
        rotate(foodBelt, r, m, n);
        int[] loc = display(foodBelt, n, m, ask);

        if (loc[0] == -1 && loc[1] == -1) {
            System.out.println("Not Available");
        } else {
            System.out.println("[" + loc[0] + ", " + loc[1] + "]");
        }
    }
}
def transform(food_belt, belt, n, m):
    index = 0
    for i in range(m):
        if i % 2 == 0:
            for j in range(n):
                food_belt[index] = belt[i][j]
                index += 1
        else:
            for j in range(n - 1, -1, -1):
                food_belt[index] = belt[i][j]
                index += 1

def rotate(food_belt, r, m, n):
    r %= m * n
    food_belt[:r], food_belt[r:] = food_belt[-r:], food_belt[:-r]

def display(food_belt, n, m, ask):
    loc = [-1, -1]
    index = 0
    for i in range(m):
        for j in range(n):
            cord = i * n + (n - 1 - j if i % 2 == 1 else j)
            if food_belt[cord] == ask:
                loc[0] = cord // n
                loc[1] = n - 1 - cord % n if i % 2 == 1 else cord % n
            print(food_belt[cord], end=" ")
        print()
    return loc

def main():
    m, n = map(int, input().split())
    belt = []
    for i in range(m):
        row = input().split()
        belt.append(row)

    r = int(input())
    ask = input()

    food_belt = [""] * (m * n)
    transform(food_belt, belt, n, m)
    rotate(food_belt, r, m, n)
    loc = display(food_belt, n, m, ask)

    if loc[0] == -1 and loc[1] == -1:
        print("Not Available")
    else:
        print(f"[{loc
#include <stdio.h>

void transform(char food_belt[], char belt[][50], int n, int m) {
    int index = 0;
    for (int i = 0; i < m; i++) {
        if (i % 2 == 0) {
            for (int j = 0; j < n; j++) {
                food_belt[index++] = belt[i][j];
            }
        } else {
            for (int j = n - 1; j >= 0; j--) {
                food_belt[index++] = belt[i][j];
            }
        }
    }
}

void rotate(char food_belt[], int r, int m, int n) {
    r %= (m * n);
    char temp;
    for (int i = 0, j = m * n - 1; i < j; i++, j--) {
        temp = food_belt[i];
        food_belt[i] = food_belt[j];
        food_belt[j] = temp;
    }
    for (int i = 0, j = m * n - r - 1; i < j; i++, j--) {
        temp = food_belt[i];
        food_belt[i] = food_belt[j];
        food_belt[j] = temp;
    }
    for (int i = 0, j = m * n - 1; i < j; i++, j--) {
        temp = food_belt[i];
        food_belt[i] = food_belt[j];
        food_belt[j] = temp;
    }
}

void display(char food_belt[], int n, int m, char ask) {
    int index = 0;
    int x = -1, y = -1;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (food_belt[index] == ask) {
                x = i;
                y = (i % 2 == 1) ? (n - 1 - j) : j;
            }
            printf("%c ", food_belt[index++]);
        }
        printf("n");
    }
    if (x == -1 && y == -1) {
        printf("Not Available");
    } else {
        printf("[%d,%d]", x, y);
    }
}

int main() {
    int m, n, r;
    scanf("%d %d", &m, &n);
    char food_belt[m * n];
    char belt[m][50];

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            scanf(" %c", &belt[i][j]);
        }
    }

    scanf("%d ", &r);
    char ask;
    scanf("%c", &ask);

    transform(food_belt, belt, n, m);
    rotate(food_belt, r, m, n);
    display(food_belt, n, m, ask);

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.