Skip to content

Commit 2eee77c

Browse files
committed
Added problems
1 parent cae417e commit 2eee77c

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed

Prerequisites/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

Time-complexity-analysis/Input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4
2+
1 2 3 4
19.6 KB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
13+
typedef long long ll;
14+
15+
ll kadane(int arr[], int n){
16+
ll curr_sum = 0;
17+
ll max_so_far = INT_MIN;
18+
for (int i = 0; i < n; ++i)
19+
{
20+
curr_sum = curr_sum +arr[i];
21+
if (max_so_far<curr_sum)
22+
{
23+
max_so_far = curr_sum;
24+
}
25+
26+
if (curr_sum<0)
27+
{
28+
curr_sum = 0;
29+
}
30+
}
31+
return max_so_far;
32+
}
33+
ll maxSubarraySum(int* arr,int n,int k){
34+
ll kadanes_sum = kadane(arr, n);
35+
if (k==1)
36+
{
37+
return kadanes_sum;
38+
}
39+
40+
ll curr_prefix_sum=0, curr_sufffix_sum=0;
41+
ll max_prefix_sum=INT_MIN, max_suffix_sum=INT_MIN;
42+
43+
for (int i = 0; i < n; ++i)
44+
{
45+
curr_prefix_sum = curr_prefix_sum + arr[i];
46+
max_prefix_sum = max(max_prefix_sum, curr_prefix_sum);
47+
}
48+
ll totalSum = curr_prefix_sum;
49+
for (int i = n-1; i >=0; --i)
50+
{
51+
curr_sufffix_sum += arr[i];
52+
max_suffix_sum = max(max_suffix_sum, curr_sufffix_sum);
53+
}
54+
55+
ll ans;
56+
if (totalSum<0)
57+
{
58+
ans=max(max_suffix_sum+max_suffix_sum, kadanes_sum);
59+
}else{
60+
ans = max(max_suffix_sum+max_prefix_sum+totalSum*(k-2), kadanes_sum);
61+
}
62+
return ans;
63+
64+
65+
66+
}
67+
int main( int argc , char ** argv )
68+
{
69+
ios_base::sync_with_stdio(false) ;
70+
cin.tie(NULL) ;
71+
72+
int t;
73+
cin>>t;
74+
while(t--){
75+
int n,k;
76+
cin>>n>>k;
77+
int arr[n];
78+
for (int i = 0; i < n; ++i)
79+
{
80+
std::cin>>arr[i];
81+
}
82+
cout << maxSubarraySum(arr, n, k) << '\n';
83+
84+
}
85+
86+
87+
return 0 ;
88+
89+
90+
91+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
13+
14+
int main( int argc , char ** argv )
15+
{
16+
ios_base::sync_with_stdio(false) ;
17+
cin.tie(NULL) ;
18+
19+
int n;
20+
cin>>n;
21+
int* arr=new int[n];
22+
23+
for (int i = 0; i < n; ++i)
24+
{
25+
cin>>arr[i];
26+
}
27+
28+
long double sum=0;
29+
30+
for (int i = 0; i < n; ++i)
31+
{
32+
sum+=(long double)log10(arr[i]);
33+
}
34+
35+
for (int i = 0; i < n; ++i)
36+
{
37+
cout << int(pow((long double)10.00, sum-log10(arr[i]))) << " ";
38+
}
39+
40+
41+
42+
return 0 ;
43+
44+
45+
46+
}

0 commit comments

Comments
 (0)