Skip to content

Commit a02c68f

Browse files
committed
leetcode 1027
1 parent f6820c9 commit a02c68f

File tree

6 files changed

+250
-0
lines changed

6 files changed

+250
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int n;
12+
cin>>n;
13+
vector<int> v(n);
14+
for(auto &x:v){
15+
cin>>x;
16+
}
17+
sort(v.begin(),v.end());
18+
int ans=0;
19+
for(int i=0;i<n/2;i++){
20+
ans+=(v[n-1-i]-v[i]);
21+
}
22+
cout<<ans<<endl;
23+
return 0;
24+
}
25+
int main()
26+
{
27+
int testCase=1;
28+
cin>>testCase;
29+
while(testCase--){
30+
solve();
31+
}
32+
return 0;
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int n;
12+
cin>>n;
13+
long long sum=0;
14+
vector<int> v(n);
15+
for(auto &x:v){
16+
cin>>x;
17+
sum += abs(x);
18+
}
19+
int ans=0;
20+
int count=0;
21+
for(auto x:v){
22+
if(x==0 && count==0){
23+
continue;
24+
}
25+
if(x<=0){
26+
count++;
27+
}
28+
else{
29+
if(count>0){
30+
ans++;
31+
}
32+
count=0;
33+
}
34+
}
35+
if(count){
36+
ans++;
37+
}
38+
cout<<sum<<" "<<ans<<endl;
39+
return 0;
40+
}
41+
int main()
42+
{
43+
int testCase=1;
44+
cin>>testCase;
45+
while(testCase--){
46+
solve();
47+
}
48+
return 0;
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
long long n;
12+
cin>>n;
13+
long long sum=0;
14+
while(n){
15+
sum+=n;
16+
n = n/2;
17+
}
18+
cout<<sum<<endl;
19+
return 0;
20+
}
21+
int main()
22+
{
23+
int testCase=1;
24+
cin>>testCase;
25+
while(testCase--){
26+
solve();
27+
}
28+
return 0;
29+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
vector<vector<int>> adj;
10+
vector<int> value;
11+
12+
int dfs(int at, int from){
13+
int count=0;
14+
for(auto child: adj[at]){
15+
if(child!=from){
16+
count+=dfs(child,at);
17+
}
18+
}
19+
if(count==0 && adj[at].size()==1){
20+
count++;
21+
}
22+
value[at]=count;
23+
return count;
24+
}
25+
26+
27+
int solve(){
28+
int n;
29+
cin>>n;
30+
adj.resize(n+1);
31+
value.resize(n+1,0);
32+
for(int i=0;i<n-1;i++){
33+
int u,v;
34+
cin>>u>>v;
35+
adj[u].push_back(v);
36+
adj[v].push_back(u);
37+
}
38+
dfs(1,1);
39+
int q;
40+
cin>>q;
41+
for(int i=1;i<=q;i++){
42+
int u,v;
43+
cin>>u>>v;
44+
long long ans=((long long)value[u]*(long long)value[v]);
45+
cout<<ans<<endl;
46+
}
47+
adj.clear();
48+
value.clear();
49+
return 0;
50+
}
51+
int main()
52+
{
53+
int testCase=1;
54+
cin>>testCase;
55+
while(testCase--){
56+
solve();
57+
}
58+
return 0;
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
long long n,m;
12+
cin>>n>>m;
13+
long long ans=0;
14+
for(int i=1;i<=n;i++){
15+
long long temp=i%5;
16+
temp=(m+temp)/5;
17+
ans+=temp;
18+
}
19+
cout<<ans<<endl;
20+
return 0;
21+
}
22+
int main()
23+
{
24+
int testCase=1;
25+
while(testCase--){
26+
solve();
27+
}
28+
return 0;
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll;
6+
const ll INF = 1e18;
7+
const ll mod1 = 1e9 + 7;
8+
const ll mod2 = 998244353;
9+
// Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
int longestArithSeqLength(vector<int> &nums)
15+
{
16+
int n = nums.size();
17+
if (n <= 2)
18+
{
19+
return n;
20+
}
21+
22+
// Create a vector of unordered maps
23+
vector<unordered_map<int, int>> dp(n);
24+
25+
int maxLength = 2;
26+
27+
for (int i = 0; i < n; i++)
28+
{
29+
for (int j = 0; j < i; j++)
30+
{
31+
int diff = nums[i] - nums[j];
32+
33+
// If the difference exists in the subsequence ending at index j
34+
// Extend the subsequence and update the length in dp[i]
35+
if (dp[j].find(diff) != dp[j].end())
36+
{
37+
dp[i][diff] = dp[j][diff] + 1;
38+
}
39+
else
40+
{
41+
// If the difference doesn't exist, initialize it to 2
42+
dp[i][diff] = 2;
43+
}
44+
45+
maxLength = max(maxLength, dp[i][diff]);
46+
}
47+
}
48+
49+
return maxLength;
50+
}
51+
};

0 commit comments

Comments
 (0)