1595784240
Given an array arr[], the task is to check that if there exist a triplet (i, j, k) such that arr[i]<arr[k]<arr[j] and i<j<k then print Yes else print No.
Examples:
Input:_ arr[] = {1, 2, 3, 4, 5}_
Output:_ No_
Explanation:
There is no such sub-sequence such that arr[i] < arr[k] < arr[j]
Input:_ arr[] = {3, 1, 5, 0, 4}_
Output:_ Yes_
Explanation:
There exist a triplet (3, 5, 4) which is arr[i] < arr[k] < arr[j]
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Naive Approach: The idea is to generate all possible triplets and if any triplets satisfy the given conditions the print Yes else print No.
Time Complexity:_ O(N3) _
Auxiliary Space:_ O(1) _
Efficient Approach: To optimize the above approach the idea is to use the stack to keep the track of the smaller elements in the right of every element in the array arr[]. Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to check if there exist
// triplet in the array such that
// i < j < k and arr[i] < arr[k] < arr[j]
**bool**
findTriplet(vector<``**int**``>& arr)
{
**int**
n = arr.size();
stack<``**int**``> st;
// Initialize the heights of h1 and h2
// to INT_MAX and INT_MIN respectively
**int**
h3 = INT_MIN, h1 = INT_MAX;
**for**
(``**int**
i = n - 1; i >= 0; i--) {
// Store the current element as h1
h1 = arr[i];
// If the element at top of stack
// is less than the current element
// then pop the stack top
// and keep updating the value of h3
**while**
(!st.empty()
&& st.top() < arr[i]) {
h3 = st.top();
st.pop();
}
// Push the current element
// on the stack
st.push(arr[i]);
// If current element is less
// than h3, then we found such
// triplet and return true
**if**
(h1 < h3) {
**return**
**true**``;
}
}
// No triplet found, hence return false
**return**
**false**``;
}
// Driver Code
**int**
main()
{
// Given array
vector<``**int**``> arr = { 4, 7, 5, 6 };
// Function Call
**if**
(findTriplet(arr)) {
cout <<
" Yes"
<<
'\n'``;
}
**else**
{
cout <<
" No"
<<
'\n'``;
}
**return**
0;
}
Output:
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #greedy #stack #cpp-stack #java #python
1595784240
Given an array arr[], the task is to check that if there exist a triplet (i, j, k) such that arr[i]<arr[k]<arr[j] and i<j<k then print Yes else print No.
Examples:
Input:_ arr[] = {1, 2, 3, 4, 5}_
Output:_ No_
Explanation:
There is no such sub-sequence such that arr[i] < arr[k] < arr[j]
Input:_ arr[] = {3, 1, 5, 0, 4}_
Output:_ Yes_
Explanation:
There exist a triplet (3, 5, 4) which is arr[i] < arr[k] < arr[j]
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Naive Approach: The idea is to generate all possible triplets and if any triplets satisfy the given conditions the print Yes else print No.
Time Complexity:_ O(N3) _
Auxiliary Space:_ O(1) _
Efficient Approach: To optimize the above approach the idea is to use the stack to keep the track of the smaller elements in the right of every element in the array arr[]. Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to check if there exist
// triplet in the array such that
// i < j < k and arr[i] < arr[k] < arr[j]
**bool**
findTriplet(vector<``**int**``>& arr)
{
**int**
n = arr.size();
stack<``**int**``> st;
// Initialize the heights of h1 and h2
// to INT_MAX and INT_MIN respectively
**int**
h3 = INT_MIN, h1 = INT_MAX;
**for**
(``**int**
i = n - 1; i >= 0; i--) {
// Store the current element as h1
h1 = arr[i];
// If the element at top of stack
// is less than the current element
// then pop the stack top
// and keep updating the value of h3
**while**
(!st.empty()
&& st.top() < arr[i]) {
h3 = st.top();
st.pop();
}
// Push the current element
// on the stack
st.push(arr[i]);
// If current element is less
// than h3, then we found such
// triplet and return true
**if**
(h1 < h3) {
**return**
**true**``;
}
}
// No triplet found, hence return false
**return**
**false**``;
}
// Driver Code
**int**
main()
{
// Given array
vector<``**int**``> arr = { 4, 7, 5, 6 };
// Function Call
**if**
(findTriplet(arr)) {
cout <<
" Yes"
<<
'\n'``;
}
**else**
{
cout <<
" No"
<<
'\n'``;
}
**return**
0;
}
Output:
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #greedy #stack #cpp-stack #java #python
1598123760
Given an array arr[] of N integers. The task is to find the maximum value of (arr[i] + arr[j] * arr[k]) among every triplet (i, j, k) such that arr[i] < arr[j] < arr[k] and i < j < k. If there doesn’t exist any such triplets then print “-1″.
Examples:
Input:_ arr[]={7, 9, 3, 8, 11, 10}_
Output:_ 106_
Explanation:
The valid triplets are:
1) (7, 9, 11), and value of (arr[i] + arr[j] * arr[k]) is 106.
2) (7, 9, 10), and value of (arr[i] + arr[j] * arr[k]) is 97.
3) (7, 8, 10), and value of (arr[i] + arr[j] * arr[k]) is 87.
4) (7, 8, 11), and value of (arr[i] + arr[j] * arr[k]) is 105.
5) (3, 8, 10), and value of (arr[i] + arr[j] * arr[k]) is 83.
6) (3, 8, 11), and value of (arr[i] + arr[j] * arr[k]) is 91.
Therefore, the maximum among the values is 106
Input:_ arr[]={1, 2, 3}_
Output:_ 7_
#arrays #mathematical #java-treeset
1621833780
As you all know laravel 8 already released and you can see there are many changes and update in laravel 8 version. many laravel users are facing issue in their new Laravel 8 version when they try to load their routes in web.php and they run into an Exception that says something like “Target class postController does not exist”.
#target class does not exist in laravel 8 #error #target class controller does not exist #target class not found #laravel #target class does not exist error solved
1621443060
This article provides an overview of core data science algorithms used in statistical data analysis, specifically k-means and k-medoids clustering.
Clustering is one of the major techniques used for statistical data analysis.
As the term suggests, “clustering” is defined as the process of gathering similar objects into different groups or distribution of datasets into subsets with a defined distance measure.
K-means clustering is touted as a foundational algorithm every data scientist ought to have in their toolbox. The popularity of the algorithm in the data science industry is due to its extraordinary features:
#big data #big data analytics #k-means clustering #big data algorithms #k-means #data science algorithms
1623397301
The Pandas library is a comprehensive tool not only for crunching numbers but also for working with text data.
For many data analysis applications and machine learning exploration/pre-processing, you’ll want to either filter out or extract information from text data. To do so, Pandas offers a wide range of in-built methods that you can use to add, remove, and edit text columns in your DataFrames.
In this piece, let’s take a look specifically at searching for substrings in a DataFrame column. This may come in handy when you need to create a new category based on existing data (for example during feature engineering before training a machine learning model).
If you want to follow along, download the dataset here.
import pandas as pd
df = pd.read_csv('vgsales.csv')
Now let’s get started!
NOTE: we’ll be using a lot of _loc_
in this piece, so if you’re unfamiliar with that method, check out the first article linked at the very bottom of this piece.
#python #data-science #software-development #check for a substring in a pandas dataframe column #pandas dataframe column #check for a substring