Skip to content

Commit 27fd000

Browse files
author
ishan.gupta
committed
count number od teams
1 parent dec5cff commit 27fd000

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

count-number-of-teams.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using namespace std;
2+
#include <iostream>
3+
#include <bits/stdc++.h>
4+
5+
// Approach used - Thought process is that since n is 10^3 means it should be solve in
6+
// N^2 or NlogN or N^2logN complexity. So for this I have used time complexity of N^2
7+
// and it is still better than lot of other submissions.
8+
// What I did is I looped on all the values and while looping, at certain index 'i' I thought
9+
// of making two arrays - one on the right side of index i and other on left side of index i.
10+
// so eventually array will be splitted like -
11+
// [12,5,3,9,7,1,6,13,10,17]
12+
// ^ -> suppose I am at value 7.
13+
// [12,5,3,9] 7 [1,6,13,10,17]
14+
// left arr indx right arr
15+
16+
// do a second loop on left and right array and make a count of all the values greater and smaller
17+
// than rating[i] i.e 7 in this case.
18+
19+
// [ 12 , 5,3, 9 ]
20+
// llow=0 lhigh=i-1
21+
22+
23+
// rating[i] - 7
24+
25+
26+
//[ 1 , 6,13,10, 17 ]
27+
// rlow=i+1 rhigh=n-1 n is size of arr.
28+
29+
// cnt+=(llessThan*rgreaterThan)+(lgreaterThan*rlessThan); will give the count of all the pairs
30+
class Solution {
31+
public:
32+
int numTeams(vector<int>& rating) {
33+
int cnt=0;
34+
for( int i=0;i<rating.size();i++){
35+
int val = rating[i];
36+
int llow = 0;
37+
int lhigh = i-1;
38+
int rlow = i+1;
39+
int rhigh = rating.size()-1;
40+
int llessThan=0;
41+
int lgreaterThan=0;
42+
int rlessThan=0;
43+
int rgreaterThan=0;
44+
if(lhigh>=0){
45+
for(int j=llow;j<=lhigh;j++){
46+
if(rating[j]<val){
47+
llessThan++;
48+
}
49+
if(rating[j]>val){
50+
lgreaterThan++;
51+
}
52+
}
53+
}
54+
if(rlow<=rating.size()-1) {
55+
for(int k=rlow;k<=rhigh;k++){
56+
if(rating[k]<val){
57+
rlessThan++;
58+
}
59+
if(rating[k]>val){
60+
rgreaterThan++;
61+
}
62+
}
63+
}
64+
65+
cnt+=(llessThan*rgreaterThan)+(lgreaterThan*rlessThan);
66+
}
67+
return cnt;
68+
}
69+
};

0 commit comments

Comments
 (0)