Open In App

Introduction to Invariants and Monovariants

Invariants and Monovariants are the two properties of mathematics and computer science that describe objects and algorithms. Invariants and mono variants are important mathematical concepts used to solve problems in mathematics, including algebra, geometry, etc.

Invariants:

Invariant is a property where the value remains unchanged under certain operations and transformations. In other words, we can say that in invariant property a quantity remains constant or fixed throughout a process. In mathematical terms, we define invariant as a function whose value does not change under some operations and transformations.



Example: let’s have an example for a better understanding

Given an array and we have to find the maximum element in that array.



Below is the implementation  based on the above steps:




// C++ code of the above approach
#include <iostream>
using namespace std;
 
// Declaring the solution function
// that return integer value.
int solution(int arr[], int n)
{
 
    // Initializing the answer variable
    // with first element of the array.
    int answer = arr[0];
 
    // Iterating through the whole array
    // and updating the answer variable.
    for (int i = 0; i < 6; i++) {
 
        // Checking that the answer
        // is smaller or not
        if (answer < arr[i]) {
 
            // Updating the value of
            // answer is smaller
            answer = arr[i];
        }
    }
 
    // Returning the answer that is
    // maximum element of whole array.
    return answer;
}
 
// Driver code;
int main()
{
 
    // Initializing the array
    int arr[6] = { 4, -2, 8, 6, 24, -28 };
 
    // Function call
    cout << solution(arr, 6);
    return 0;
}




// Java code of the above approach
import java.util.*;
 
class Main {
    // Declaring the solution function
    // that return integer value.
    static int solution(int[] arr, int n)
    {
 
        // Initializing the answer variable
        // with first element of the array.
        int answer = arr[0];
 
        // Iterating through the whole array
        // and updating the answer variable.
        for (int i = 0; i < 6; i++) {
 
            // Checking that the answer
            // is smaller or not
            if (answer < arr[i]) {
 
                // Updating the value of
                // answer is smaller
                answer = arr[i];
            }
        }
 
        // Returning the answer that is
        // maximum element of whole array.
        return answer;
    }
 
    // Driver code;
    public static void main(String[] args)
    {
        // Initializing the array
        int[] arr = { 4, -2, 8, 6, 24, -28 };
 
        // Function call
        System.out.println(solution(arr, 6));
    }
}




# Python code of the above approach
 
# Declaring the solution function
# that return integer value.
 
 
def solution(arr, n):
 
    # Initializing the answer variable
    # with first element of the array.
    answer = arr[0]
 
    # Iterating through the whole array
    # and updating the answer variable.
    for i in range(6):
 
        # Checking that the answer
        # is smaller or not
        if answer < arr[i]:
 
            # Updating the value of
            # answer is smaller
            answer = arr[i]
 
    # Returning the answer that is
    # maximum element of whole array.
    return answer
 
 
# Driver code;
arr = [4, -2, 8, 6, 24, -28]
print(solution(arr, 6))




// C# code of the above approach
using System;
 
public class GFG {
 
    // Declaring the solution function
    // that returns an integer value.
    public static int solution(int[] arr, int n)
    {
 
        // Initializing the answer variable
        // with first element of the array.
        int answer = arr[0];
 
        // Iterating through the whole array
        // and updating the answer variable.
        for (int i = 0; i < n; i++) {
 
            // Checking that the answer
            // is smaller or not
            if (answer < arr[i]) {
 
                // Updating the value of
                // answer is smaller
                answer = arr[i];
            }
        }
 
        // Returning the answer that is
        // maximum element of whole array.
        return answer;
    }
 
    // Driver code;
    public static void Main()
    {
 
        // Initializing the array
        int[] arr = { 4, -2, 8, 6, 24, -28 };
 
        // Function call
        Console.WriteLine(solution(arr, 6));
    }
}




// Declaring the solution function
// that return integer value.
function solution(arr, n) {
 
    // Initializing the answer variable
    // with first element of the array.
    let answer = arr[0];
 
    // Iterating through the whole array
    // and updating the answer variable.
    for (let i = 0; i < 6; i++) {
 
        // Checking that the answer
        // is smaller or not
        if (answer < arr[i]) {
 
            // Updating the value of
            // answer is smaller
            answer = arr[i];
        }
    }
 
    // Returning the answer that is
    // maximum element of whole array.
    return answer;
}
 
// Driver code;
let arr = [4, -2, 8, 6, 24, -28];
 
// Function call
console.log(solution(arr, 6));

Output
24



Time complexity: O(n)
Auxiliary Space: O(1)

Monovariants:

Monovariants are invariant types that increase or decrease monotonically during transformations or operations. In other words, if an object is increasing continuously or decreasing continuously under certain transformations then we say that the object is a mono-variant. 

Example: Let’s have an example for better understanding:

Given a string of opening and closing brackets as a string. Find whether the string is balanced or not (It means every open bracket has a corresponding closed bracket or not).

Below is the implementation  based on the above steps:




// C++ code of the above approach
#include <iostream>
using namespace std;
 
