Magic Triplets | Practice | GeeksforGeeks

Given an array of size n, a triplet (a[i], a[j], a[k]) is called a Magic Triplet if a[i] < a[j] < a[k] and i < j < k.  Count the number of magic triplets in a given array.
 
Example 1:
Input: arr = [3, 2, 1]
Output: 0
Explanation: There is no magic triplet.


Example 2:
Input: arr = [1, 2, 3, 4]
Output: 4
Explanation: Fours magic triplets are 
(1, 2, 3), (1, 2, 4), (1, 3, 4) and 
(2, 3, 4).

 
Your Task:
You don't need to read or print anything. Your task is to complete the function countTriplets() which takes the array nums[] as input parameter and returns the number of magic triplets in the array.
 
Expected Time Complexity: O(N2) 
Expected Space Complexity: O(N)
 
Constraints:
1 <= length of array <= 1000
1 <= arr[i] <= 100000
0
nguyentronghuy061 month ago
class Solution{
	public:
	int countTriplets(vector<int>nums){
	   int count = 0;
       int length = nums.size();
       for(int i=1;i<length-1;i++) {
           int temp = 0;
           for(int j=i+1;j<length;j++)
           if(nums[j]>nums[i])
           temp++;
           int temp1 = 0;
           for(int j=i-1;j>=0;j--)
           if(nums[j]<nums[i])
           temp1++;
           count += temp*temp1;
       }
       return count;
	}
};
0
manishbit232 months ago
//C++ solution
class Solution{
	public:
	int countTriplets(vector<int>nums){
	   int count = 0;
       int length = nums.size();
       for(int i=1;i<length-1;i++) {
           int temp = 0;
           for(int j=i+1;j<length;j++)
           if(nums[j]>nums[i])
           temp++;
           int temp1 = 0;
           for(int j=i-1;j>=0;j--)
           if(nums[j]<nums[i])
           temp1++;
           count += temp*temp1;
       }
       return count;
	}
};
0
insanelion4 months ago
int count = 0;       int len = nums.length;       //outer for loop helps us keep track of middle element in triplets       for(int i=1;i<len-1;i++) {           //right           int temp = 0;           for(int j=i+1;j<len;j++)           if(nums[j]>nums[i])           temp++;           int temp1 = 0;           //left           for(int j=i-1;j>=0;j--)           if(nums[j]<nums[i])           temp1++;           count += temp*temp1;       }       return count;
0
shobhitsingh11046 months ago
Short and simple Solution . 
 
int countTriplets(vector<int>nums){      int cnt = 0 , n = nums.size();   for(int i=1;i<n-1;i++)   {       int l = i-1 , r = i+1 , c1 = 0 , c2 = 0;       while(l>=0) c1+=(nums[l]<nums[i]) , l--;       while(r<n) c2+=(nums[r]>nums[i]) , r++;       cnt+=c1*c2;   }   return cnt;}
0
tauruas08057 months ago
vector<int>b(nums.size(),0);int n=nums.size();  for(int i=0;i<n;i++){      for(int j=i+1;j<n;j++){          if(nums[j]>nums[i])b[i]++;      }  }  int c=0;  for(int i=0;i<n;i++){      for(int j=i+1;j<n;j++){          if(nums[j]>nums[i]){            c+=b[j];            }      }  }  return c;
0
reaper277 months ago
Easy Solution Python
class Solution:
	def countTriplets(self, nums):
	    n=len(nums)
	    ans=0
		count=[None]*n
		for i in range(n-1,-1,-1):
		    cnt=[]
		    for j in range(i+1,n):
		        if nums[i]<nums[j]:cnt.append(j)
		    count[i]=cnt
		for i in range(n-2):
		    tmp=count[i]
		    for j in tmp:ans+=len(count[j])
		return ans
0
GeeksforGeeks ✔8 months ago
GeeksforGeeks ✔
public int countTriplets(int[] nums){        int n = nums.length;                int res = 0;                for(int i = 1; i < n - 1; i++){            int temp1 = 0;            // Left Smaller            for(int j = i - 1; j >= 0; j--){                if(nums[i] > nums[j]) temp1++;            }                        if(temp1 == 0) continue; // No left smaller                        int temp2 = 0;            // right greater            for(int j = i + 1; j < n; j++){                if(nums[i] < nums[j]) temp2++;            }                        res+= (temp1 * temp2);        }                return res;    }
0
wallflower9 months ago
wallflower
Java O(n^2) solution
https://ide.geeksforgeeks.o...
0
DIPU DEEPAK
This comment was deleted.
+1
Debarshi Maitra1 year ago
Debarshi Maitra
O(N LogN) Solution using Binary Index Tree : https://ide.geeksforgeeks.o...Auxiliary Space : O(N)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.