
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to restore the array from adjacent pairs in Python
Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] represents that the elements ui and vi are adjacent in an array called nums, in nums there are n unique elements. We have to find the array nums. If there are multiple solutions, return any of them.
So, if the input is like adPair = [[3,2],[4,5],[4,3]], then the output will be [2,3,4,5]
To solve this, we will follow these steps −
- my_map := an empty map to store list for different keys
- for each pair (a, b) in adPair, do
- insert b at the end of my_map[a]
- insert a at the end of my_map[b]
- for each key a and value list l in my_map, do
- if size of l is same as 1, then
- nums := a list with two elements (a, l[0])
- come out from loop
- if size of l is same as 1, then
- for i in range 1 to size of adPair - 1, do
- a, b := my_map[last element of nums]
- if a is same as second last element of nums, then
- insert b at the end of nums
- otherwise,
- insert a at the end of nums
- return nums
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict def solve(adPair): my_map = defaultdict(list) for a, b in adPair: my_map[a].append(b) my_map[b].append(a) for a, l in my_map.items(): if len(l) == 1: nums = [a, l[0]] break for i in range(1, len(adPair)): a, b = my_map[nums[-1]] if a == nums[-2]: nums.append(b) else: nums.append(a) return nums adPair = [[3,2],[4,5],[4,3]] print(solve(adPair))
Input
[[3,2],[4,5],[4,3]]
Output
[2, 3, 4, 5]
- Related Articles
- Restore The Array in C++
- Program to count nice pairs in an array in Python
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Program to find array of doubled pairs using Python
- Program to find array by swapping consecutive index pairs in Python
- Counting adjacent pairs of words in JavaScript
- Program to count index pairs for which array elements are same in Python
- In the figure, write all pairs of adjacent angles and all the linear pairs."\n
- Program to make pairwise adjacent sums small in Python
- Removing letters to make adjacent pairs different using JavaScript
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Python program to count the pairs of reverse strings
- Python program to count Bidirectional Tuple Pairs
- Program to check if array pairs are divisible by k or not using Python
- Program to Find K-Largest Sum Pairs in Python

Advertisements