From 56efd1b837ed32961781d78b73106406003e9a1b Mon Sep 17 00:00:00 2001 From: Vivek Anand Singh <17vivekanandsingh@gmail.com> Date: Mon, 28 Oct 2024 18:37:15 +0530 Subject: [PATCH] Added the code and README for Increasing Triplet Subsequence --- .../Increasing Triplet Subsequence/Program.c | 78 +++++++++++++++++++ .../Increasing Triplet Subsequence/README.md | 74 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 Greedy Algorithms/Increasing Triplet Subsequence/Program.c create mode 100644 Greedy Algorithms/Increasing Triplet Subsequence/README.md diff --git a/Greedy Algorithms/Increasing Triplet Subsequence/Program.c b/Greedy Algorithms/Increasing Triplet Subsequence/Program.c new file mode 100644 index 00000000..9375445c --- /dev/null +++ b/Greedy Algorithms/Increasing Triplet Subsequence/Program.c @@ -0,0 +1,78 @@ +#include +#include + +// Function to check for an increasing triplet subsequence in the given array +bool increasingTriplet(int* nums, int n) +{ + // If the length of the array is less than 3, an increasing triplet cannot exist + if (n < 3) return false; + + // Initialize the prefix minimum with the first element + int prefixMin = nums[0]; + // Declare an array to store the maximum elements from the right side + int suffixMax[n]; + + // Set the last element of suffixMax to the last element of the input array + suffixMax[n - 1] = nums[n - 1]; + + // Calculate the suffixMax array by iterating from the end of the array + for (int i = n - 2; i >= 0; i--) + { + // Store the maximum value between the current element and the next suffixMax + suffixMax[i] = (suffixMax[i + 1] > nums[i]) ? suffixMax[i + 1] : nums[i]; + } + + // Iterate from the second element to the second-to-last element + for (int i = 1; i < n - 1; i++) + { + // Check if there exists a prefix min less than the current element and a suffix max greater than the current element + if (prefixMin < nums[i] && suffixMax[i] > nums[i]) + { + return true; // Triplet found + } + // Update the prefixMin to the minimum of itself and the current element + prefixMin = (prefixMin < nums[i]) ? prefixMin : nums[i]; + } + + // Return false if no increasing triplet subsequence is found + return false; +} + +// Main function to test the increasingTriplet function +int main() +{ + int n; + + // Prompt the user for the number of elements in the array + printf("Enter the number of elements in the array: "); + scanf("%d", &n); + + // Check for valid array size + if (n < 3) { + printf("Array must have at least 3 elements to find an increasing triplet.\n"); + return 1; // Exit with error code + } + + int nums[n]; + + // Prompt the user to enter the elements of the array + printf("Enter the elements of the array (space-separated):\n"); + for (int i = 0; i < n; i++) + { + scanf("%d", &nums[i]); + } + + // Call the function to check for an increasing triplet subsequence + bool result = increasingTriplet(nums, n); + + if (result) + { + printf("An increasing triplet subsequence exists in the array.\n"); + } + else + { + printf("No increasing triplet subsequence exists in the array.\n"); + } + + return 0; +} diff --git a/Greedy Algorithms/Increasing Triplet Subsequence/README.md b/Greedy Algorithms/Increasing Triplet Subsequence/README.md new file mode 100644 index 00000000..9bc9eb6a --- /dev/null +++ b/Greedy Algorithms/Increasing Triplet Subsequence/README.md @@ -0,0 +1,74 @@ +# Increasing Triplet Subsequence Problem + +## Description + +This program determines whether there exists an increasing triplet subsequence within a list of integers provided by the user. It employs a linear-time algorithm that maintains a prefix minimum and a suffix maximum to efficiently check for the presence of the triplet. + +## Structures + +1. **Input Data** + + - Description: An array to hold the input integers. + - Members: + - `int *nums`: A pointer to an array of integers representing the input sequence. + +## Functions + +**bool increasingTriplet(int* nums, int n)** + + - **Parameters**: + - `int* nums`: A pointer to the array of integers. + - `int n`: The number of elements in the array. + - **Returns**: + - `true` if there exists an increasing triplet subsequence. + - `false` otherwise. + + - **Description**: This function checks for the existence of an increasing triplet subsequence in the input array by: + - Initializing a prefix minimum to track the smallest element encountered so far. + - Iterating through the array to check for the triplet condition. + +## Main Function + +- **Description**: The entry point of the program. +- **Details**: + - Prompts the user to enter the number of elements in the array (n). + - Prompts the user to enter the elements of the array. + - Calls the `increasingTriplet` function to check for the existence of the triplet subsequence. + - Prints whether an increasing triplet subsequence exists in the array. + +## Example + +### Input + +``` +Enter the number of elements in the array: 5 +Enter the elements of the array (space-separated): 1 2 3 4 5 +``` + +### Output + +``` +An increasing triplet subsequence exists in the array. +``` + +### Additional Examples + +**Example 1:** + +- Input: `nums = [5,4,3,2,1]` +- Output: `false` +- Explanation: No triplet exists. + +--- + +**Example 2:** + +- Input: `nums = [2,1,5,0,4,6]` +- Output: `true` +- Explanation: The triplet `(3, 4, 5)` is valid because `nums[3] == 0 < nums[4] == 4 < nums[5] == 6`. + + +## Notes + +- The algorithm runs in O(n) time complexity and O(n) space complexity due to the auxiliary array used for suffix maximums. +- This program is designed to handle inputs of varying lengths and values, ensuring robustness and efficiency. \ No newline at end of file