// Function that will return
// a boolean value.
bool solution(string s)
{
 
    // Initializing the value
    // of count to 0;
    int count = 0;
 
    // Traversing the whole string.
    for (int i = 0; i < s.length(); i++) {
 
        // Increasing the value of count
        // if we get '(' char at that index.
        if (s[i] == '(') {
            count++;
        }
 
        // Decreasing the value of the
        // count if we get ')' char
        // at that index.
        else {
            count--;
        }
 
        // Return false if a closed bracket
        // occur before the opening
        // bracket.
        if (count < 0) {
            return false;
        }
    }
 
    // Return true if the value
    // of the count is 0
    if (count == 0)
        return true;
 
    // If the count is not 0
    // then we return false.
    return false;
}
 
// Driver code
int main()
{
    string s1 = "(()()()(()))";
    string s2 = "(()()((()))";
 
    // Function call
    cout << solution(s1) << endl;
    cout << solution(s2) << endl;
 
    return 0;
}




// Java code of the above approach
 
import java.io.*;
 
class GFG {
 
    // Function that will return a boolean value.
    static boolean solution(String s)
    {
 
        // Initializing the value of count to 0;
        int count = 0;
 
        // Traversing the whole string.
        for (int i = 0; i < s.length(); i++) {
            // Increasing the value of count if we get '('
            // char at that index.
            if (s.charAt(i) == '(') {
                count++;
            }
 
            // Decreasing the value of the count if we get
            // ')' char at that index.
            else {
                count--;
            }
 
            // Return false if a closed bracket occur before
            // the opening bracket.
            if (count < 0) {
                return false;
            }
        }
 
        // Return true if the value of the count is 0
        if (count == 0) {
            return true;
        }
 
        // If the count is not 0 then we return false.
        return false;
    }
 
    public static void main(String[] args)
    {
        String s1 = "(()()()(()))";
        String s2 = "(()()((()))";
 
        // Function call
        System.out.println(solution(s1) ? 1 : 0);
        System.out.println(solution(s2) ? 1 : 0);
    }
}
 
// This code is contributed by karthik.




# Function that will return
# a boolean value.
def solution(s):
       
    # Initializing the value
    # of count to 0;
    count = 0
     
     
    # Traversing the whole string.
    for i in range(len(s)):
          # Increasing the value of count
        # if we get '(' char at that index.
        if s[i] == '(':
            count += 1
        # Decreasing the value of the
        # count if we get ')' char
        # at that index.
        else:
            count -= 1
         
        # Return false if a closed bracket
        # occur before the opening
        # bracket.
        if count < 0:
            return 1
     
    # Return true if the value
    # of the count is 0
    if count == 0:
        return 1
     
    # If the count is not 0
    # then we return false
    return 0
 
   
# Driver code
s1 = "(()()()(()))"
s2 = "(()()((()))"
 
print(solution(s1))
print(solution(s2))




using System;
 
// Function that will return
// a boolean value.
public class Solution {
    public static bool IsValid(string s) {
       
          // Initializing the value
        // of count to 0;
        int count = 0;
       
          // Traversing the whole string.
        for (int i = 0; i < s.Length; i++) {
               
              // Increasing the value of count
            // if we get '(' char at that index.
            if (s[i] == '(') {
                count++;
            }
           
              // Decreasing the value of the
                // count if we get ')' char
            // at that index.
            else {
                count--;
            }
           
              // Return false if a closed bracket
                // occur before the opening
            // bracket.
            if (count < 0) {
                return false;
            }
        }
           
          // Return result
        return count == 0;
    }
 
       
    // Driver code
    public static void Main(string[] args) {
        string s1 = "(()()()(()))";
        string s2 = "(()()((()))";
        Console.WriteLine(IsValid(s1));
        Console.WriteLine(IsValid(s2));
    }
}




// JavaScript code of the above approach
 
// Function that will return
// a boolean value.
function solution(s) {
    // Initializing the value
    // of count to 0;
    let count = 0;
    // Traversing the whole string.
    for (let i = 0; i < s.length; i++) {
        // Increasing the value of count
        // if we get '(' char at that index.
        if (s[i] === '(') {
            count++;
        }
        // Decreasing the value of the
        // count if we get ')' char
        // at that index.
        else {
            count--;
        }
        // Return false if a closed bracket
        // occur before the opening
        // bracket.
        if (count < 0) {
            return false;
        }
    }
    // Return true if the value
    // of the count is 0
    if (count === 0)
        return true;
    // If the count is not 0
    // then we return false.
    return false;
}
// Driver code
let s1 = "(()()()(()))";
let s2 = "(()()((()))";
// Function call
console.log(solution(s1));
console.log(solution(s2));

Output
1
0

Difference between Invariant and Monovariant?

S.N

Invariant

Monovariant

1. A property or value that does not change its value throughout the number of processes or transformations. A function that changes its value in number of operations or transformations, but in a consistent way. (i.e. continuously increasing or decreasing manner).
2. This is used to prove that two figures or objects are similar and congruent or equivalent. This is used to analyze the change in value in the number of transformations by tracking.
3. Usually involves identifying a value or property that is preserved by certain operations. Usually involves identifying a value or property that consistently increases or decreases during a process.
4. Can be used to simplify complex problems by reducing them to a more manageable form. can be used to analyze complex systems by tracing changes over time or operations.
5. example:- Sum of angles of any figure is an invariant property. Example:- the area of a figure is a monovariant property.the 

Advantages of Invariants and monovariants:

Disadvantages of Invariants and monovariants:

Applications of Invariants and monovariants:


Article Tags :