Representation Change in Transform and Conquer Technique
Representation Change is one of the variants of the Transfer and Conquer technique where the given problem is transformed into another domain that is more familiar or simpler to execute. In the case of representation change, the instance of a given problem is transformed into another representation without affecting the original instance.
Characteristics:
Given below are some basic characteristics of the Representation Change technique:
- Only the representation of the instance is changed but the original instance should be retained.
- The extraction of the result from the new representation should be more efficient than the original one.
Example:
Let us understand the representation change in a better way with the help of an example:
Consider the problem Find if there is any duplicate element in the array
Approach 1: To solve this problem one can compare each element with all other elements of the array and find if any duplicate is present in the array or not.
It can be written as follows:
- Iterate a loop from the start to the end
- Compare each element with all the other elements.
- If there is a match then a duplicate exists.
- Otherwise, there is no duplicate found for the current element.
- Ultimately, after all the elements are traversed and no duplicate found in any iteration, then the array does not contain a duplicate value.
Algorithm:
Algorithm find_duplicate(A[1, 2, . . . N]):
for i = 0 to N-1:
temp = A[i]
for j = i+1 to N:
temp1 = A[j]
if temp == temp1:
duplicate found
end if
end for
end for
Below is the implementation of the above approach:
#include <iostream>
#include <vector>
using namespace std;
// Function to find duplicate in array
bool find_duplicate(vector<int>& A) {
int N = A.size();
// Outer loop to pick elements one by one
for (int i = 0; i < N-1; ++i) {
int temp = A[i];
// Inner loop to compare the picked element with rest of the elements
for (int j = i+1; j < N; ++j) {
int temp1 = A[j];
// If duplicate is found
if (temp == temp1) {
return true; // Return true
}
}
}
// If no duplicate is found, return false
return false;
}
int main() {
// Initialize array
vector<int> A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Call function to check for duplicate
if (find_duplicate(A)) {
// If duplicate is found, print message
cout << "Duplicate found" << endl;
} else {
// If no duplicate is found, print message
cout << "No duplicate found" << endl;
}
return 0;
}
import java.util.ArrayList;
public class Main {
// Function to find duplicate in array
static boolean findDuplicate(ArrayList<Integer> A) {
int N = A.size();
// Outer loop to pick elements one by one
for (int i = 0; i < N - 1; ++i) {
int temp = A.get(i);
// Inner loop to compare the picked element with rest of the elements
for (int j = i + 1; j < N; ++j) {
int temp1 = A.get(j);
// If duplicate is found
if (temp == temp1) {
return true; // Return true
}
}
}
// If no duplicate is found, return false
return false;
}
public static void main(String[] args) {
// Initialize array
ArrayList<Integer> A = new ArrayList<>();
A.add(1);
A.add(2);
A.add(3);
A.add(4);
A.add(5);
A.add(6);
A.add(7);
A.add(8);
A.add(9);
A.add(10);
// Call function to check for duplicate
if (findDuplicate(A)) {
// If duplicate is found, print message
System.out.println("Duplicate found");
} else {
// If no duplicate is found, print message
System.out.println("No duplicate found");
}
}
}
# Function to find duplicate in array
def find_duplicate(A):
N = len(A)
# Outer loop to pick elements one by one
for i in range(N - 1):
temp = A[i]
# Inner loop to compare the picked element with rest of the elements
for j in range(i + 1, N):
temp1 = A[j]
# If duplicate is found
if temp == temp1:
return True # Return True
# If no duplicate is found, return False
return False
# Main function
def main():
# Initialize array
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Call function to check for duplicate
if find_duplicate(A):
# If duplicate is found, print message
print("Duplicate found")
else:
# If no duplicate is found, print message
print("No duplicate found")
# Call the main function
if __name__ == "__main__":
main()
// Function to find duplicate in array
function findDuplicate(A) {
let N = A.length;
// Outer loop to pick elements one by one
for (let i = 0; i < N - 1; i++) {
let temp = A[i];
// Inner loop to compare the picked element with rest of the elements
for (let j = i + 1; j < N; j++) {
let temp1 = A[j];
// If duplicate is found
if (temp === temp1) {
return true; // Return True
}
}
}
// If no duplicate is found, return False
return false;
}
// Main function
function main() {
// Initialize array
let A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Call function to check for duplicate
if (findDuplicate(A)) {
// If duplicate is found, print message
console.log("Duplicate found");
} else {
// If no duplicate is found, print message
console.log("No duplicate found");
}
}
// Call the main function
main();
Output
No duplicate found
Time Complexity: O(N2)
Auxiliary Space: O(1)
Approach 2 (Representation Change): The above problem is complex in the sense of comparison. It requires a lot of comparisons. This complexity can be reduced as shown below:
- Change the above array to a Red-Black Tree that will hold only unique elements (the functionality is implemented in a Set).
- If the size of the tree and the array are the same then there is no duplicate.
This is the representation change technique where the array representation is changed and the problem is solved efficiently.
The approach is as follows:
- Build an empty set (which implements the Red-Black tree).
- Insert the array elements into the set.
- If the set size and the array size are the same then there is no duplicate. Otherwise, there are duplicate elements.
Algorithm:
Algorithm find_duplicate(A[1, 2, . . . N]):
set st[]
for i = 0 to N:
insert A[i] in st[]
end for
if sizeof(st) == N:
no duplicate
else:
duplicate exists
end if
Below is the implementation of above approach:
#include <iostream>
#include <set>
#include <vector>
bool find_duplicate(const std::vector<int>& A)
{
std::set<int> st;
for (int i = 0; i < A.size(); ++i) {
st.insert(A[i]);
}
if (st.size() == A.size()) {
return false; // no duplicate
}
else {
return true; // duplicate exists
}
}
int main()
{
std::vector<int> A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
if (find_duplicate(A)) {
std::cout << "Duplicate exists in the array.\n";
}
else {
std::cout << "No duplicates in the array.\n";
}
return 0;
}
import java.util.HashSet;
import java.util.Set;
public class Main {
static boolean findDuplicate(int[] A)
{
Set<Integer> set = new HashSet<>();
for (int i = 0; i < A.length; ++i) {
if (!set.add(A[i])) {
return true; // Duplicate exists
}
}
return false; // No duplicate
}
public static void main(String[] args)
{
int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
if (findDuplicate(A)) {
System.out.println(
"Duplicate exists in the array.");
}
else {
System.out.println(
"No duplicates in the array.");
}
}
}
def findDuplicate(A):
seen = set()
for num in A:
if num in seen:
return True # Duplicate exists
seen.add(num)
return False # No duplicate
# Example usage
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if findDuplicate(A):
print("Duplicate exists in the array.")
else:
print("No duplicates in the array.")
function findDuplicate(A) {
let set = new Set();
for (let i = 0; i < A.length; ++i) {
if (!set.add(A[i])) {
return true; // Duplicate exists
}
}
return false; // No duplicate
}
function main() {
let A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
if (findDuplicate(A)) {
console.log("Duplicate exists in the array.");
} else {
console.log("No duplicates in the array.");
}
}
main();
Output
No duplicates in the array.
Time Complexity: O(N * logN) which is required to insert all array elements into the set.
Auxiliary Space: O(N)
Advantages: The advantages of the representation change method are mentioned below
- By changing the representation, the time complexity of the problem gets reduced to a large extent.
- The instance of the given problem is retained after changing its representation.
Disadvantages: There are also a few disadvantages of the technique like:
- Constructing a new instance of the given problem is difficult and costly.
Contact Us