Algorithm

Problem Name: Mathematics - Strange Grid Again

In this HackerRank in Mathematics - Strange Grid Again solutions,

A strange grid has been recovered from an old book. It has 5 columns and infinite number of rows. The bottom row is considered as the first row. First few rows of the grid are like this:

``````..............

..............

20 22 24 26 28

11 13 15 17 19

10 12 14 16 18

1  3  5  7  9

0  2  4  6  8
``````

The grid grows upwards forever!

Your task is to find the integer in cth column in rth row of the grid.

Input Format

There will be two integers r and c separated by a single space.

Constraints

• 1 <= r < 2 * 109
• 1 <= c <= 5

Rows are indexed from bottom to top and columns are indexed from left to right.

Output Format

Output the answer in a single line.

Sample Input

``````6 3
``````

Sample Output

``````25
``````

Explanation

The number in the 6th row and 3rd column is 25.

Code Examples

#1 Code Example with C Programming

```Code - C Programming```

``````
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>>
#include <string.h>

char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);

int parse_int(char*);

/*
* Complete the 'strangeGrid' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER r
*  2. INTEGER c
*/

long int strangeGrid(int r, int c) {
long int ans,sum,rem;
if(r%2==0)
{
rem=r/2;
sum=10*(rem-1)+1;
ans=sum+(2*(c-1));
}
else
{
rem=r/2;
sum=10*(rem);
ans=sum+(2*(c-1));
}
return ans;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

int r = parse_int(*(first_multiple_input + 0));

int c = parse_int(*(first_multiple_input + 1));

int result = strangeGrid(r, c);

fprintf(fptr, "%d\n", result);

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!data) {
data = '\0';

break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}

return data;
}

char* ltrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

while (*str != '\0' && isspace(*str)) {
str++;
}

return str;
}

char* rtrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

char* end = str + strlen(str> - 1;

while (end >= str && isspace(*end)) {
end--;
}

*(end + 1) = '\0';

return str;
}

char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");

int spaces = 0;

while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);

if (!splits) {
return splits;
}

splits[spaces - 1] = token;

token = strtok(NULL, " ");
}

return splits;
}

int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);

if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}

return value;
}

``````
Copy The Code &

#2 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include<bits/stdc++.h>
using namespace std;
int main(){
long long int r,c;
cin>>r>>c;

if(r%2!=0)
cout<< (5*(r-1))+((c-1)*2);
else
cout << (5*(r-2))+1+((c-1)*2);

return 0;
}
``````
Copy The Code &

#3 Code Example with Java Programming

```Code - Java Programming```

``````
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt(), column = sc.nextInt();

int[] odd_row = {0, 2, 4, 6, 8};
int[] even_row = {1, 3, 5, 7, 9};

long result;
if (row % 2 == 0)
result = 10L * (row / 2 - 1) + even_row[column - 1];
else
result = 10L * (row / 2) + odd_row[column - 1];

System.out.println(result);
}
}
``````
Copy The Code &

#4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

return inputString[currentLine++];
}

/*
* Complete the 'strangeGrid' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER r
*  2. INTEGER c
*/

function strangeGrid(r, c) {
return Math.floor(0.5*r-.5)*10 + (r-1)%2 + 2*c - 2
}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

const firstMultipleInput = readLine().replace(/\s+\$/g, '').split(' ');

const r = parseInt(firstMultipleInput[0], 10);

const c = parseInt(firstMultipleInput[1], 10);

const result = strangeGrid(r, c);

ws.write(result + '\n');

ws.end();
}
``````
Copy The Code &

#5 Code Example with Python Programming

```Code - Python Programming```

``````
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'strangeGrid' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER r
#  2. INTEGER c
#

def strangeGrid(r, c):
if r % 2 == 0:
return int(5*r+2*c-11)
else:
return int(5*r+2*c-7)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

first_multiple_input = input().rstrip().split()

r = int(first_multiple_input[0])

c = int(first_multiple_input[1])

result = strangeGrid(r, c)

fptr.write(str(result) + '\n')

fptr.close()

``````
Copy The Code &