Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fineanmol authored Nov 2, 2021
2 parents 1394e57 + 893b590 commit cd6035e
Show file tree
Hide file tree
Showing 218 changed files with 15,170 additions and 2,519 deletions.
36 changes: 36 additions & 0 deletions 0 - 1 Knapsack/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Problem Statement:

You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).


Example 1:

Input:
N = 3
W = 4
values[] = {1,2,3}
weight[] = {4,5,1}
Output: 3

Example 2:

Input:
N = 3
W = 3
values[] = {1,2,3}
weight[] = {4,5,6}
Output: 0


Your Task:
Complete the function knapSack() which takes maximum capacity W, weight array wt[], value array val[], and the number of items n as a parameter and returns the maximum possible value you can get.

Expected Time Complexity: O(N*W).
Expected Auxiliary Space: O(N*W)

Constraints:
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ v[i] ≤ 1000
75 changes: 75 additions & 0 deletions 0 - 1 Knapsack/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include<bits/stdc++.h>
using namespace std;


// } Driver Code Ends


#define rep(i,n) for(int i=0;i<(n);++i)
class Solution
{
public:
//Function to return max value that can be put in knapsack of capacity W.
int knapSack(int W, int wt[], int val[], int N)
{

int t[N + 1][W + 1];

rep(i, N + 1)
{
rep(j, W + 1)
{
if (i == 0 || j == 0)
{
t[i][j] = 0;
}
else
{
if (wt[i - 1] <= j)
{
t[i][j] = max ( (val[i - 1] + t[i - 1][j - wt[i - 1]] ) , (t[i - 1][j]) );
}
else
{
t[i][j] = t[i - 1][j];
}
}
}
}

return t[N][W];


}
};

// { Driver Code Starts.

int main()
{
//taking total testcases
int t;
cin >> t;
while (t--)
{
//reading number of elements and weight
int n, w;
cin >> n >> w;

int val[n];
int wt[n];

//inserting the values
for (int i = 0; i < n; i++)
cin >> val[i];

//inserting the weights
for (int i = 0; i < n; i++)
cin >> wt[i];
Solution ob;
//calling method knapSack()
cout << ob.knapSack(w, wt, val, n) << endl;

}
return 0;
} // } Driver Code Ends
49 changes: 49 additions & 0 deletions AlternatingCharacters
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Complete the alternatingCharacters function below.
static int alternatingCharacters(String s) {
int count =0;


for(int i=0; i<s.length()-1; i++){
if(s.charAt(i) == s.charAt(i+1)){

count++;

}
}

return count;

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();

int result = alternatingCharacters(s);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}

bufferedWriter.close();

scanner.close();
}
}
70 changes: 70 additions & 0 deletions BitTrick.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
/**
* @brief Check the number is Power of 2 or not
*
*/
int n;
cin >> n;
if (n & (n - 1))
{
cout << "NO";
}
else
{
cout << "YES";
}
/**
* @brief Convert UpperCase to LowerCase and Viceversa
*
*/
char ch;
cin >> ch;
ch |= 1 << 5; // UpperCase to LowerCase
std::cout << ch << std::endl;
ch ^= 1 << 5; // LowerCase to UpperCase
std::cout << ch << std::endl;
/**
* @brief Find the number of set bits in a number
*
*/
int n;
cin >> n;
std::cout << __builtin_popcount(n) << std::endl;
/**
* @brief Find the number which is not Repeated in the array
*
*/
vector<int> arr = {1, 5, 2, 1, 3, 4, 5, 8, 4, 8, 3};
int ans = 0;
for (size_t i = 0; i < arr.size(); i++)
{
ans ^= arr[i];
}
std::cout << ans << std::endl;
/**
* @brief Odd Even using bits
*
*/
int n;
cin >> n;
if (n & 1)
{
cout << "Odd";
}
else
{
cout << "Even";
}
/**
* @brief multiply by two and divide by two
*
*/
int n;
cin >> n;
cout << (n << 1) << endl; // multiply by two
cout << (n >> 1) << endl; // divide by two
return 0;
}
Loading

0 comments on commit cd6035e

Please sign in to comment.