├── .gitattributes
├── .gitignore
├── Difficulty: Medium
├── Jump Game
│ ├── README.md
│ └── jump-game.java
└── Topological sort
│ ├── README.md
│ └── topological-sort.java
├── arrays
├── ConvertArrayInZigZagManner.cpp
├── ElementOnLeftSideIsSmallerAndRightIsGreater.cpp
├── EquilibriumPoint.cpp
├── LastIndexOfOne.cpp
├── MaximumIndex.cpp
├── MaximumRectangularAreaInHistogram.cpp
├── RearrangeArrayAlternately.cpp
├── ReverseArrayInGroups.cpp
├── SequentialDigits.cpp
├── SmallestNumberOnLeft.cpp
├── StockBuySell.cpp
├── SubarrayWithGivenSum.cpp
├── TrappingRainWater.cpp
├── chocolate_distribution_problem.cpp
├── closest_sum_to_zero.cpp
├── count_possible_triangles.cpp
├── count_triplets.cpp
├── equilibrium_point.cpp
├── inversion_of_array.cpp
├── kadaneAlgorithm.cpp
├── kth_smallest_element.cpp
├── leaders_in_array.cpp
├── merge_without_extra_space.cpp
├── minimum_platforms.cpp
├── missing_number_in_array.cpp
├── number_of_pairs.cpp
├── rearrange_array_alternatively.cpp
└── sort_0s_1s_2s.cpp
├── backtracking
├── N_queenProblem.cpp
├── RatInMazeProblem.cpp
└── SolveTheSuduko.cpp
├── bit magic
├── bit_difference.cpp
├── check_kth_bit.cpp
├── first_set_bit.cpp
├── maximum_subset_xor.cpp
├── party_of_couples.cpp
├── powerOf2.cpp
├── rightmost_different_bit.cpp
├── rotate_bits.cpp
├── set_kth_bit.cpp
└── toggle_bits_in_range.cpp
├── divide and conquer
├── BinarySearch.cpp
├── KthElementOfTwoSortedArrays.cpp
├── MergeSort.cpp
├── SearchInRotatedArray.cpp
├── SumOfMiddleElementsOfTwoSortedArray.cpp
├── element_appeared_once_in_sorted_array.cpp
└── search_in_rotated_array.cpp
├── dynamic programming
├── 0-1Knapsack.cpp
├── BoxStacking.cpp
├── EggDropPuzzle.cpp
├── LongestCommonSubsequence.cpp
├── LongestCommonSubstring.cpp
├── LongestIncreasingSubsequence.cpp
├── LongestPathInMatrix.cpp
├── MaxLengthChain.cpp
├── MaximizeCutSegments.cpp
├── MaximumSumIncreasingSubsequence.cpp
├── MinimumNoOfJumps.cpp
├── MinimumOperations.cpp
├── MinimumSumPartition.cpp
├── NoOfWaysToCoverDistance.cpp
├── OptimalStrategyForGame.cpp
├── PartitionEqualSubsetSum.cpp
├── ShortestCommonSupersequence.cpp
├── coin_change.cpp
└── edit_distance.cpp
├── graph
├── AlienDictionary.cpp
├── CircleOfStrings.cpp
├── DetectCycleInDirectedGraph.cpp
├── DetectCycleInUndirectedGraph.cpp
├── FindIfPathExists.cpp
├── FindNoOfIslands.cpp
├── FloydWarshall.cpp
├── Kosaraju'sAlgo.cpp
├── MinimumCostPath.cpp
├── MinimumSwapToSort.cpp
├── ShortestSourceToDestinationPath.cpp
├── SnakeAndLadder.cpp
├── StronglyConnectedComponents.cpp
├── WordBoggle.cpp
├── bfs.cpp
├── dfs.cpp
└── topologicalSort.cpp
├── greedy
├── ActivitySelection.cpp
├── GeekCollectsTheBall.cpp
├── MaximizeToys.cpp
├── MinimizeTheSumOfProduct.cpp
├── N_meeting_in_a_room.cpp
└── ShopInCandyStore.cpp
├── hashing
├── ArrayPairSumDivisibiltyProblem.cpp
├── CommonElements.cpp
├── CountDistinctElementInEveryWindow.cpp
├── FindAllSumFourNumber.cpp
├── LargestSubarrayWith0sum.cpp
├── LongestConsecutiveSubsequence.cpp
├── SortArrayAccordingToOther.cpp
├── SortingArrayOnFrequency.cpp
└── SwappingPairsMakeSumEqual.cpp
├── linked list
├── FlatteningLinkedList.cpp
├── IntersectionPointInY-ShapedLinkedList.cpp
├── LoopInLinkedList.cpp
├── NthNodeFromEndOfLinkedList.cpp
├── RemoveLoopInLinkedList.cpp
├── ReverseLinkedListInGivenGroup.cpp
├── finding_middle_element.cpp
├── reverse_linked_list.cpp
└── rotate_linked_list.cpp
├── recursion
├── DecodeString.cpp
├── floodFill.cpp
├── josephusProblem.cpp
├── numberOfPaths.cpp
└── specialKeyboard.cpp
├── stack and queue
├── DecodeString.cpp
├── GetMinElementFromStack.cpp
├── QueueUsingTwoStack.cpp
├── StackUsingTwoQueue.cpp
└── parenthesis_checker.cpp
├── string
├── Anagram.cpp
├── FormPalindrome.cpp
├── ImplementAtoi.cpp
├── ImplementStrstr.cpp
├── LongestCommonPrefix.cpp
├── LongestCommonPrefixInArray.cpp
├── LongestDistinctCharacter.cpp
├── PermutationOfString.cpp
├── RemoveDuplicates.cpp
├── ReverseWordInString.cpp
├── RomanToInteger.cpp
└── RotateStringByTwoPlace.cpp
└── tree and bst
├── BinaryTreeToDLL.cpp
├── BottomViewOfBinaryTree.cpp
├── ConnectNodesAtSameLevel.cpp
├── CountLeafNodes.cpp
├── HeightOfBinaryTree.cpp
├── LevelOrderTraversalInSpiralForm.cpp
├── LowestCommonAncestorInBinaryTree.cpp
├── MaximumSumBSTBinaryTree.cpp
├── MirrorTree.cpp
├── RecursivelyRemoveAllAdjacentDuplicates.cpp
├── SerializeAndDeserializeBinaryTree.cpp
├── SumTree.cpp
├── SymmetricTree.cpp
├── VerticalTraversalOfBinaryTree.cpp
├── check_for_bst.cpp
└── left_view_of_binary_tee.cpp
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.exe
2 | *.bin
3 | *.prob
--------------------------------------------------------------------------------
/Difficulty: Medium/Jump Game/README.md:
--------------------------------------------------------------------------------
1 |
Difficulty Level : Difficulty: Medium
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
2 |
3 |
4 | Example 1:
5 |
6 |
Input:
7 | N = 6
8 | A[] = {1, 2, 0, 3, 0, 0}
9 | Output:
10 | 1
11 | Explanation:
12 | Jump 1 step from first index to
13 | second index. Then jump 2 steps to reach
14 | 4th index, and now jump 2 steps to reach
15 | the end.
16 |
17 |
18 |
Example 2:
19 |
20 |
Input:
21 | N = 3
22 | A[] = {1, 0, 2}
23 | Output:
24 | 0
25 | Explanation:
26 | You can't reach the end of the array.
27 |
28 |
29 |
30 | Your Task:
31 | You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
32 |
33 |
34 | Expected Time Complexity: O(N)
35 | Expected Auxiliary Space: O(1)
36 |
37 |
38 | Constraints:
39 | 1 <= N <= 105
40 | 0 <= A[i] <= 105
41 |
Company Tags :
Amazon
Microsoft
Google
Facebook
Topic Tags :
Arrays
Dynamic Programming
Greedy
Data Structures
Algorithms
--------------------------------------------------------------------------------
/Difficulty: Medium/Jump Game/jump-game.java:
--------------------------------------------------------------------------------
1 | //{ Driver Code Starts
2 | import java.io.*;
3 | import java.util.*;
4 |
5 | class GFG {
6 | public static void main(String args[]) throws IOException {
7 | BufferedReader read =
8 | new BufferedReader(new InputStreamReader(System.in));
9 | int t = Integer.parseInt(read.readLine());
10 | while (t-- > 0) {
11 | int N = Integer.parseInt(read.readLine());
12 |
13 | String S1[] = read.readLine().split(" ");
14 |
15 | int[] A = new int[N];
16 |
17 | for(int i=0; i 0) {
13 | ArrayList> list = new ArrayList<>();
14 | String st[] = read.readLine().trim().split("\\s+");
15 | int edg = Integer.parseInt(st[0]);
16 | int nov = Integer.parseInt(st[1]);
17 |
18 | for (int i = 0; i < nov; i++)
19 | list.add(i, new ArrayList());
20 |
21 | int p = 0;
22 | for (int i = 1; i <= edg; i++) {
23 | String s[] = read.readLine().trim().split("\\s+");
24 | int u = Integer.parseInt(s[0]);
25 | int v = Integer.parseInt(s[1]);
26 | list.get(u).add(v);
27 | }
28 |
29 | int[] res = new Solution().topoSort(nov, list);
30 |
31 | if (check(list, nov, res) == true)
32 | System.out.println("1");
33 | else
34 | System.out.println("0");
35 | }
36 | }
37 | static boolean check(ArrayList> list, int V, int[] res) {
38 |
39 | if(V!=res.length)
40 | return false;
41 |
42 | int[] map = new int[V];
43 | for (int i = 0; i < V; i++) {
44 | map[res[i]] = i;
45 | }
46 | for (int i = 0; i < V; i++) {
47 | for (int v : list.get(i)) {
48 | if (map[i] > map[v]) return false;
49 | }
50 | }
51 | return true;
52 | }
53 | }
54 |
55 | // } Driver Code Ends
56 |
57 |
58 | /*Complete the function below*/
59 |
60 |
61 | class Solution
62 | {
63 | //Function to return list containing vertices in Topological order.
64 |
65 | public static void dfs(int i, ArrayList> adj, Stack ans, boolean[] vis){
66 | vis[i] = true;
67 |
68 | for(int j=0; j> adj)
78 | {
79 | // add your code here
80 | Stack st = new Stack();
81 |
82 | int n = adj.size();
83 | boolean[] vis = new boolean[V];
84 |
85 | for(int i=0; i < V; i++){
86 | if(!vis[i]){
87 | dfs(i, adj, st, vis);
88 | }
89 | }
90 |
91 | int[] ans = new int[n];
92 |
93 | for(int i=0; i
3 |
4 | using namespace std;
5 |
6 |
7 | // } Driver Code Ends
8 |
9 |
10 | //User function template for C++
11 | class Solution{
12 | public:
13 | // Program for zig-zag conversion of array
14 | void zigZag(int arr[], int n) {
15 | // code here
16 | for(int i=0; i arr[i+1]){
26 | continue;
27 | }else{
28 | swap(arr[i], arr[i+1]);
29 | }
30 | }
31 | }
32 | }
33 | };
34 |
35 | // { Driver Code Starts.
36 |
37 | int main() {
38 | int t;
39 | cin >> t;
40 | while (t--) {
41 | int n;
42 | cin >> n;
43 | int arr[n];
44 | for (int i = 0; i < n; i++) {
45 | cin >> arr[i];
46 | }
47 | Solution ob;
48 | ob.zigZag(arr, n);
49 | for (int i = 0; i < n; i++) {
50 | cout << arr[i] << " ";
51 | }
52 | cout << "\n";
53 | }
54 | return 0;
55 | }
56 | // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/ElementOnLeftSideIsSmallerAndRightIsGreater.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | int findElement(int arr[], int n);
6 |
7 | int main() {
8 | int t;
9 | cin >> t;
10 | while (t--) {
11 | int n, i;
12 | cin >> n;
13 | int a[n];
14 | for (i = 0; i < n; i++) cin >> a[i];
15 | cout << findElement(a, n) << endl;
16 | }
17 | return 0;
18 | }
19 | // } Driver Code Ends
20 |
21 |
22 | int findElement(int arr[], int n) {
23 | int min = arr[0];
24 | int max = arr[n-1];
25 | for(int i=1; i= arr[j]){
31 | j++;
32 | }else{
33 | flag = 1;
34 | break;
35 | }
36 | }
37 | if(flag == 0){
38 | while(k>i){
39 | if(arr[i] <= arr[k]){
40 | k--;
41 | }else{
42 | break;
43 | }
44 | }
45 | if(k == i){
46 | return arr[i];
47 | }
48 | }
49 | }
50 | return -1;
51 | }
--------------------------------------------------------------------------------
/arrays/EquilibriumPoint.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // Position this line where user code will be pasted.
6 |
7 | int equilibriumPoint(long long a[], int n);
8 |
9 | int main() {
10 |
11 | long long t;
12 |
13 | //taking testcases
14 | cin >> t;
15 |
16 | while (t--) {
17 | long long n;
18 |
19 | //taking input n
20 | cin >> n;
21 | long long a[n];
22 |
23 | //adding elements to the array
24 | for (long long i = 0; i < n; i++) {
25 | cin >> a[i];
26 | }
27 |
28 | //calling equilibriumPoint() function
29 | cout << equilibriumPoint(a, n) << endl;
30 | }
31 | return 0;
32 | }
33 | // } Driver Code Ends
34 |
35 |
36 | // Function to find equilibrium point
37 | // a: input array
38 | // n: size of array
39 | int equilibriumPoint(long long a[], int n) {
40 |
41 | // Your code here
42 | if(n == 1){
43 | return 1;
44 | }else if(n == 2){
45 | return -1;
46 | }
47 | int lsum[n];
48 | int sum = 0;
49 | for(int i=0; i
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 |
10 |
11 | class Solution{
12 | public:
13 | int lastIndex(string s)
14 | {
15 | for(int i=s.size()-1; i>=0; i--){
16 | if(s[i] == '1'){
17 | return i;
18 | }
19 | }
20 | return -1;
21 | }
22 |
23 | };
24 |
25 | // { Driver Code Starts.
26 |
27 | int main() {
28 | long long t;
29 | cin >> t;
30 | while (t--) {
31 | string s;
32 | cin >> s;
33 | Solution ob;
34 | cout << ob.lastIndex(s) << endl;
35 | }
36 | return 0;
37 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/MaximumIndex.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 | class Solution{
8 | public:
9 |
10 | // A[]: input array
11 | // N: size of array
12 | // Function to find the maximum index difference.
13 | int maxIndexDiff(int A[], int N)
14 | {
15 | // Your code here
16 | int max = 0;
17 | for(int i=0; i=i+max; j--){
19 | if(A[j] >= A[i]){
20 | if(j-i > max){
21 | max = j-i;
22 | }
23 | }
24 | }
25 |
26 | }
27 | return max;
28 | }
29 | };
30 |
31 | // { Driver Code Starts.
32 |
33 | /* Driver program to test above functions */
34 | int main()
35 | {
36 | int T;
37 | //testcases
38 | cin>>T;
39 | while(T--){
40 | int num;
41 | //size of array
42 | cin>>num;
43 | int arr[num];
44 |
45 | //inserting elements
46 | for (int i = 0; i>arr[i];
48 | Solution ob;
49 |
50 | //calling maxIndexDiff() function
51 | cout<
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 | class Solution
8 | {
9 | public:
10 | long long getMaxArea(long long arr[], int n)
11 | {
12 |
13 | stack st;
14 | vector left(n),right(n);
15 |
16 | for(int i=0;i=0;i--){
38 | while(!st.empty() && arr[i]<=arr[st.top()]){
39 | st.pop();
40 | }
41 |
42 | if(st.empty()){
43 | right[i]=n-1;
44 | }else{
45 | right[i]= st.top()-1;
46 | }
47 | st.push(i);
48 |
49 | }
50 |
51 |
52 | long long int res=0;
53 | for(int i=0;i>t;
71 | while(t--)
72 | {
73 | int n;
74 | cin>>n;
75 |
76 | long long arr[n];
77 | for(int i=0;i>arr[i];
79 | Solution ob;
80 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | // This function wants you to modify the given input
12 | // array and no need to return anything
13 | // arr: input array
14 | // n: size of array
15 | void rearrange(long long *arr, int n)
16 | {
17 |
18 | // Your code here
19 | int k=0;
20 | int j=n-1;
21 | int max = arr[n-1] + 1;
22 | int min = arr[0];
23 | for(int i=0; i> t;
46 |
47 | while(t--){
48 |
49 | //size of array
50 | int n;
51 | cin >> n;
52 |
53 | long long arr[n];
54 |
55 | //adding elements to the array
56 | for(int i = 0;i> arr[i];
58 | }
59 |
60 | //calling rearrange() function
61 | rearrange(arr, n);
62 |
63 | //printing the elements
64 | for (int i = 0; i < n; i++)
65 | cout << arr[i] << " ";
66 |
67 | cout << endl;
68 | }
69 | return 0;
70 | }
71 | // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/ReverseArrayInGroups.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function template for C++
12 |
13 | class Solution{
14 | public:
15 | void reverseInGroups(vector& arr, int n, int k){
16 | // code here
17 | int i;
18 | for(i=0; i < n-k ; i+=k){
19 | reverse(arr.begin()+i, arr.begin()+i+k);
20 | }
21 | reverse(arr.begin()+i , arr.begin()+n);
22 | }
23 | };
24 |
25 | // { Driver Code Starts.
26 | int main() {
27 | int t;
28 | cin >> t;
29 | while(t--){
30 | int n;
31 | cin >> n;
32 | vector arr;
33 | int k;
34 | cin >> k;
35 |
36 | for(long long i = 0; i> x;
40 | arr.push_back(x);
41 | }
42 | Solution ob;
43 | ob.reverseInGroups(arr, n, k);
44 |
45 | for(long long i = 0; i sequentialDigits(int low, int high) {
4 | vector v;
5 | int arr[36] = {12, 23, 34, 45, 56, 67, 78, 89,
6 | 123, 234, 345, 456, 567, 678, 789,
7 | 1234, 2345, 3456, 4567, 5678, 6789,
8 | 12345, 23456, 34567, 45678, 56789,
9 | 123456, 234567, 345678, 456789,
10 | 1234567, 2345678, 3456789,
11 | 12345678, 23456789,
12 | 123456789};
13 | for(int i=0; i<36; i++){
14 | if(arr[i] < low) continue;
15 | if(arr[i] > high) break;
16 | v.push_back(arr[i]);
17 | }
18 |
19 | // string digits = "123456789";
20 |
21 | // int nl = to_string(low).length();
22 | // int nh = to_string(high).length();
23 |
24 | // for(int i=nl; i<=nh; i++){
25 | // for(int j=0; j<10-i; j++){
26 | // int num = stoi(digits.substr(j, i));
27 | // if(num >= low && num <= high){
28 | // v.push_back(num);
29 | // }
30 | // }
31 | // }
32 | return v;
33 | }
34 | };
--------------------------------------------------------------------------------
/arrays/SmallestNumberOnLeft.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Initial Template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 | // User function Template for C++
9 |
10 | // class Solution{
11 | // public:
12 | // vector leftSmaller(int n, int a[]){
13 | // // code here
14 | // vector v;
15 | // for(int i=n-1; i>0; i--){
16 | // for(int j=i-1; j>=0; j--){
17 | // if(a[i] > a[j]){
18 | // v.push_back(a[j]);
19 | // break;
20 | // }
21 | // if(j==0 && a[j] >= a[i]){
22 | // v.push_back(-1);
23 | // }
24 | // }
25 | // }
26 | // v.push_back(-1);
27 | // reverse(v.begin(), v.end());
28 | // return v;
29 | // }
30 | // };
31 |
32 | class Solution{
33 | public:
34 | vector leftSmaller(int n, int a[]){
35 | // code here
36 | vector v;
37 | stack s;
38 | for(int i=0; i= a[i]){
40 | s.pop();
41 | }
42 |
43 | if(s.empty()){
44 | v.push_back(-1);
45 | }else{
46 | int a = s.top();
47 | v.push_back(a);
48 | }
49 |
50 | s.push(a[i]);
51 | }
52 | return v;
53 | }
54 | };
55 |
56 | // { Driver Code Starts.
57 |
58 | int main(){
59 | int t;
60 | cin>>t;
61 | while(t--){
62 | int n;
63 | cin>>n;
64 | int a[n];
65 | for(int i = 0;i < n;i++)
66 | cin>>a[i];
67 |
68 | Solution ob;
69 | vector ans = ob.leftSmaller(n, a);
70 | for(int i = 0;i < n;i++)
71 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function template for C++
12 |
13 | class Solution{
14 | public:
15 | vector > stockBuySell(vector A, int n){
16 | // code here
17 | int max = 0;
18 | int left = 0;
19 | int right = 0;
20 | vector> v;
21 | vector ans;
22 | for(int i=0; i A[i]){
24 | ans.push_back(i);
25 | while(A[i+1] >= A[i] && i>t;
44 | while(t--){
45 | int n;
46 | cin>>n;
47 | vector A(n);
48 | for (int i=0; i>A[i];
50 | }
51 | Solution ob;
52 | vector > ans = ob.stockBuySell(A, n);
53 | if(ans.size()==0)
54 | cout<<"No Profit";
55 | else{
56 | for (int i=0; i
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | // Function to find the subarray with given sum k
10 | // arr: input array
11 | // n: size of array
12 | vector subarraySum(int arr[], int n, int s){
13 |
14 | // Your code here
15 | int left = 0, right = 0, sum = 0;
16 | vector ans;
17 | ans.push_back(-1);
18 | for(int i=0; i s){
26 | sum -= arr[left++] + arr[i--];
27 | }
28 | }
29 | return ans;
30 | }
31 |
32 | // { Driver Code Starts.
33 |
34 | int main()
35 | {
36 | int t;
37 | cin>>t;
38 | while(t--)
39 | {
40 | int n;
41 | long long s;
42 | cin>>n>>s;
43 | int arr[n];
44 |
45 | for(int i=0;i>arr[i];
47 |
48 | vectorres;
49 | res = subarraySum(arr, n, s);
50 |
51 | for(int i = 0;i
3 |
4 | using namespace std;
5 |
6 |
7 | // } Driver Code Ends
8 |
9 |
10 |
11 |
12 | // function to find the trapped water in between buildings
13 | // arr: input array
14 | // n: size of array
15 | int trappingWater(int arr[], int n){
16 |
17 | // Your code here
18 | if(n < 3){
19 | return 0;
20 | }
21 | int r = n-1;
22 | int l = 0;
23 | int lmax = 0;
24 | int rmax = 0;
25 | int ans = 0;
26 | while(l <= r){
27 | if(arr[l] <= arr[r]){
28 | if(lmax < arr[l]) lmax = arr[l];
29 | else ans+= lmax - arr[l];
30 | l++;
31 | }else{
32 | if(rmax < arr[r]) rmax = arr[r];
33 | else ans+= rmax - arr[r];
34 | r--;
35 | }
36 | }
37 | return ans;
38 | }
39 |
40 | // { Driver Code Starts.
41 |
42 | int main(){
43 |
44 | int t;
45 | //testcases
46 | cin >> t;
47 |
48 | while(t--){
49 | int n;
50 |
51 | //size of array
52 | cin >> n;
53 |
54 | int a[n];
55 |
56 | //adding elements to the array
57 | for(int i =0;i> a[i];
59 | }
60 |
61 | //calling trappingWater() function
62 | cout << trappingWater(a, n) << endl;
63 |
64 | }
65 |
66 | return 0;
67 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/chocolate_distribution_problem.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int distribute(int arr[], int k, int n){
5 | sort(arr, arr+n);
6 | int min = INT_MAX;
7 | for(int i=0; i> t;
23 | while(t--){
24 | int n;
25 | cin >> n;
26 | int arr[n];
27 | for(int i=0; i> arr[i];
29 | }
30 | int k;
31 | cin >> k;
32 | cout << distribute(arr, k, n) << endl;
33 | }
34 | return 0;
35 | }
--------------------------------------------------------------------------------
/arrays/closest_sum_to_zero.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 |
10 | class Solution
11 | {
12 | public:
13 | int closestToZero(int arr[], int n)
14 | {
15 | // your code here
16 | sort (arr, arr + n); // sorting the array
17 | int i = 0, j = n - 1;
18 | int sum = arr[i] + arr[j]; // initializing sum
19 | int diff = abs (sum); // initializing the result
20 |
21 | while (i < j)
22 | {
23 | // if we have zero sum, there's no result better. Hence, we return
24 | if (arr[i] + arr[j] == 0)
25 | return 0;
26 |
27 | // if we get a better result, we update the difference
28 | if (abs (arr[i] + arr[j]) < abs (diff))
29 | {
30 | diff = (arr[i] + arr[j]);
31 | sum = arr[i] + arr[j];
32 | }
33 |
34 | // if the current sum is greater than zero, we search for a smaller sum
35 | if (arr[i] + arr[j] > 0)
36 | j--;
37 | // else, we search for a larger sum
38 | else
39 | i++;
40 | }
41 | return sum;
42 | }
43 | };
44 |
45 | // { Driver Code Starts.
46 | int main ()
47 | {
48 | int t;
49 | cin >> t;
50 | while (t--)
51 | {
52 | int n;
53 | cin >> n;
54 | int arr[n];
55 | for (int i = 0; i < n; i++)
56 | cin >> arr[i];
57 | Solution ob;
58 | cout << ob.closestToZero(arr, n) << endl;
59 | }
60 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/count_possible_triangles.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | class Solution
10 | {
11 | public:
12 | int findNumberOfTriangles(int arr[], int n)
13 | {
14 | // code here
15 | sort(arr, arr+n);
16 |
17 | int count = 0;
18 |
19 | for(int c = n-1; c >= 2; c--)
20 | {
21 | int a = 0, b = c - 1;
22 |
23 | while(a < b)
24 | {
25 | if(arr[a] + arr[b] > arr[c])
26 | {
27 | count += b - a;
28 |
29 | b--;
30 | }
31 | else
32 | {
33 | a++;
34 | }
35 | }
36 | }
37 | return count;
38 | }
39 | };
40 |
41 |
42 | // { Driver Code Starts.
43 |
44 | int main()
45 | {
46 | int T;
47 | cin>>T;
48 | while(T--)
49 | {
50 | int n;
51 | cin>>n;
52 | int arr[n];
53 | for(int i=0; i>arr[i];
55 | Solution ob;
56 | cout<
2 | using namespace std;
3 |
4 | int main(){
5 | int t, n, i;
6 | cin >> t;
7 | while(t--){
8 | cin >> n;
9 | int arr[n], ans = 0;
10 | for(i=0; i> arr[i];
12 | };
13 |
14 | sort(arr, arr+n);
15 |
16 | for(i=n-1; i>=0; i--){
17 | int j = 0;
18 | int k = i-1;
19 | while(k > j){
20 | if(arr[i] == arr[j] + arr[k]){
21 | ans ++;
22 | j++;
23 | k--;
24 | }else if(arr[i] > arr[j] + arr[k]){
25 | j++;
26 | }else{
27 | k--;
28 | }
29 | }
30 | }
31 |
32 | cout << ans << endl;
33 | }
34 |
35 | return 0;
36 | }
--------------------------------------------------------------------------------
/arrays/equilibrium_point.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | //
6 | int t;
7 | cin >> t;
8 | while(t--){
9 | int n;
10 | cin >> n;
11 | int arr[n], sum[n], s = 0;
12 | for(int i=0; i> arr[i];
14 | }
15 | for(int i=0; i 1){
22 | for(int i=0; i 0 && i < n-1){
24 | aft = sum[n-1] - sum[i];
25 | bef = sum[i-1];
26 | if(aft == bef && aft > 0){
27 | ans = i + 1;
28 | break;
29 | }
30 | }else if(i == 0){
31 | aft = sum[n-1];
32 | if(aft == bef && aft > 0){
33 | ans = i + 1;
34 | break;
35 | }
36 | }else if(i == n-1){
37 | bef = sum[i-1];
38 | if(aft == bef && aft > 0){
39 | ans = i + 1;
40 | break;
41 | }
42 | }
43 | aft = 0, bef = 0;
44 | }
45 | }else if(n == 1){
46 | ans = 1;
47 | }
48 | cout << ans << endl;
49 | }
50 |
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/arrays/inversion_of_array.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #define ll long long
3 | using namespace std;
4 |
5 | ll query(ll bit[],ll pos){
6 | ll sum=0;
7 | while(pos>0){
8 | sum+=bit[pos];
9 | pos-=pos&(-pos);
10 | }
11 | return sum;
12 | }
13 |
14 | void update(ll bit[],ll n,ll pos){
15 | while(pos<=n){
16 | bit[pos]+=1;
17 | pos+=pos&(-pos);
18 | }
19 | }
20 |
21 | int main()
22 | {
23 | ll t;
24 | cin>>t;
25 | while(t--){
26 | ll n,i,summ=0;
27 | cin>>n;
28 | ll a[n],temp[n],bit[n+1]={0};
29 | for(i=0;i>a[i];
31 | temp[i]=a[i];
32 | }
33 | sort(temp,temp+n);
34 | for(i=0;i=0;i--){
38 | summ+=query(bit,a[i]-1);
39 | update(bit,n,a[i]);
40 | }
41 | cout<
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | // Function to find subarray with maximum sum
10 | // arr: input array
11 | // n: size of array
12 | int maxSubarraySum(int arr[], int n){
13 |
14 | // Your code here
15 | int sum=0, ans = 0, j = 0;
16 | for(int i=0; i>t; //input testcases
35 | while(t--) //while testcases exist
36 | {
37 |
38 | cin>>n; //input size of array
39 |
40 | int a[n];
41 |
42 | for(int i=0;i>a[i]; //inputting elements of array
44 |
45 | cout << maxSubarraySum(a, n) << endl;
46 | }
47 | }
48 | // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/kth_smallest_element.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial function template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | int kthSmallest(int *, int, int, int);
8 |
9 | int main()
10 | {
11 | // ios_base::sync_with_stdio(false);
12 | // cin.tie(NULL);
13 |
14 | int test_case;
15 | cin>>test_case;
16 | while(test_case--)
17 | {
18 | int number_of_elements;
19 | cin>>number_of_elements;
20 | int a[number_of_elements];
21 |
22 | for(int i=0;i>a[i];
24 |
25 | int k;
26 | cin>>k;
27 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 |
12 |
13 | vector leaders(int a[], int n){
14 | // code here
15 | int max = INT_MIN;
16 | vector ans;
17 | for(int i=n-1; i>=0; i--){
18 | if(max <= a[i]){
19 | max = a[i];
20 | ans.push_back(max);
21 | }
22 | }
23 | reverse(ans.begin(), ans.end());
24 | return ans;
25 | }
26 |
27 | // { Driver Code Starts.
28 |
29 | int main()
30 | {
31 | long long t;
32 | cin >> t;//testcases
33 | while (t--)
34 | {
35 | long long n;
36 | cin >> n;//total size of array
37 |
38 | int a[n];
39 |
40 | //inserting elements in the array
41 | for(long long i =0;i> a[i];
43 | }
44 |
45 | //calling leaders() function
46 | vector v = leaders(a, n);
47 |
48 | //printing elements of the vector
49 | for(auto it = v.begin();it!=v.end();it++){
50 | cout << *it << " ";
51 | }
52 |
53 | cout << endl;
54 |
55 | }
56 | }
57 | // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/merge_without_extra_space.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | //User function template for C++
9 | class Solution{
10 | public:
11 | void merge(int a[], int b[], int n, int m) {
12 | // code here
13 | int c=n,sr=0,i=0,j=0;
14 | while(c-- && i=b[j])
19 | {
20 | sr++;
21 | j++;
22 | }
23 |
24 | }
25 | for(i=0;i> t;
36 | while (t--) {
37 | int n, m, i;
38 | cin >> n >> m;
39 | int arr1[n], arr2[m];
40 | for (i = 0; i < n; i++) {
41 | cin >> arr1[i];
42 | }
43 | for (i = 0; i < m; i++) {
44 | cin >> arr2[i];
45 | }
46 | Solution ob;
47 | ob.merge(arr1, arr2, n, m);
48 | for (i = 0; i < n; i++) {
49 | cout << arr1[i] << " ";
50 | }
51 | for (i = 0; i < m; i++) {
52 | cout << arr2[i] << " ";
53 | }
54 | cout << "\n";
55 | }
56 | return 0;
57 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/arrays/minimum_platforms.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Program to find minimum number of platforms
3 | // required on a railway station
4 | #include
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | int findPlatform(int arr[], int dep[], int n)
12 | {
13 | // Your code here
14 | sort(arr, arr+n);
15 | sort(dep, dep+n);
16 | int i = 1;
17 | int j = 0;
18 | int p = 1;
19 | int max = 1;
20 | while(i dep[j]){
22 | p--;
23 | j++;
24 | }else if(arr[i] <= dep[j]){
25 | p++;
26 | i++;
27 | if(p > max){
28 | max = p;
29 | }
30 | }
31 | }
32 | return max;
33 | }
34 |
35 |
36 | // { Driver Code Starts.
37 | // Driver code
38 | int main()
39 | {
40 | int t;
41 | cin>>t;
42 | while(t--)
43 | {
44 | int n;
45 | cin>>n;
46 | int arr[n];
47 | int dep[n];
48 | for(int i=0;i>arr[i];
50 | for(int j=0;j>dep[j];
52 | }
53 | cout <
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | void merge(int arr1[], int arr2[], int n, int m)
10 | {
11 | int x=n-1, y = 0;
12 | while(x>=0 && y arr2[y]){
14 | swap(arr1[x], arr2[y]);
15 | x--;
16 | y++;
17 | }else{
18 | break;
19 | }
20 | }
21 | sort(arr1 , arr1+n);
22 | sort(arr2 , arr2+m);
23 | }
24 |
25 |
26 | // { Driver Code Starts.
27 |
28 | int main()
29 | {
30 |
31 | int T;
32 | cin >> T;
33 |
34 | while(T--){
35 | int n, m;
36 | cin >> n >> m;
37 |
38 | int arr1[n], arr2[m];
39 |
40 | for(int i = 0;i> arr1[i];
42 | }
43 |
44 | for(int i = 0;i> arr2[i];
46 | }
47 |
48 | merge(arr1, arr2, n, m);
49 |
50 | for (int i = 0; i < n; i++)
51 | printf("%d ", arr1[i]);
52 |
53 |
54 | for (int i = 0; i < m; i++)
55 | printf("%d ", arr2[i]);
56 |
57 | cout<
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 |
10 |
11 | // Function to count number of pairs such that x^y is greater than y^x
12 | // X[], Y[]: input arrau
13 | // m, n: size of arrays X[] and Y[] respectively
14 | int getIndex(int Y[] , int n , int ele){
15 |
16 | int low = 0;
17 | int high = n-1;
18 | int ans = -1;
19 | while(low<=high){
20 | int mid = (low+high)/2;
21 | if(Y[mid] > ele){
22 | ans = mid;
23 | high = mid -1;
24 | }else{
25 | low = mid+1;
26 | }
27 | }
28 | return ans;
29 | }
30 |
31 | long long countPairs(int X[], int Y[], int m, int n)
32 | {
33 | //Your code here
34 | int zero = 0, one = 0, three = 0, four = 0, two= 0;
35 | sort(X, X+m);
36 | sort(Y, Y+n);
37 |
38 | for(int i=0;i>T;
84 | while(T--)
85 | {
86 | int M,N;
87 | cin>>M>>N;
88 | int i,a[M],b[N];
89 | for(i=0;i>a[i];
92 | }
93 | for(i=0;i>b[i];
96 | }
97 | cout<
2 | using namespace std;
3 | int main()
4 | {
5 | //code
6 | int t;
7 | cin >> t;
8 | int n;
9 | while(t--){
10 | cin >> n;
11 | int arr[n];
12 | for(int i=0; i> arr[i];
14 | }
15 |
16 | int size = sizeof(arr)/sizeof(arr[0]);
17 |
18 | sort(arr, arr+size);
19 |
20 | if(n%2 == 0){
21 | for(int i=0; i
3 | using namespace std;
4 | void sort012(int[],int);
5 |
6 | int main() {
7 |
8 | int t;
9 | cin >> t;
10 |
11 | while(t--){
12 | int n;
13 | cin >>n;
14 | int a[n];
15 | for(int i=0;i> a[i];
17 | }
18 |
19 | sort012(a, n);
20 |
21 | for(int i=0;i m;
39 | for(int i=0; i i){
47 | a[i] = 0;
48 | }else if(one > i){
49 | a[i] = 1;
50 | }else if(two > i){
51 | a[i] = 2;
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/backtracking/N_queenProblem.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Initial Template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 | // User function Template for C++
9 |
10 | class Solution{
11 | unordered_map diag1, diag2;
12 | public:
13 |
14 | void solve(vector> &ans, int i, int n, vector cols, vector vec){
15 | if(i==n){
16 | ans.push_back(vec);
17 | return;
18 | }
19 |
20 | for(int j=0; j> nQueen(int n) {
38 | vector cols(n,false);
39 | vector> ans;
40 | solve(ans, 0, n, cols, {});
41 | return ans;
42 | }
43 | };
44 |
45 | // { Driver Code Starts.
46 |
47 | int main(){
48 | int t;
49 | cin>>t;
50 | while(t--){
51 | int n;
52 | cin>>n;
53 |
54 | Solution ob;
55 | vector> ans = ob.nQueen(n);
56 | if(ans.size() == 0)
57 | cout<<-1<<"\n";
58 | else {
59 | for(int i = 0;i < ans.size();i++){
60 | cout<<"[";
61 | for(int u: ans[i])
62 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 | // User function template for C++
10 |
11 | class Solution{
12 | public:
13 | void solve(vector> &m, int i, int j, int n, vector &v, string s){
14 | if(i>n-1 || j>n-1 || i<0 || j<0 || m[i][j] == 0){
15 | return;
16 | }
17 | if(i==n-1 && j==n-1){
18 | v.push_back(s);
19 | }
20 | m[i][j] = 0;
21 | solve(m, i+1, j, n, v, s+"D");
22 | solve(m, i, j+1, n, v, s+"R");
23 | solve(m, i-1, j, n, v, s+"U");
24 | solve(m, i, j-1, n, v, s+"L");
25 | m[i][j] = 1;
26 | }
27 |
28 | vector findPath(vector> &m, int n) {
29 | // Your code goes here
30 | vector v;
31 | string s;
32 | solve(m, 0, 0, n, v, s);
33 | sort(v.begin(), v.end());
34 | return v;
35 | }
36 | };
37 |
38 |
39 |
40 |
41 | // { Driver Code Starts.
42 |
43 | int main() {
44 | int t;
45 | cin >> t;
46 | while (t--) {
47 | int n;
48 | cin >> n;
49 | vector> m(n, vector (n,0));
50 | for (int i = 0; i < n; i++) {
51 | for (int j = 0; j < n; j++) {
52 | cin >> m[i][j];
53 | }
54 | }
55 | Solution obj;
56 | vector result = obj.findPath(m, n);
57 | if (result.size() == 0)
58 | cout << -1;
59 | else
60 | for (int i = 0; i < result.size(); i++) cout << result[i] << " ";
61 | cout << endl;
62 | }
63 | return 0;
64 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/backtracking/SolveTheSuduko.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 | // UNASSIGNED is used for empty cells in sudoku grid
5 | #define UNASSIGNED 0
6 |
7 | // N is used for the size of Sudoku grid.
8 | // Size will be NxN
9 | #define N 9
10 |
11 |
12 | // } Driver Code Ends
13 |
14 |
15 | class Solution {
16 | public:
17 | bool isSafe(int grid[N][N], int i, int j, int val){
18 | for(int k=0; k>t;
83 | while(t--)
84 | {
85 | int grid[N][N];
86 |
87 | for(int i=0;i<9;i++)
88 | for(int j=0;j<9;j++)
89 | cin>>grid[i][j];
90 |
91 | Solution ob;
92 |
93 | if (ob.SolveSudoku(grid) == true)
94 | ob.printGrid(grid);
95 | else
96 | cout << "No solution exists";
97 |
98 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function Template for C++
12 |
13 | // Function to find number of bits to be flip
14 | // to convert A to B
15 | int countBitsFlip(int a, int b){
16 |
17 | // Your logic here
18 | int x = a^b;
19 | int ans=0;
20 | while(x){
21 | ans += x&1;
22 | x >>= 1;
23 | }
24 | return ans;
25 | }
26 |
27 | // { Driver Code Starts.
28 |
29 | // Driver Code
30 | int main()
31 | {
32 | int t;
33 | cin>>t;// input the testcases
34 | while(t--) //while testcases exist
35 | {
36 | int a,b;
37 | cin>>a>>b; //input a and b
38 |
39 | cout<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function Template for C++
12 |
13 | // Function to check if Kth bit is set or not
14 | bool checkKthBit(int n, int k){
15 |
16 | // Your code here
17 | // It can be a one liner logic!! Think of it!!
18 | if(n & (1 << k)){
19 | return true;
20 | }else{
21 | return false;
22 | }
23 |
24 | }
25 |
26 | // { Driver Code Starts.
27 |
28 | // Driver Code
29 | int main()
30 | {
31 | int t;
32 | cin>>t;//taking testcases
33 | while(t--)
34 | {
35 | long long n;
36 | cin>>n;//input n
37 | int k;
38 | cin>>k;//bit number k
39 |
40 | if(checkKthBit(n, k))
41 | cout << "Yes" << endl;
42 | else
43 | cout << "No" << endl;
44 | }
45 | return 0;
46 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/bit magic/first_set_bit.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial Template for C++
3 |
4 |
5 | #include
6 | using namespace std;
7 |
8 |
9 | // } Driver Code Ends
10 |
11 | //User function Template for C++
12 |
13 | /* function to find position of first set
14 | bit in the given number
15 | * n: given input for which we want to get
16 | the position of first set bit
17 | */
18 | unsigned int getFirstSetBit(int n){
19 |
20 | // Your code here
21 | int ans;
22 | if((n & 1) != 0){
23 | ans = 1;
24 | }else{
25 | for(int i=1; i<20; i++){
26 | if((n&(1<>t; // testcases
42 | while(t--)
43 | {
44 | int n;
45 | cin>>n; //input n
46 | printf("%u\n", getFirstSetBit(n)); // function to get answer
47 | }
48 | return 0;
49 | }
50 | // } Driver Code Ends
--------------------------------------------------------------------------------
/bit magic/maximum_subset_xor.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // Number of bits to represent int
6 | #define INT_BITS 32
7 | int maxSubarrayXOR(int [], int n);
8 | int main()
9 | {
10 |
11 | int t,n,a[100004],k;
12 | scanf("%d",&t);
13 | while(t--)
14 | {
15 | //cin>>n;
16 | scanf("%d",&n);
17 |
18 | for(int i=0;i
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 |
9 |
10 | //User function Template for C++
11 |
12 | class Solution{
13 | public:
14 | int findSingle(int N, int arr[]){
15 | // code here
16 | sort(arr, arr+N);
17 | int ans;
18 | for(int i=0; i>t;
38 | while(t--)
39 | {
40 | int N, X;
41 | cin >> N;
42 | int arr[N];
43 | for(int i = 0; i < N; i++){
44 | cin >> arr[i];
45 | }
46 |
47 | Solution ob;
48 | cout << ob.findSingle(N, arr) << endl;
49 | }
50 | return 0;
51 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/bit magic/powerOf2.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial Template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function Template for C++
12 |
13 | // Function to check power of two
14 | bool isPowerofTwo(long long n){
15 |
16 | // Your code here
17 | if((n & n - 1) == 0){
18 | return true;
19 | }else {
20 | return false;
21 | }
22 |
23 | }
24 |
25 | // { Driver Code Starts.
26 |
27 | // Driver code
28 | int main()
29 | {
30 |
31 | int t;
32 | cin>>t;//testcases
33 |
34 | for(int i=0;i>n;
38 | if(n<=0) // if n is less than equal to zero then it can't be a power of 2
39 | {
40 | cout<<"NO"<
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 |
11 | //User function Template for C++
12 |
13 | /* Function to find the first position with different bits
14 | * This function returns the position with different bit
15 | */
16 | int posOfRightMostDiffBit(int m, int n)
17 | {
18 | // Your code here
19 | int ans, x;
20 | ans = (m ^ n);
21 | if(ans == 0){
22 | x = -1;
23 | }else{
24 | for(int i=1; i<11; i++){
25 | if(ans&1 == true){
26 | x = 1;
27 | break;
28 | }else if((ans&(1<>t; //input number of testcases
44 | while(t--)
45 | {
46 | int m,n;
47 | cin>>m>>n; //input m and n
48 | cout << posOfRightMostDiffBit(m, n)<
2 | using namespace std;
3 | # define ll long long int
4 | int main()
5 | {
6 | ll t;
7 | t=1;
8 | cin>>t;
9 | while(t--)
10 | {
11 | ll n,d,i,ans=0,t;
12 | cin>>n>>d;
13 | t=n;
14 | int x[16]={0},m=0;
15 | while(n>0)
16 | {
17 | x[m++] = n%2;
18 | n/=2;
19 | }
20 | d%=16;
21 | int arr[16]={0},k=0;
22 | for(i=d-1;i>=0;i--)
23 | arr[k++] = x[i];
24 | for(i=15;i>=d;i--)
25 | arr[k++] = x[i];
26 | reverse(arr,arr+16);
27 | for(i=0;i<16;i++)
28 | {
29 | if(arr[i])
30 | ans += (ll)pow(2,i);
31 | }
32 | cout<<(t<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 | class Solution
8 | {
9 | public:
10 | int setKthBit(int N, int K)
11 | {
12 | // Write Your Code here
13 | int ans = N | (1<> t;
23 | while (t--)
24 | {
25 | int N, K;
26 | cin >> N >> K;
27 |
28 | Solution ob;
29 | int ans = ob.setKthBit(N,K);
30 | cout << ans << endl;
31 | }
32 | return 0;
33 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/bit magic/toggle_bits_in_range.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution {
9 | public:
10 | int toggleBits(int N , int L , int R) {
11 | // code here
12 | for(int i=L; i<=R; i++){
13 | N = (N^(1 << i-1));
14 | }
15 | return N;
16 | }
17 | };
18 |
19 | // { Driver Code Starts.
20 | int main() {
21 | int t;
22 | cin >> t;
23 | while (t--) {
24 | int N,L,R;
25 |
26 | cin>>N>>L>>R;
27 |
28 | Solution ob;
29 | cout << ob.toggleBits(N,L,R) << endl;
30 | }
31 | return 0;
32 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/divide and conquer/BinarySearch.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | int bin_search(int A[],int left,int right,int k);
6 |
7 | int main()
8 | {
9 | int t;
10 | cin>>t;
11 |
12 | while(t--)
13 | {
14 | int N;
15 | cin>>N;
16 | int a[N];
17 | for(int i=0;i>a[i];
19 | int key;
20 | cin>>key;
21 | int found = bin_search(a,0,N-1,key);
22 | cout<= left){
32 | int mid = (left+right)/2;
33 |
34 | if(A[mid] == k){
35 | return mid;
36 | }else if(A[mid] > k){
37 | return bin_search(A, left, mid-1, k);
38 | }else if(A[mid] < k){
39 | return bin_search(A, mid+1, right, k);
40 | }
41 | }
42 | return -1;
43 | }
--------------------------------------------------------------------------------
/divide and conquer/KthElementOfTwoSortedArrays.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 | class Solution{
8 | public:
9 | int kthElement(int arr1[], int arr2[], int n, int m, int k)
10 | {
11 | int a[n+m];
12 |
13 | merge(arr1, arr1+n, arr2, arr2+m, a);
14 |
15 | return a[k-1];
16 | }
17 | };
18 |
19 | // { Driver Code Starts.
20 |
21 | // Driver code
22 | int main()
23 | {
24 | int t;
25 | cin>>t;
26 | while(t--){
27 | int n,m,k;
28 | cin>>n>>m>>k;
29 | int arr1[n],arr2[m];
30 | for(int i=0;i>arr1[i];
32 | for(int i=0;i>arr2[i];
34 |
35 | Solution ob;
36 | cout << ob.kthElement(arr1, arr2, n, m, k)<
3 | #include
4 | using namespace std;
5 |
6 |
7 |
8 | /* Function to print an array */
9 | void printArray(int arr[], int size)
10 | {
11 | int i;
12 | for (i=0; i < size; i++)
13 | printf("%d ", arr[i]);
14 | printf("\n");
15 | }
16 |
17 |
18 | // } Driver Code Ends
19 | class Solution
20 | {
21 | public:
22 | void merge(int arr[], int l, int m, int r)
23 | {
24 | // Your code here
25 | int n1, n2;
26 | n1 = m-l+1;
27 | n2 = r-m;
28 | int a[n1], b[n2];
29 | for(int i=0; i
3 | using namespace std;
4 | int search(int A[], int l, int h, int key);
5 |
6 |
7 | int main() {
8 | int t;
9 | cin >> t;
10 | while(t--){
11 | int n;
12 | cin >> n;
13 | int A[n];
14 | for(int i = 0; i < n; i++)
15 | cin >> A[i];
16 | int key;
17 | cin >> key;
18 | cout << search(A, 0, n - 1, key) << endl;
19 | }
20 | return 0;
21 | }// } Driver Code Ends
22 |
23 |
24 | int search(int A[], int l, int h, int key){
25 | //complete the function here
26 | for(int i=l; i<=h; i++){
27 | if(A[i] == key){
28 | return i;
29 | }
30 | }
31 | return -1;
32 | }
--------------------------------------------------------------------------------
/divide and conquer/SumOfMiddleElementsOfTwoSortedArray.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial template for C++
3 |
4 | #include
5 | #include
6 | using namespace std;
7 |
8 | // } Driver Code Ends
9 | //User function template for C++
10 |
11 | class Solution {
12 | public:
13 | int findMidSum(int ar1[], int ar2[], int n) {
14 | // code here
15 | int i=0, j=n-1;
16 | while(ar2[i] < ar1[j]){
17 | swap(ar2[i], ar1[j]);
18 | i++;
19 | j--;
20 | }
21 | sort(ar1, ar1+n);
22 | sort(ar2, ar2+n);
23 | return ar2[0] + ar1[n-1];
24 | }
25 | };
26 |
27 | // { Driver Code Starts.
28 | int main() {
29 | int t;
30 | cin >> t;
31 | while (t--) {
32 | int n, i;
33 | cin >> n;
34 | int ar1[n], ar2[n];
35 | for (i = 0; i < n; i++) {
36 | cin >> ar1[i];
37 | }
38 | for (i = 0; i < n; i++) {
39 | cin >> ar2[i];
40 | }
41 | Solution ob;
42 | auto ans = ob.findMidSum(ar1, ar2, n);
43 | cout << ans << "\n";
44 | }
45 | return 0;
46 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/divide and conquer/element_appeared_once_in_sorted_array.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Driver code
3 |
4 | #include
5 | using namespace std;
6 |
7 |
8 | // } Driver Code Ends
9 |
10 | class Solution
11 | {
12 | public:
13 | int findOnce(int arr[], int n)
14 | {
15 | //code here.
16 | for(int i = 0; i> t;
32 |
33 | while (t--)
34 | {
35 | int n;
36 | cin >> n;
37 | int A[n];
38 | for(int i = 0;i < n;i++)
39 | {
40 | cin>>A[i];
41 | }
42 |
43 | Solution ob;
44 |
45 | int res = ob.findOnce(A,n);
46 | cout << res << endl;
47 | }
48 |
49 | return 0;
50 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/divide and conquer/search_in_rotated_array.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | //code
6 | int t;
7 | cin >> t;
8 | while(t--){
9 | int n;
10 | cin >> n;
11 | int arr[n], k;
12 | for(int i=0; i> arr[i];
14 | }
15 | int ans = -1;
16 | cin >> k;
17 | for(int i=0; i
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | // Returns the maximum value that
10 | // can be put in a knapsack of capacity W
11 | int knapSack(int W, int wt[], int val[], int n)
12 | {
13 | // Your code here
14 | int dp[n+1][W+1];
15 | for(int i=0; i<=n; i++){
16 | dp[i][0] = 0;
17 | }
18 | for(int j=0; j<=W; j++){
19 | dp[0][j] = 0;
20 | }
21 | for(int i=1; i<=n; i++){
22 | for(int j=1; j<=W; j++){
23 | if(wt[i-1] > j){
24 | dp[i][j] = dp[i-1][j];
25 | }else{
26 | dp[i][j] = max(val[i-1] + dp[i-1][j-wt[i-1]], dp[i-1][j]);
27 | }
28 | }
29 | }
30 | return dp[n][W];
31 | }
32 |
33 |
34 |
35 |
36 |
37 | // { Driver Code Starts.
38 |
39 | int main()
40 | {
41 | //taking total testcases
42 | int t;
43 | cin>>t;
44 | while(t--)
45 | {
46 | //reading number of elements and weight
47 | int n, w;
48 | cin>>n>>w;
49 |
50 | int val[n];
51 | int wt[n];
52 |
53 | //inserting the values
54 | for(int i=0;i>val[i];
56 |
57 | //inserting the weights
58 | for(int i=0;i>wt[i];
60 |
61 | //calling method knapSack()
62 | cout<
3 |
4 | using namespace std;
5 |
6 | int maxHeight(int height[],int width[],int length[],int n);
7 |
8 | int main()
9 | {
10 | int t;
11 | cin>>t;
12 | while(t--){
13 | int n;
14 | cin>>n;
15 |
16 |
17 | int A[1000],B[1000],C[10001];
18 | for(int i=0;i>a>>b>>c;
22 | A[i]=a;
23 | B[i]=b;
24 | C[i]=c;
25 | }
26 | cout<, pair>> v;
40 |
41 | for(int i=0; i dp(m);
51 | for(int i=0; i
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | /* Function to get minimum number of trials needed in worst
10 | case with n eggs and k floors */
11 | int eggDrop(int n, int k){
12 | vector dp(n + 1, 0);
13 | int i;
14 | for (i = 0; dp[n] < k; i++){
15 | for (int k = n; k > 0; --k){
16 | dp[k] += dp[k - 1] + 1;
17 | }
18 | }
19 | return i;
20 | }
21 |
22 | // { Driver Code Starts.
23 |
24 | int main()
25 | {
26 | //taking total testcases
27 | int t;
28 | cin>>t;
29 | while(t--)
30 | {
31 | //taking eggs and floors count
32 | int n, k;
33 | cin>>n>>k;
34 |
35 | //calling function eggDrop()
36 | cout<
3 | const int mod=1e9+7;
4 | using namespace std;
5 |
6 | int lcs(int, int, string, string);
7 |
8 | int main()
9 | {
10 | int t,n,k,x,y;
11 |
12 | cin>>t;
13 | while(t--)
14 | {
15 | cin>>x>>y; // Take size of both the strings as input
16 | string s1,s2;
17 | cin>>s1>>s2; // Take both the string as input
18 |
19 | cout << lcs(x, y, s1, s2) << endl;
20 | }
21 | return 0;
22 | }
23 | // } Driver Code Ends
24 |
25 |
26 | // function to find longest common subsequence
27 |
28 | int lcs(int x, int y, string s1, string s2){
29 |
30 | // your code here
31 | int dp[x+1][y+1];
32 | for(int i=0; i<=x; i++){
33 | dp[i][0] = 0;
34 | }
35 | for(int j=0; j<=y; j++){
36 | dp[0][j] = 0;
37 | }
38 | for(int i=1; i<=x; i++){
39 | for(int j=1; j<=y; j++){
40 | if(s1[i-1] == s2[j-1]){
41 | dp[i][j] = 1 + dp[i-1][j-1];
42 | }else{
43 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
44 | }
45 | }
46 | }
47 | return dp[x][y];
48 | }
49 |
--------------------------------------------------------------------------------
/dynamic programming/LongestCommonSubstring.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 |
9 |
10 | class Solution{
11 | public:
12 |
13 | int longestCommonSubstr (string S1, string S2, int n, int m)
14 | {
15 | // your code here
16 | int dp[n+1][m+1];
17 | int max = 0;
18 | memset(dp, 0, sizeof(dp));
19 | for(int i=1; i<=n; i++){
20 | for(int j=1; j<=m; j++){
21 | if(S1[i-1] == S2[j-1]){
22 | dp[i][j] = 1 + dp[i-1][j-1];
23 | if(max < dp[i][j]){
24 | max = dp[i][j];
25 | }
26 | }else{
27 | dp[i][j] = 0;
28 | }
29 | }
30 | }
31 | return max;
32 | }
33 | };
34 |
35 | // { Driver Code Starts.
36 |
37 | int main()
38 | {
39 | int t; cin >> t;
40 | while (t--)
41 | {
42 | int n, m; cin >> n >> m;
43 | string s1, s2;
44 | cin >> s1 >> s2;
45 | Solution ob;
46 |
47 | cout << ob.longestCommonSubstr (s1, s2, n, m) << endl;
48 | }
49 | }
50 | // Contributed By: Pranay Bansal
51 | // } Driver Code Ends
--------------------------------------------------------------------------------
/dynamic programming/LongestIncreasingSubsequence.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | int longestSubsequence(int, int[]);
6 |
7 | int main()
8 | {
9 | //taking total testcases
10 | int t,n;
11 | cin>>t;
12 | while(t--)
13 | {
14 | //taking size of array
15 | cin>>n;
16 | int a[n];
17 |
18 | //inserting elements to the array
19 | for(int i=0;i>a[i];
21 |
22 | //calling method longestSubsequence()
23 | cout << longestSubsequence(n, a) << endl;
24 | }
25 | }
26 | // } Driver Code Ends
27 |
28 |
29 | // return length of longest strictly increasing subsequence
30 |
31 | int longestSubsequence(int n, int a[])
32 | {
33 | // your code here
34 | vector v;
35 | v.push_back(a[0]);
36 | for(int i=1; i v.back()){
38 | v.push_back(a[i]);
39 | }else{
40 | int min = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
41 | v[min] = a[i];
42 | }
43 | }
44 | return v.size();
45 | }
--------------------------------------------------------------------------------
/dynamic programming/LongestPathInMatrix.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #define n 3
3 | using namespace std;
4 |
5 | int findLongestFromACell(int i, int j, int mat[n][n], int dp[n][n])
6 | {
7 | if (i < 0 || i >= n || j < 0 || j >= n)
8 | return 0;
9 |
10 | if (dp[i][j] != -1)
11 | return dp[i][j];
12 |
13 | int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN;
14 |
15 | if (j < n - 1 && ((mat[i][j] + 1) == mat[i][j + 1]))
16 | x = 1 + findLongestFromACell(i, j + 1, mat, dp);
17 |
18 | if (j > 0 && (mat[i][j] + 1 == mat[i][j - 1]))
19 | y = 1 + findLongestFromACell(i, j - 1, mat, dp);
20 |
21 | if (i > 0 && (mat[i][j] + 1 == mat[i - 1][j]))
22 | z = 1 + findLongestFromACell(i - 1, j, mat, dp);
23 |
24 | if (i < n - 1 && (mat[i][j] + 1 == mat[i + 1][j]))
25 | w = 1 + findLongestFromACell(i + 1, j, mat, dp);
26 |
27 | return dp[i][j] = max(x, max(y, max(z, max(w, 1))));
28 | }
29 |
30 | int finLongestOverAll(int mat[n][n])
31 | {
32 | int result = 1;
33 |
34 | int dp[n][n];
35 | memset(dp, -1, sizeof dp);
36 | for (int i = 0; i < n; i++) {
37 | for (int j = 0; j < n; j++) {
38 | if (dp[i][j] == -1) {
39 | findLongestFromACell(i, j, mat, dp);
40 | }
41 | result = max(result, dp[i][j]);
42 | }
43 | }
44 |
45 | return result;
46 | }
47 |
48 | // Driver program
49 | int main()
50 | {
51 | int mat[n][n] = { { 1, 2, 9 },
52 | { 5, 3, 8 },
53 | { 4, 6, 7 } };
54 | cout << "Length of the longest path is "
55 | << finLongestOverAll(mat);
56 | return 0;
57 | }
58 |
--------------------------------------------------------------------------------
/dynamic programming/MaxLengthChain.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | struct val{
6 | int first;
7 | int second;
8 | };
9 |
10 |
11 |
12 | int maxChainLen(struct val p[],int n);
13 | int main() {
14 | // your code goes here
15 | int t;
16 | cin>>t;
17 | while(t--)
18 | {
19 | int n;
20 | cin>>n;
21 | val p[n];
22 | for(int i=0;i>p[i].first>>p[i].second;
25 | }
26 |
27 | cout<a.second);
43 | }
44 | int maxChainLen(struct val p[],int n)
45 | {
46 | //Your code here
47 | sort(p, p+n, comp);
48 | int end = 0, sum = 1;
49 | int dp[n];
50 | for(int i=0; i sum){
64 | sum = dp[i];
65 | }
66 | }
67 | return sum;
68 | }
--------------------------------------------------------------------------------
/dynamic programming/MaximizeCutSegments.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | int maximizeTheCuts(int n, int x, int y, int z);
6 |
7 | int main() {
8 |
9 | //taking testcases
10 | int t;
11 | cin >> t;
12 | while(t--)
13 | {
14 | //taking length of line segment
15 | int n;
16 | cin >> n;
17 |
18 | //taking types of segments
19 | int x,y,z;
20 | cin>>x>>y>>z;
21 |
22 | //calling function maximizeTheCuts()
23 | cout<= 0){
42 | dp[i] = max(dp[i], dp[i-a]);
43 | }
44 |
45 | if(i-b >= 0){
46 | dp[i] = max(dp[i], dp[i-b]);
47 | }
48 |
49 | if(i-c >= 0){
50 | dp[i] = max(dp[i], dp[i-c]);
51 | }
52 |
53 | if(dp[i] != -1){
54 | dp[i]++;
55 | }
56 | }
57 |
58 | return max(dp[n], 0);
59 | }
--------------------------------------------------------------------------------
/dynamic programming/MaximumSumIncreasingSubsequence.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution{
9 |
10 | public:
11 | int maxSumIS(int arr[], int n)
12 | {
13 | // Your code goes here
14 | int dp[n];
15 | int ans = 0;
16 | for(int i=0; i arr[j]){
22 | dp[i] = max(dp[i], dp[j]+arr[i]);
23 | }
24 | }
25 | }
26 | sort(dp, dp+n);
27 | return dp[n-1];
28 | }
29 | };
30 |
31 | // { Driver Code Starts.
32 | int main()
33 | {
34 |
35 |
36 | int t;
37 | cin >> t;
38 | while (t--)
39 | {
40 | int n;
41 | cin >> n;
42 |
43 | int a[n];
44 |
45 | for(int i = 0; i < n; i++)
46 | cin >> a[i];
47 |
48 |
49 |
50 | Solution ob;
51 | cout << ob.maxSumIS(a, n) << "\n";
52 |
53 | }
54 | return 0;
55 | }
56 |
57 | // } Driver Code Ends
--------------------------------------------------------------------------------
/dynamic programming/MinimumNoOfJumps.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | // Function to return minimum number of jumps to end of array
10 | int minJumps(int arr[], int n){
11 |
12 | int count = 0;
13 | int i = 0;
14 | while(i < n-1) {
15 | if(arr[i] == 0)
16 | return -1;
17 | if(arr[i]+i >= n-1) {
18 | count++;
19 | return count;
20 | }
21 | int mx = INT_MIN;
22 | int loc = 0;
23 | for(int j = 1; j <= arr[i]; j++) {
24 | if(mx+loc < arr[j+i]+j) {
25 | mx = arr[j+i];
26 | loc = j;
27 | }
28 | }
29 | count++;
30 | i += loc;
31 | }
32 | return count;
33 |
34 | }
35 |
36 |
37 | // { Driver Code Starts.
38 |
39 | int main()
40 | {
41 | int t;
42 | cin>>t;
43 | while(t--)
44 | {
45 | int n,i,j;
46 | cin>>n;
47 | int arr[n];
48 | for(int i=0; i>arr[i];
50 |
51 | cout<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution
9 | {
10 | public:
11 | int minOperation(int n)
12 | {
13 | //code here.
14 | int count = 0;
15 | while(n != 0){
16 | if(n%2 == 0){
17 | n /= 2;
18 | }else{
19 | n -= 1;
20 | }
21 | count++;
22 | }
23 | return count;
24 | }
25 | };
26 |
27 | // { Driver Code Starts.
28 | int main()
29 | {
30 | int t;
31 | cin>>t;
32 | while(t--)
33 | {
34 | int n;
35 | cin>>n;
36 | Solution ob;
37 | cout<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 | class Solution{
7 |
8 | public:
9 | int minDiffernce(int arr[], int n)
10 | {
11 | // Your code goes here
12 | int s=0 ;
13 |
14 | for(int i=0; i=0; i-- )
36 | {
37 | if(t[n][i]){
38 | break;
39 | }
40 | }
41 | return s-2*i ;
42 | }
43 | };
44 |
45 |
46 | // { Driver Code Starts.
47 | int main()
48 | {
49 |
50 |
51 | int t;
52 | cin >> t;
53 | while (t--)
54 | {
55 | int n;
56 | cin >> n;
57 |
58 | int a[n];
59 | for(int i = 0; i < n; i++)
60 | cin >> a[i];
61 |
62 |
63 |
64 | Solution ob;
65 | cout << ob.minDiffernce(a, n) << "\n";
66 |
67 | }
68 | return 0;
69 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/dynamic programming/NoOfWaysToCoverDistance.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | long long countWays(int);
6 |
7 | int main()
8 | {
9 | //taking testcases
10 | int t;
11 | cin >> t;
12 |
13 | while(t--)
14 | {
15 | //taking number of steps in stair
16 | int n;
17 | cin>>n;
18 |
19 | //calling function countWays()
20 | cout << countWays(n) << endl;
21 | }
22 |
23 | return 0;
24 |
25 | }
26 | // } Driver Code Ends
27 |
28 |
29 | // function to count number of ways to reach top of the stair
30 | long long countWays(int n){
31 |
32 | // your code here
33 | long long dp[n+1];
34 | dp[0] = 0;
35 | dp[1] = 1;
36 | dp[2] = 2;
37 | dp[3] = 4;
38 |
39 | for(long long i=4; i<=n; i++){
40 | dp[i] = (dp[i-1] + dp[i-2] + dp[i-3])%1000000007;
41 | }
42 | return dp[n];
43 |
44 | }
--------------------------------------------------------------------------------
/dynamic programming/OptimalStrategyForGame.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 |
6 | // } Driver Code Ends
7 |
8 |
9 | long long maximumAmount(int arr[], int n)
10 | {
11 | // long long ans = 0;
12 | long long dp[n][n];
13 |
14 | for(int g = 0; g < n; g++) {
15 | for(int i = 0, j = g; j < n; i++, j++) {
16 | if(g == 0) {
17 | dp[i][j] = arr[i];
18 | }else if(g == 1) {
19 | dp[i][j] = max(arr[i] , arr[j]);
20 | }else {
21 | int val1 = arr[i] + min(dp[i + 2][j], dp[i + 1][j - 1]);
22 | int val2 = arr[j] + min(dp[i + 1][j - 1], dp[i][j - 2]);
23 | int val = max(val1 , val2);
24 | dp[i][j] = val;
25 | }
26 | }
27 | }
28 |
29 | return dp[0][n -1];
30 | }
31 |
32 | // { Driver Code Starts.
33 |
34 | int main() {
35 |
36 | //taking total testcases
37 | int T;
38 | cin>>T;
39 | while(T--)
40 | {
41 | //taking number of elements
42 | int N;
43 | cin>>N;
44 |
45 | int A[N];
46 |
47 | //inserting the elements
48 | for(int i=0;i>A[i];
50 |
51 | //calling function maximumAmount()
52 | cout<
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 |
9 |
10 | // User function Template for C++
11 |
12 | class Solution{
13 | public:
14 | int equalPartition(int N, int arr[]){
15 | int sum=0;
16 | for(int i=0;i> dp(N+1,vector(target+1));
25 | for(int i=0;i<=N;i++){
26 | dp[i][0]=1;
27 | }
28 | for(int j=1;j<=target;j++){
29 | dp[0][j]=0;
30 | }
31 | for(int i=1;i<=N;i++){
32 | for(int j=1;j<=target;j++){
33 | if(arr[i-1]<=j)
34 | dp[i][j]=(dp[i-1][j-arr[i-1]] || dp[i-1][j]);
35 | else
36 | dp[i][j]=dp[i-1][j];
37 | }
38 | }
39 | return dp[N][target];
40 | }
41 | };
42 |
43 | // { Driver Code Starts.
44 |
45 | int main(){
46 | int t;
47 | cin>>t;
48 | while(t--){
49 | int N;
50 | cin>>N;
51 | int arr[N];
52 | for(int i = 0;i < N;i++)
53 | cin>>arr[i];
54 |
55 | Solution ob;
56 | if(ob.equalPartition(N, arr))
57 | cout<<"YES\n";
58 | else
59 | cout<<"NO\n";
60 | }
61 | return 0;
62 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/dynamic programming/ShortestCommonSupersequence.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | int shortestCommonSupersequence(char* , char*, int, int );
8 |
9 | int main()
10 | {
11 |
12 | int t;
13 | char X[10001], Y[10001];
14 |
15 | //taking total testcases
16 | cin >> t;
17 | while(t--){
18 |
19 | //taking String X and Y
20 | cin >> X >> Y;
21 |
22 | //calling function shortestCommonSupersequence()
23 | cout << shortestCommonSupersequence(X, Y, strlen(X), strlen(Y))<< endl;
24 | }
25 | return 0;
26 | }
27 |
28 | // } Driver Code Ends
29 |
30 |
31 | //User function template for C++
32 |
33 | // X : 1st given string of size m
34 | // Y : 2nd given string of size n
35 | int shortestCommonSupersequence(char* X, char* Y, int m, int n) {
36 | //code here
37 | int dp[m+1][n+1];
38 | memset(dp, 0, sizeof(dp));
39 | for(int i=1; i<=m; i++){
40 | for(int j=1; j<=n; j++){
41 | if(X[i-1] == Y[j-1]){
42 | dp[i][j] = 1 + dp[i-1][j-1];
43 | }else{
44 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
45 | }
46 | }
47 | }
48 | return m+n-dp[m][n];
49 | }
--------------------------------------------------------------------------------
/dynamic programming/coin_change.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution
9 | {
10 | public:
11 | long long int count( int S[], int m, int n){
12 |
13 | long long int dp[m+1][n+1];
14 | for(int i=0;i<=m;i++){
15 | dp[i][0]=1;
16 | }
17 | for(int i=1;i<=n;i++){
18 | dp[0][i]=0;
19 | }
20 | for(int i=1;i<=m;i++){
21 | for(int j=1;j<=n;j++){
22 | dp[i][j]=dp[i-1][j];
23 | if(j-S[i-1]>=0){
24 | dp[i][j]+=dp[i][j-S[i-1]];
25 | }
26 | }
27 | }
28 | return dp[m][n];
29 | }
30 | };
31 |
32 | // { Driver Code Starts.
33 | int main()
34 | {
35 | int t;
36 | cin>>t;
37 | while(t--)
38 | {
39 | int n,m;
40 | cin>>n>>m;
41 | int arr[m];
42 | for(int i=0;i>arr[i];
44 | Solution ob;
45 | cout<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution
9 | {
10 | public:
11 | int editDistance(string s, string t)
12 | {
13 | int m = s.length();
14 | int n = t.length();
15 | int dp[m+1][n+1];
16 | int i,j;
17 | for(i=0;i<=m;i++){
18 | for(j=0;j<=n;j++){
19 | if(i == 0)
20 | dp[i][j] = j;
21 | else if(j == 0)
22 | dp[i][j] = i;
23 | else if(s[i-1] == t[j-1])
24 | dp[i][j] = dp[i-1][j-1];
25 | else{
26 | dp[i][j] = 1 + min(min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1]);
27 | }
28 | }
29 | }
30 | return dp[m][n];
31 | }
32 | };
33 |
34 | // { Driver Code Starts.
35 | int main(){
36 | int T;
37 | cin >> T;
38 | while(T--)
39 | {
40 | string s, t;
41 | cin >> s >> t;
42 | Solution ob;
43 | int ans = ob.editDistance(s, t);
44 | cout << ans <<"\n";
45 | }
46 | return 0;
47 | }
48 | // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/AlienDictionary.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Initial Template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 | // User function Template for C++
9 |
10 | void dfs(int i, bool vis[], vector> v, string ans){
11 | vis[i] = true;
12 | for(auto x:v[i]){
13 | if(!vis[x]){
14 | dfs(x, vis, v, ans);
15 | }
16 | }
17 | char ch = i + 'a';
18 | ans = ch + ans;
19 | }
20 |
21 | class Solution{
22 | public:
23 | string findOrder(string dict[], int N, int K) {
24 | //code here
25 | vector> v(K);
26 | for(int i=0; i> t;
68 | while (t--) {
69 | int N, K;
70 | cin >> N >> K;
71 | string dict[N];
72 | for (int i = 0; i < N; i++) cin >> dict[i];
73 |
74 | Solution obj;
75 | string ans = obj.findOrder(dict, N, K);
76 | order = "";
77 | for (int i = 0; i < ans.size(); i++) order += ans[i];
78 |
79 | string temp[N];
80 | std::copy(dict, dict + N, temp);
81 | sort(temp, temp + N, f);
82 |
83 | bool f = true;
84 | for (int i = 0; i < N; i++)
85 | if (dict[i] != temp[i]) f = false;
86 |
87 | if(f)cout << 1;
88 | else cout << 0;
89 | cout << endl;
90 | }
91 | return 0;
92 | }
93 | // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/CircleOfStrings.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | // Initial Template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 | // User function Template for C++
9 |
10 | class Solution
11 | {
12 | public:
13 | int isCircle(int N, vector A)
14 | {
15 | vector adj[26];
16 | vector in(26, 0);
17 | vector out(26, 0);
18 | for(int i=0; i vis(26, 0);
36 | dfs(adj, vis, src);
37 | for(int i=0; i<26; i++) {
38 | if(out[i] && !vis[i])
39 | return 0;
40 | }
41 | return 1;
42 | }
43 | void dfs(vector adj[], vector& vis, int s) {
44 | vis[s] = 1;
45 | for(auto& it: adj[s]) {
46 | if(!vis[it]) {
47 | dfs(adj, vis, it);
48 | }
49 | }
50 | }
51 | };
52 |
53 | // { Driver Code Starts.
54 | int main()
55 | {
56 | int t;
57 | cin>>t;
58 | while(t--){
59 | int N;
60 | cin>>N;
61 | vector A;
62 | string s;
63 |
64 | for(int i = 0;i < N; i++)
65 | {
66 | cin>>s;
67 | A.push_back(s);
68 | }
69 |
70 | Solution ob;
71 | cout<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | /* This function is used to detect a cycle in undirected graph
9 |
10 | * g[]: array of vectors to represent graph
11 | * V: number of vertices
12 | */
13 | bool isCyclicUtil(int i, int V, vector adj[],bool visited[], bool inTheCall[])
14 | {
15 | visited[i]=true;
16 | inTheCall[i]=true;
17 | for(int j=0;j adj[], int V)
30 | {
31 | bool visited[V];
32 | bool inTheCall[V];
33 | for(int i=0;i>T;
53 | while(T--)
54 | {
55 | int V, E;
56 | cin>>V>>E;
57 |
58 | // array of vectors to represent graph
59 | vector adj[V];
60 |
61 | int u, v;
62 | for(int i=0;i>u>>v;
65 |
66 | // adding edge to the graph
67 | adj[u].push_back(v);
68 | adj[v].push_back(u);
69 | }
70 |
71 | cout << isCyclic(adj, V)<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | /* This function is used to detect a cycle in undirected graph
9 |
10 | * g[]: array of vectors to represent graph
11 | * V: number of vertices
12 | */
13 | bool isCyclicUtil(vector g[], int curr, vector &visited, int parent){
14 | visited[curr] = true;
15 | for(auto i:g[curr]){
16 | if(!visited[i]){
17 | if(isCyclicUtil(g, i, visited, curr)){
18 | return true;
19 | }
20 | }else if(i != parent || i == curr){
21 | return true;
22 | }
23 | }
24 | return false;
25 | }
26 |
27 | bool isCyclic(vector g[], int V)
28 | {
29 | // Your code here
30 | vector visited(V);
31 | for(int i=0; i>T;
47 | while(T--)
48 | {
49 | int V, E;
50 | cin>>V>>E;
51 |
52 | // array of vectors to represent graph
53 | vector adj[V];
54 |
55 | int u, v;
56 | for(int i=0;i>u>>v;
59 |
60 | // adding edge to the graph
61 | adj[u].push_back(v);
62 | adj[v].push_back(u);
63 | }
64 |
65 | cout << isCyclic(adj, V)<
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 | class Solution
9 | {
10 | public:
11 | //Function to find whether a path exists from the source to destination.
12 | bool dfs(vector>&grid,int i,int j){
13 | int n = grid.size(), m=grid[0].size();
14 | if(i<0 || j<0 || i>=n || j>=m || grid[i][j]==0){
15 | return false;
16 | }
17 | if(grid[i][j]==2){
18 | return true;
19 | }
20 | int x = grid[i][j];
21 |
22 | grid[i][j]=0;
23 |
24 | return(dfs(grid,i+1,j) || dfs(grid,i-1,j) || dfs(grid,i,j+1) || dfs(grid,i,j-1));
25 | }
26 |
27 | bool is_Possible(vector>& grid)
28 | {
29 | //code here
30 | int n = grid.size();
31 | for(int i=0; i> tc;
47 | while(tc--){
48 | int n;
49 | cin >> n;
50 | vector>grid(n, vector(n, -1));
51 | for(int i = 0; i < n; i++){
52 | for(int j = 0; j < n; j++){
53 | cin >> grid[i][j];
54 | }
55 | }
56 | Solution obj;
57 | bool ans = obj.is_Possible(grid);
58 | cout << ((ans) ? "1\n" : "0\n");
59 | }
60 | return 0;
61 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/FindNoOfIslands.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 | class Solution {
7 | public:
8 | void mark(int rows, int cols, int i, int j, vector>& grid){
9 | if(i<0 || i>=rows || j<0 || j>=cols || grid[i][j] != '1'){
10 | return;
11 | }
12 |
13 | grid[i][j] = '2';
14 | mark(rows, cols, i+1, j, grid);
15 | mark(rows, cols, i-1, j, grid);
16 | mark(rows, cols, i, j+1, grid);
17 | mark(rows, cols, i, j-1, grid);
18 | mark(rows, cols, i-1, j-1, grid);
19 | mark(rows, cols, i+1, j+1, grid);
20 | mark(rows, cols, i+1, j-1, grid);
21 | mark(rows, cols, i-1, j+1, grid);
22 | }
23 |
24 | int numIslands(vector>& grid) {
25 | // Code here
26 | int rows = grid.size();
27 | int cols = grid[0].size();
28 | if(rows == 0){
29 | return 0;
30 | }
31 | int is = 0;
32 | for(int i=0; i> tc;
48 | while(tc--){
49 | int n, m;
50 | cin >> n >> m;
51 | vector>grid(n, vector(m, '#'));
52 | for(int i = 0; i < n; i++){
53 | for(int j = 0; j < m; j++){
54 | cin >> grid[i][j];
55 | }
56 | }
57 | Solution obj;
58 | int ans = obj.numIslands(grid);
59 | cout << ans <<'\n';
60 | }
61 | return 0;
62 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/FloydWarshall.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | //Initial template for C++
3 |
4 | #include
5 | using namespace std;
6 |
7 | // } Driver Code Ends
8 | //User function template for C++
9 |
10 | class Solution {
11 | public:
12 | void shortest_distance(vector>&matrix){
13 | // Code here
14 | int n = matrix.size();
15 | for(int k=0; k> tc;
35 | while(tc--){
36 | int n;
37 | cin >> n;
38 | vector>matrix(n, vector(n, -1));
39 | for(int i = 0; i < n; i++){
40 | for(int j = 0; j < n; j++){
41 | cin >> matrix[i][j];
42 | }
43 | }
44 | Solution obj;
45 | obj.shortest_distance(matrix);
46 | for(int i = 0; i < n; i++){
47 | for(int j = 0; j < n; j++){
48 | cout << matrix[i][j] << " ";
49 | }
50 | cout << "\n";
51 | }
52 | }
53 | return 0;
54 | } // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/Kosaraju'sAlgo.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include
3 | using namespace std;
4 |
5 | // } Driver Code Ends
6 |
7 |
8 |
9 | class Solution
10 | {
11 | public:
12 | //Function to find number of strongly connected components in the graph.
13 | vector order;
14 |
15 | void dfs(int src, vector &vis, vector g[]){
16 | vis[src] = 1;
17 | for(auto x:g[src]){
18 | if(!vis[x]){
19 | dfs(x, vis, g);
20 | }
21 | }
22 | order.push_back(src);
23 | }
24 |
25 | void rdfs(int src, vector &vis1, vector rev[]){
26 | vis1[src] = 1;
27 | for(auto x:rev[src]){
28 | if(!vis1[x]){
29 | rdfs(x, vis1, rev);
30 | }
31 | }
32 | }
33 |
34 | int kosaraju(int V, vector adj[])
35 | {
36 | //code here
37 | order.clear();
38 | vector rev[V];
39 | for(int y=0; y vis(V, 0);
45 | for(int i=0; i vis1(V, 0);
52 | int count = 0;
53 | for(int i=V-1; i>=0; i--){
54 | if(!vis1[order[i]]){
55 | rdfs(order[i], vis1, rev);
56 | count++;
57 | }
58 | }
59 | return count;
60 | }
61 | };
62 |
63 | // { Driver Code Starts.
64 |
65 |
66 | int main()
67 | {
68 |
69 | int t;
70 | cin >> t;
71 | while(t--)
72 | {
73 | int V, E;
74 | cin >> V >> E;
75 |
76 | vector adj[V];
77 |
78 | for(int i = 0; i < E; i++)
79 | {
80 | int u, v;
81 | cin >> u >> v;
82 | adj[u].push_back(v);
83 | }
84 |
85 | Solution obj;
86 | cout << obj.kosaraju(V, adj) << "\n";
87 | }
88 |
89 | return 0;
90 | }
91 |
92 | // } Driver Code Ends
--------------------------------------------------------------------------------
/graph/MinimumCostPath.cpp:
--------------------------------------------------------------------------------
1 | // { Driver Code Starts
2 | #include