- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two arrays A and B. The size of A is the number of rows and A[i] is the number of boxes in the ith row. And B is the array of balls where B[i] denotes a number on the ball. Given that ball i (value B[i]) will be placed in a box whose position from starting is B[i]. We have to find the row and column of the boxes corresponding to each B[i].

So, if the input is like A = [3, 4, 5, 6], B = [1, 3, 5, 2], then the output will be [(1, 1), (1, 3), (2, 2), (1, 2)] as B[0] = 1, then the box position will be 1st row, 1st column B[1] = 3, then the box position will be 1st row, 3rd column, B[2] = 5, then the box position will be 2nd row, 2nd column, B[3] = 2, then the box position will be 1st row, 2nd column

To solve this, we will follow these steps −

len_a := size of A

len_b := size of B

for i in range 1 to len_a, do

A[i] := A[i] + A[i - 1]

for i in range 0 to len_b, do

row := an index where we can insert B[i] to maintain A sorted

if row >= 1, then

box_num := B[i] - A[row - 1]

otherwise,

box_num := B[i]

display a pair (row + 1, box_num)

Let us see the following implementation to get better understanding −

import bisect def get_position(A, B): len_a = len(A) len_b = len(B) for i in range(1, len_a): A[i] += A[i - 1] for i in range(len_b): row = bisect.bisect_left(A, B[i]) if row >= 1: box_num = B[i] - A[row - 1] else: box_num = B[i] print ((row + 1, box_num)) A = [3, 4, 5, 6] B = [1, 3, 5, 2] get_position(A, B)

[3, 4, 5, 6], [1, 3, 5, 2]

(1, 1) (1, 3) (2, 2) (1, 2)

- Related Questions & Answers
- Program to find where the ball lands in a grid box in Python
- Catching the ball game using Python
- Find the lexicographically smallest string which satisfies the given condition in Python
- Find the time which is palindromic and comes after the given time in Python
- Find element position in given monotonic sequence in Python
- Find position of the given number among the numbers made of 4 and 7 in C++
- How to customize the position of an alert box using JavaScript?
- Program to find next board position after sliding the given direction once in Python
- Who has delivered the fastest ball in the history of cricket?
- Find frequency of given character at every position in list of lists in Python
- Program to reverse the position of each word of a given string in Python
- Automatically position text box in Matplotlib
- What is the debate over the use of pink ball in cricket?
- Find position of the only set bit in C++
- Program to find the number of possible position in a line in Python

Advertisements