Skip to content

Commit

Permalink
Merge pull request #1374 from vivek-anand-singh/increasingtriplet
Browse files Browse the repository at this point in the history
[NEW ALGORITHM] Increasing Triplet Subsequence
  • Loading branch information
pankaj-bind authored Oct 29, 2024
2 parents 70dd9e2 + 56efd1b commit 0633934
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Greedy Algorithms/Increasing Triplet Subsequence/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdbool.h>

// 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;
}
74 changes: 74 additions & 0 deletions Greedy Algorithms/Increasing Triplet Subsequence/README.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 0633934

Please sign in to comment.