File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ /*
3+ TC -O(N)
4+ SC-O(N)
5+ */
6+
7+ #include < iostream>
8+ #include < vector>
9+ #include < algorithm>
10+
11+ using namespace std ;
12+
13+ void solve () {
14+ int n;
15+ cin >> n;
16+ vector<int > a (n);
17+ int min_val = 1e9 , max_val = -1e9 ;
18+ for (int i = 0 ; i < n; i++) {
19+ cin >> a[i];
20+ if (a[i] < min_val) min_val = a[i];
21+ if (a[i] > max_val) max_val = a[i];
22+ }
23+ if (min_val == max_val) {
24+ cout << 1LL * n * (n - 1 ) << endl;
25+ return ;
26+ }
27+ long long count_min = 0 ;
28+ long long count_max = 0 ;
29+ for (int i = 0 ; i < n; i++) {
30+ if (a[i] == min_val) count_min++;
31+ if (a[i] == max_val) count_max++;
32+ }
33+ cout << 2 * count_min * count_max << endl;
34+ }
35+ int main () {
36+ int t;
37+ cin >> t;
38+ while (t--) {
39+ solve ();
40+ }
41+ return 0 ;
42+ }
Original file line number Diff line number Diff line change 1+ /*
2+ TC - O(N^2)
3+ SC - O(N^2)
4+ */
5+
6+
7+ #include < iostream>
8+ #include < vector>
9+ #include < algorithm>
10+ #include < map>
11+
12+ using namespace std ;
13+ long long C[1005 ][1005 ];
14+ const int MOD = 1e9 + 7 ;
15+ void precompute () {
16+ for (int i = 0 ; i <= 1000 ; i++) {
17+ C[i][0 ] = 1 ;
18+ for (int j = 1 ; j <= i; j++) {
19+ C[i][j] = (C[i - 1 ][j - 1 ] + C[i - 1 ][j]) % MOD;
20+ }
21+ }
22+ }
23+
24+ void solve () {
25+ int n, k;
26+ cin >> n >> k;
27+ vector<int > a (n);
28+ map<int , int > total_freq;
29+
30+ for (int i = 0 ; i < n; i++) {
31+ cin >> a[i];
32+ total_freq[a[i]]++;
33+ }
34+
35+ sort (a.rbegin (), a.rend ());
36+
37+ int v = a[k - 1 ];
38+ int needed_count = 0 ;
39+
40+ for (int i = 0 ; i < k; i++) {
41+ if (a[i] == v) needed_count++;
42+ }
43+
44+ int total_count = total_freq[v];
45+
46+ cout << C[total_count][needed_count] << " \n " ;
47+ }
48+
49+ int main () {
50+
51+ precompute ();
52+
53+ int t;
54+ cin >> t;
55+ while (t--) {
56+ solve ();
57+ }
58+ return 0 ;
59+ }
Original file line number Diff line number Diff line change 1+ // #include <bits/stdc++.h>
2+ #include < iostream>
3+ #include < vector>
4+ #include < string>
5+ #include < algorithm>
6+ #include < map>
7+ #include < set>
8+ #include < unordered_map>
9+ #include < queue>
10+ #include < stack>
11+ #include < cmath>
12+ #include < climits>
13+ #define int long long
14+ #define fo (n ) for (int i = 0 ; i < n; i++)
15+ #define fo1 (ii, n ) for (int i=ii; i<n; i++)
16+ #define all (x ) x.begin(), x.end()
17+ #define rall (x ) x.rbegin(), x.rend()
18+ #define pb push_back
19+ #define fi first
20+ #define se second
21+ #define vec (a ) vector<int > a
22+ #define vecn (a,n ) vector<int > a (n)
23+ #define py cout<<" YES" <<endl
24+ #define pn cout<<" NO" <<endl
25+ const int MOD = 1e9 + 7 ;
26+ const int INF = 1e18 ;
27+ using namespace std ;
28+
29+ void solve (){
30+ int n;
31+ cin>>n;
32+ vector<int >a (n);
33+ fo (n)cin>>a[i];
34+ int count=0 ;
35+ int highest=*max_element (a.begin (),a.end ());
36+ int lowest=*min_element (a.begin (),a.end ());
37+ int difference=highest-lowest;
38+ for (int i=0 ;i<n;i++){
39+ for (int j=i+1 ;j<n;j++){
40+ if (abs (a[i]-a[j])==difference)count++;
41+ }
42+ }
43+ cout<<count*2 <<endl;
44+ }
45+
46+ signed main () {
47+ ios_base::sync_with_stdio (false );
48+ cin.tie (0 );
49+ int t;
50+ cin >> t;
51+ while (t--)
52+ solve ();
53+ return 0 ;
54+ }
Original file line number Diff line number Diff line change 1+ // Submission Link
2+ // https://codeforces.com/contest/1771/submission/355396669
3+ // Time complexity = O(n) + O(nlogn)
4+ // Space Comlexity = O(n)
5+
6+
7+
8+
9+
10+ #include < bits/stdc++.h>
11+ using namespace std ;
12+ int main (){
13+ int t;
14+ cin>>t;
15+ while (t--){
16+ long long int n;
17+ cin>>n;
18+ vector<long long int >v (n);
19+ for (int i=0 ; i<n; i++){
20+ cin>>v[i];
21+ }
22+ sort (v.begin (),v.end ());
23+ long long int mini =0 ;
24+ long long int maxi= 0 ;
25+ int i=0 ;
26+ int j = n-1 ;
27+ if (v[0 ]!=v[n-1 ]){
28+ while (v[i]==v[0 ] && i<n){
29+ mini++;
30+ i++;
31+ }
32+ while (v[j]==v[n-1 ] && j>=0 ){
33+ maxi++;
34+ j--;
35+ }
36+ cout<<2 *maxi*mini<<" \n " ;
37+ }
38+ else {
39+ cout<<(n*(n-1 ))<<" \n " ;
40+ }
41+
42+ }
43+ return 0 ;
44+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Problem: counting pairs where absolute difference equals max possible difference in array
3+
4+ My approach:
5+ basically max diff is always gonna be max_element - min_element
6+ so we just need pairs where one is max and other is min
7+ first find both values then count how many times each appears
8+ if everything is same number then answer is just n*(n-1) (all pairs work)
9+ otherwise its 2*count_max*count_min (since both (i,j) and (j,i) count as different)
10+
11+ TimeComplexity: O(n)
12+ SpaceC: O(n)
13+
14+ CodeForces submission: https://codeforces.com/contest/1771/submission/355388765
15+ */
16+
17+ #include < bits/stdc++.h>
18+ using namespace std ;
19+
20+ int main (){
21+ int t;
22+ cin >> t;
23+
24+ while (t--){
25+ int n;
26+ cin >> n;
27+ vector<int > arr (n);
28+
29+ for (int i=0 ; i<n; i++){
30+ cin >> arr[i];
31+ }
32+ int maxi = arr[0 ], mini = arr[0 ];
33+ for (int i=1 ; i<n; i++){
34+ if (arr[i] > maxi) maxi = arr[i];
35+ if (arr[i] < mini) mini = arr[i];
36+ }
37+ if (maxi == mini){
38+ long long result = (long long )n * (n-1 );
39+ cout << result << endl;
40+ continue ;
41+ }
42+ long long max_count = 0 , min_count = 0 ;
43+ for (int i=0 ; i<n; i++){
44+ if (arr[i] == maxi) max_count++;
45+ if (arr[i] == mini) min_count++;
46+ }
47+ long long answer = 2 * max_count * min_count;
48+ cout << answer << endl;
49+ }
50+
51+ return 0 ;
52+ }
Original file line number Diff line number Diff line change 5858| Apoorv Mittal | Apoorv012 | JIIT Noida |
5959| Aman Kumar Mehta | AmanMehta22 | Amity University Jharkhand | |
6060| Naman Pal | Naman2251 | IIIT Allahabad
61+ | Mohammed Tailor | tailormst | Shri Ramdeobaba College of Engineering & Management, Nagpur |
62+ | Yuvaraj Ragothaman | LevelSilence | IIIT Allahabad|
63+ | Sudhanshu | sahsudhanshu | IIIT Allahabad |
6164| Harjas |harjasbb07-eng | IIIT Allahabad
6265| Ashutosh Kesarwani | Lucifer-0612 | HIT Haldia
6366| Samarth Patel | capricemoto | IIIT Allahbad |
6467| Divakar Bhatt | wodivakar | IIIT Allahabad |
65- <!-- ADD ABOVE THIS -->
68+ | Aiyaan Mahajan | Aiyaan-Mahajan | NIT Srinagar |
69+
6670<!-- example | Korvac | Betty | Reyansh College | -->
You can’t perform that action at this time.
0 commit comments