fork download
  1. import java.util.*;
  2. import java.io.*;
  3. public class Main
  4. {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int n=sc.nextInt();
  8. int[]nums = new int[n];
  9. for(int i=0;i<n;i++){
  10. nums[i]=sc.nextInt();
  11. }
  12.  
  13. int[] pref = new int[n];
  14. pref[0]=0;
  15. int[] suff = new int[n];
  16. suff[n-1]=0;
  17.  
  18. int j=1;
  19. while(j<=n-1){
  20. int i=0;
  21. int count=0;
  22. while(i<j){
  23. if(nums[i]>nums[j]){
  24. count++;
  25. }
  26. i++;
  27. }
  28. pref[j]=count;
  29. j++;
  30. }
  31.  
  32. j=n-2;
  33. while(j>=0){
  34. int k=n-1;
  35. int count=0;
  36. while(j<k){
  37. if(nums[j]<nums[k]){
  38. count++;
  39. }
  40. k--;
  41. }
  42. suff[j]=count;
  43. j--;
  44. }
  45. int count_triplets=0;
  46. for(int x=0;x<n;x++){
  47. count_triplets+=pref[x]*suff[x];
  48. }
  49.  
  50. System.out.print("No of triplets in the array " +count_triplets);
  51. }
  52. }
Runtime error #stdin #stdout #stderr 0.15s 56716KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:7)