Skip to content

Commit bd2f9b2

Browse files
committed
Practise 09-Jun-2020
1 parent 56c0dc5 commit bd2f9b2

File tree

6 files changed

+235
-18
lines changed

6 files changed

+235
-18
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Given an integer, write a function to determine if it is a power of two.
2+
3+
Example 1:
4+
5+
Input: 1
6+
Output: true
7+
Explanation: 20 = 1
8+
Example 2:
9+
10+
Input: 16
11+
Output: true
12+
Explanation: 24 = 16
13+
Example 3:
14+
15+
Input: 218
16+
Output: false
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
bool isPowerOfTwo(int n) {
24+
if(abs(floor(log2(n))-ceil(log2(n)))==0) return true;
25+
else return false;
26+
}
27+
};
28+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Given an integer, write a function to determine if it is a power of two.
2+
3+
Example 1:
4+
5+
Input: 1
6+
Output: true
7+
Explanation: 20 = 1
8+
Example 2:
9+
10+
Input: 16
11+
Output: true
12+
Explanation: 24 = 16
13+
Example 3:
14+
15+
Input: 218
16+
Output: false
17+
18+
19+
20+
21+
22+
class Solution {
23+
public:
24+
bool isPowerOfTwo(int n) {
25+
if(abs(floor(log2(n))-ceil(log2(n)))==0) return true;
26+
else return false;
27+
}
28+
};
29+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
3+
4+
Input:
5+
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'N' denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
6+
7+
8+
Output:
9+
In each seperate line print minimum absolute difference.
10+
11+
12+
Constraints:
13+
1<=T<=200
14+
1<=N<=50
15+
1<=A[i]<=200
16+
17+
18+
Example:
19+
Input:
20+
2
21+
4
22+
1 6 5 11
23+
4
24+
36 7 46 40
25+
26+
Output :
27+
1
28+
23
29+
30+
Explaination :
31+
Testcase 1:
32+
Subset1 = {1, 5, 6} ; sum of Subset1 = 12
33+
Subset2 = {11} ; sum of Subset2 = 11
34+
35+
Testcase 2:
36+
Subset1 = {7, 46} ; sum = 53
37+
Subset2 = {36, 40} ; sum = 76
38+
*/
39+
40+
41+
42+
43+
44+
45+
46+
#include<bits/stdc++.h>
47+
using namespace std;
48+
49+
int minSumPartion(int a[], int n){
50+
int range=0;
51+
for(int i=0;i<n;i++) range+=a[i];
52+
53+
bool dp[n+1][range+1];
54+
55+
for(int i=0;i<n+1;i++) dp[i][0]=true;
56+
for(int j=1;j<range+1;j++) dp[0][j]=false;
57+
58+
for(int i=1;i<n+1;i++){
59+
for(int j=1;j<range+1;j++){
60+
if(a[i-1]<=j){
61+
dp[i][j]=dp[i-1][j-a[i-1]] || dp[i-1][j];
62+
} else {
63+
dp[i][j]=dp[i-1][j];
64+
}
65+
}
66+
}
67+
68+
vector<int> v;
69+
for(int j=0;j<(range)/2 + 1;j++){
70+
if(dp[n][j]) v.push_back(j);
71+
}
72+
73+
int mini=INT_MAX;
74+
75+
for(auto x: v){
76+
int tmp=range-2*x;
77+
if(tmp<mini) mini=tmp;
78+
}
79+
80+
return mini;
81+
82+
}
83+
84+
85+
int main(){
86+
ios_base::sync_with_stdio(false);
87+
cin.tie(NULL);
88+
cout.tie(NULL);
89+
int t;
90+
cin>>t;
91+
while(t--){
92+
int n;
93+
cin>>n;
94+
int a[n];
95+
for(int i=0;i<n;i++) cin>>a[i];
96+
cout<<minSumPartion(a,n)<<endl;
97+
}
98+
99+
return 0;
100+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int subsetCountDiff(int a[], int x, int n){
5+
int dp[n+1][x+1];
6+
7+
for(int i=0;i<n+1;i++) dp[i][0]=1;
8+
for(int j=1;j<x+1;j++) dp[0][j]=0;
9+
10+
for(int i=1;i<n+1;i++){
11+
for(int j=1;j<x+1;j++){
12+
if(a[i-1]<=j){
13+
dp[i][j]=dp[i-1][j-a[i-1]] + dp[i-1][j];
14+
}
15+
else dp[i][j]=dp[i-1][j];
16+
}
17+
}
18+
19+
return dp[n][x];
20+
}
21+
22+
int main(){
23+
ios_base::sync_with_stdio(false);
24+
cin.tie(NULL);
25+
cout.tie(NULL);
26+
int t;
27+
cin>>t;
28+
while(t--){
29+
int n;
30+
cin>>n;
31+
int a[n];
32+
33+
for(int i=0;i<n;i++) cin>>a[i];
34+
35+
int diff;
36+
cin>>diff;
37+
38+
int sum=0;
39+
for(int i=0;i<n;i++) sum+=a[i];
40+
41+
int x=(diff+sum);
42+
43+
if(x%2!=0) cout<<"0"<<endl;
44+
else cout<<subsetCountDiff(a, x/2, n)<<endl;
45+
}
46+
return 0;
47+
}

dynamic programming/subset_sum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ bool subsetsum(int a[], int sum, int n){
4040

4141
for(int i=1;i<n+1;i++){
4242
for(int j=1;j<sum+1;j++){
43-
if(a[i-1]<=sum){
43+
if(a[i-1]<=j){
4444
dp[i][j]=dp[i-1][j-a[i-1]] || dp[i-1][j];
4545
}
46-
else dp[i][j]=dp[i-1][sum];
46+
else dp[i][j]=dp[i-1][j];
4747
}
4848
}
4949
return dp[n][sum];

dynamic programming/subset_sum_count.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1+
/*
2+
Given an array arr[] of length N and an integer X, the task is to find the number of subsets with sum equal to X.
3+
4+
Examples:
5+
6+
Input: arr[] = {1, 2, 3, 3}, X = 6
7+
Output: 3
8+
All the possible subsets are {1, 2, 3},
9+
{1, 2, 3} and {3, 3}
10+
11+
12+
13+
Input: arr[] = {1, 1, 1, 1}, X = 1
14+
Output: 4
15+
*/
16+
17+
18+
19+
20+
21+
122
#include<bits/stdc++.h>
223
using namespace std;
324

425
int subsetsumcount(int a[], int sum, int n){
5-
bool dp[n+1][sum+1];
26+
int dp[n+1][sum+1];
627

7-
for(int i=0;i<n+1;i++) dp[i][0]=true;
8-
for(int j=1;j<sum+1;j++) dp[0][j]=false;
28+
for(int i=0;i<n+1;i++) dp[i][0]=1;
29+
for(int j=1;j<sum+1;j++) dp[0][j]=0;
930

1031
for(int i=1;i<n+1;i++){
1132
for(int j=1;j<sum+1;j++){
12-
if(a[i-1]<=sum){
13-
dp[i][j]=dp[i-1][j-a[i-1]] || dp[i-1][j];
33+
if(a[i-1]<=j){
34+
dp[i][j]=dp[i-1][j-a[i-1]] + dp[i-1][j];
1435
}
15-
else dp[i][j]=dp[i-1][sum];
36+
else dp[i][j]=dp[i-1][j];
1637
}
1738
}
18-
for(int i=0;i<n+1;i++){
19-
for(int j=0;j<sum+1;j++){
20-
cout<<dp[i][j]<<" ";
21-
} cout<<endl;
22-
}
23-
int cnt=0;
24-
for(int i=0;i<n+1;i++){
25-
if(dp[i][sum]==true) cnt++;
26-
}
27-
return cnt;
39+
40+
return dp[n][sum];
2841
}
2942

3043
int main(){

0 commit comments

Comments
 (0)