PayPal | OA | SDE
Anonymous User
5251

Round was on Hackerearth platform having two coding questions. Duration: 1 hour 20 minutes.

  1. Maximum Triangular Subsequence:
    Given an array of integers A, find the length of largest triangular subsequence. A subsequence is called triangular if every triplet S[i],S[j],S[k] belonging to subsequence S satisfies triangle inequality for i<j<k.
    Example Input: A=[4,5,4,10,4]
    Example Output= 4
    Explanation: [4,5,4,4] is largest valid triangular subsequence. In [4,5,4,10,4] , 4+4<10, so not valid.
    Solution with one test case failing: (Please mention the one corner case in comments)
int main(){
		int n, a;
		cin>>n;
		vector<int> v;
		for(int i=0;i<n;i++){
				cin>>a;
				v.push_back(a);
		}
		sort(v.begin(),v.end());
		int ans=0;
		vector<int>::iterator itr;
		for(int i=0;i<n-2;i++){
				int k=v[i]+v[i+1];
				itr=lower_bound(v.begin(),v.end(),k);
				int z=itr-v.begin();
				ans=max(ans,z-i);
		}
		cout<<ans;
		return 0;
}
  1. Beautiful Subsequence: Given a string s of length n containing only letters 'a' and 'b'. Find the lexicographically smallest subsequence of s of length k having atleast x number of 'b' .
    Example Input: n=6, k=4, x=2 , s=aababb
    Example Output: aabb
    Constraints for n: [1,1e5]
    Tried Brute force recursive solution but got TLE for most of the test cases. Please Do suggest better approach in comments.
Comments (14)