Open In App

Representation Change in Transform and Conquer Technique

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

C++
#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;
}
Java
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");
        }
    }
}
Python3
# 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()

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

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. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads