Find original sequence from Array containing the sequence merged many times in order
Given a number N and an array arr[] that consist of merging N length sequence of distinct integers any number of times maintaining the relative order of elements in the initial sequence. The task is to find the initial sequence of length N maintaining the right order.
Examples:
Input: N = 4, arr[] = {1, 13, 1, 24, 13, 24, 2, 2}
Output: 1 13 24 2
Explanation:
Here the given sequence is obtained by merging 1 13 24 2 maintaining the relative order of elements. Therefore, the answer is 1 13 24 2.
Input: N = 3, arr[] = {3, 2, 3, 1, 2, 3, 2, 1, 1}
Output: 3 2 1
Explanation:
Here the given sequence is obtained by merging 3 2 1 maintaining the relative order of elements. Therefore, the answer is 3 2 1.
Approach: The idea is to observe that the element occurring first in the given sequence is the first element of the resultant restored sequence. Take that element in our restored sequence and don’t include duplicate from the given sequence. Perform the same for the rest of the elements until we reach the end. The idea can be implemented by both Map and Set in C++.
Using Map
- Traverse through the given sequence from left to right.
- The element coming for the first time in the sequence is taken into account and marked using a map.
- Elements that are marked while traversing are ignored.
- Step 2 and Step 3 is continued until the end of the given sequence is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > restore( int arr[], int N)
{
vector< int > result;
map< int , int > mp;
for ( int i = 0; i < N; i++) {
if (mp[arr[i]] == 0) {
result.push_back(arr[i]);
mp[arr[i]]++;
}
}
return result;
}
void print_result(vector< int > result)
{
for ( int i = 0; i < result.size(); i++)
cout << result[i] << " " ;
}
int main()
{
int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
print_result(restore(arr, N));
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> restore( int arr[], int N)
{
Vector<Integer> result = new Vector<>();
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
if (mp.containsKey(arr[i]) &&
mp.get(arr[i]) == 0 )
{
result.add(arr[i]);
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
else
mp.put(arr[i], 0 );
}
return result;
}
static void print_result(Vector<Integer> result)
{
for ( int i = 0 ; i < result.size(); i++)
System.out.print(result.get(i) + " " );
}
public static void main(String[] args)
{
int arr[] = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 };
int N = arr.length;
print_result(restore(arr, N));
}
}
|
Python3
def restore(arr, N):
result = []
mp = {}
for i in range (N):
if not arr[i] in mp:
result.append(arr[i])
mp[arr[i]] = 1
return result
def print_result(result):
for i in range ( len (result)):
print (result[i], end = " " )
def main():
arr = [ 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 ]
N = len (arr)
print_result(restore(arr, N))
main()
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > restore( int []arr, int N)
{
List< int > result = new List< int >();
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]) &&
mp[arr[i]] == 0)
{
result.Add(arr[i]);
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
else
mp.Add(arr[i], 0);
}
return result;
}
static void print_result(List< int > result)
{
for ( int i = 0; i < result.Count; i++)
Console.Write(result[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 1, 13, 1, 24, 13, 24, 2, 2 };
int N = arr.Length;
print_result(restore(arr, N));
}
}
|
Javascript
<script>
function restore(arr, N)
{
var result = [];
var mp = new Map();
for ( var i = 0; i < N; i++) {
if (!mp.has(arr[i])) {
result.push(arr[i]);
mp.set(arr[i], mp.get(arr[i])+1);
}
}
return result;
}
function print_result(result)
{
for ( var i = 0; i < result.length; i++)
{
document.write( result[i] + " " );
}
}
var arr = [1, 13, 1, 24, 13, 24, 2, 2];
var N = arr.length;
print_result(restore(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Using Set
- Traverse through the given sequence from left to right.
- A counter is taken and initialized as 1.
- Insert the elements into the set one by one. If at some point that the size of the set and the counter is the same, the element is taken into account and the counter is increased by 1.
- Step 3 and Step 4 is continued until the end of the given sequence is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > restore( int arr[], int N)
{
vector< int > result;
int count1 = 1;
set< int > s;
for ( int i = 0; i < N; i++) {
s.insert(arr[i]);
if (s.size() == count1) {
result.push_back(arr[i]);
count1++;
}
}
return result;
}
void print_result(vector< int > result)
{
for ( int i = 0; i < result.size(); i++)
cout << result[i] << " " ;
}
int main()
{
int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
print_result(restore(arr, N));
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> restore( int arr[], int N)
{
Vector<Integer> result = new Vector<Integer>();
int count1 = 1 ;
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++)
{
s.add(arr[i]);
if (s.size() == count1)
{
result.add(arr[i]);
count1++;
}
}
return result;
}
static void print_result(Vector<Integer> result)
{
for ( int i = 0 ; i < result.size(); i++)
System.out.print(result.get(i) + " " );
}
public static void main(String[] args)
{
int arr[] = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 };
int N = arr.length;
print_result(restore(arr, N));
}
}
|
Python3
def restore(arr, N):
result = []
count1 = 1
s = set ([])
for i in range (N):
s.add(arr[i])
if ( len (s) = = count1):
result.append(arr[i])
count1 + = 1
return result
def print_result(result):
for i in range ( len (result)):
print (result[i],
end = " " )
if __name__ = = "__main__" :
arr = [ 1 , 13 , 1 , 24 ,
13 , 24 , 2 , 2 ]
N = len (arr)
print_result(restore(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > restore( int []arr, int N)
{
List< int > result = new List< int >();
int count1 = 1;
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
s.Add(arr[i]);
if (s.Count == count1)
{
result.Add(arr[i]);
count1++;
}
}
return result;
}
static void print_result(List< int > result)
{
for ( int i = 0; i < result.Count; i++)
Console.Write(result[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 1, 13, 1, 24, 13, 24, 2, 2 };
int N = arr.Length;
print_result(restore(arr, N));
}
}
|
Javascript
<script>
function restore(arr, N)
{
let result = [];
let count1 = 1;
let s = new Set();
for (let i = 0; i < N; i++)
{
s.add(arr[i]);
if (s.size == count1)
{
result.push(arr[i]);
count1++;
}
}
return result;
}
function print_result(result)
{
for (let i = 0; i < result.length; i++)
document.write(result[i] + " " );
}
let arr = [ 1, 13, 1, 24, 13, 24, 2, 2 ];
let N = arr.length;
print_result(restore(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...