Skip to content

Commit ec81ded

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents cb5dbed + 424cdbb commit ec81ded

File tree

8 files changed

+287
-13
lines changed

8 files changed

+287
-13
lines changed

.vscode/settings.json

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

Bit Manipulation/max_subset_XOR.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Given an array of N positive integers. Find an integer denoting the maximum XOR subset value in the given array.
2+
// Time complexity: O(n)
3+
4+
5+
#include<bits/stdc++.h>
6+
#define fl(i,a,b) for(i=a;i<b;i++)
7+
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
8+
using namespace std;
9+
typedef long long int ll;
10+
11+
ll maxSubsetXOR(ll set[], ll n)
12+
{
13+
ll index=0, max_pos, max;
14+
for (ll i = 31; i >= 0; i--)
15+
{
16+
max= LONG_MIN;
17+
max_pos= index;
18+
for (ll j = index; j < n; j++)
19+
{
20+
if ( (arr[j] & (1 << i)) != 0 && arr[j] > max )
21+
{
22+
max= arr[j];
23+
max_pos= j;
24+
}
25+
26+
}
27+
if (max == LONG_MIN)
28+
continue;
29+
30+
swap(arr[index], arr[max_pos]);
31+
max_pos = index;
32+
33+
for (ll j=0; j<n; j++)
34+
{
35+
if (j != max_pos && (arr[j] & (1 << i)) != 0)
36+
arr[j] = arr[j] ^ arr[max_pos];
37+
}
38+
index++;
39+
}
40+
ll ans = 0;
41+
for (ll i = 0; i < n; i++)
42+
ans ^= arr[i];
43+
return ans;
44+
}
45+
46+
int main()
47+
{
48+
fast;
49+
50+
#ifndef ONLINE_JUDGE
51+
freopen("input.txt","r",stdin);
52+
freopen("output.txt","w",stdout);
53+
#endif
54+
55+
ll n; cin>>n;
56+
ll arr[n];
57+
for(auto &x: arr)
58+
cin>>x;
59+
60+
cout << maxSubsetXOR(arr, n);
61+
62+
return 0;
63+
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Src : LeetCode
3+
--------------
4+
5+
Given the root of a binary tree, flatten the tree into a "linked list":
6+
7+
1) The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
8+
2) The "linked list" should be in the same order as a pre-order traversal of the binary tree.
9+
10+
Input: root = [1,2,5,3,4,null,6]
11+
Output: [1,null,2,null,3,null,4,null,5,null,6]
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
TreeNode* flattenSubtrees(TreeNode* root){
28+
if(!root->left && !root->right) return root;
29+
if(root->left==NULL)
30+
return fun(root->right);
31+
TreeNode* last=fun(root->left);
32+
last->right=root->right;
33+
root->right=root->left;
34+
root->left=NULL;
35+
return fun(last);
36+
}
37+
38+
void flatten(TreeNode* root) {
39+
if(!root) return;
40+
flatten(root->left);
41+
flatten(root->right);
42+
TreeNode* right=root->right;
43+
root->right=root->left;
44+
root->left=NULL;
45+
TreeNode* cur=root;
46+
while(cur->right){
47+
cur=cur->right;
48+
}
49+
cur->right=right;
50+
51+
}
52+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Node :
2+
"""Class to define tree nodes,
3+
this has a value attribute to store the value, and
4+
left and right attributes to point to the left and right child respectively"""
5+
def __init__(self, value):
6+
self.value = value
7+
self.right = None
8+
self.left = None
9+
10+
def expression_tree_evaluation (node):
11+
"""A recursive function which evaluates the expression subtree passed to it
12+
and returns the evaluated value"""
13+
if node == None: # Node doesn't exist
14+
return 0
15+
if node.left == None and node.right == None: # leaf node containing an operand
16+
return node.value
17+
18+
# node is an operator
19+
left_child = expression_tree_evaluation(node.left)
20+
right_child = expression_tree_evaluation (node.right)
21+
22+
return eval(str(left_child)+node.value+str(right_child))
23+
24+
25+
# DRIVER CODE
26+
27+
if __name__ == '__main__':
28+
# Expression in the example : (5*4)+(100-2)
29+
root = Node('+')
30+
root.left = Node('*')
31+
root.left.left = Node('5')
32+
root.left.right = Node('4')
33+
root.right = Node('-')
34+
root.right.left = Node('100')
35+
root.right.right = Node('20')
36+
print (expression_tree_evaluation(root))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* problem statement
2+
first input is n representing the number of days
3+
after that we have n values ,representing the price of stock on that day
4+
ouput should be maximum profit , if you can buy and sell only one time
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
int main()
10+
{
11+
int n;
12+
cin >> n; // it represent the number of days
13+
int arr[n]; // this array contains the prices of stock on that day
14+
for (int i = 0; i < n; i++)
15+
cin >> arr[i];
16+
int lsf = 9999999; //least so far
17+
int op = 0; //overall profit
18+
int pist = 0;
19+
for (int i = 0; i < n; i++)
20+
{
21+
if (arr[i] < lsf)
22+
{
23+
lsf = arr[i];
24+
}
25+
int pist = arr[i] - lsf; // if stock is sold today
26+
if (pist > op)
27+
{
28+
op = pist;
29+
}
30+
}
31+
cout << op << endl;
32+
}
33+
34+
/* sample input values are
35+
9
36+
11
37+
6
38+
7
39+
19
40+
4
41+
1
42+
6
43+
18
44+
4
45+
*/
46+
47+
/*sample output value for the upper input
48+
17
49+
*/

Numbers/SquareRoot.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.*;
2+
public class SquareRoot
3+
{
4+
/* Time Comeplexity:-O(lg(n))
5+
Space Complexity:- O(1)**/
6+
public static void main(String args[])throws IOException
7+
{
8+
InputStreamReader read=new InputStreamReader(System.in);
9+
BufferedReader in=new BufferedReader(read);
10+
System.out.println("Enter your number");
11+
int n=Integer.parseInt(in.readLine());
12+
int x=n;
13+
if(x<=1)
14+
System.out.println(x);
15+
int sqrt=x/2;
16+
int quotient=x/sqrt;
17+
while(sqrt>quotient)
18+
{
19+
sqrt=(sqrt+quotient)>>1;//bitwise operator used to make the process faster
20+
quotient=x/sqrt;
21+
}
22+
System.out.println("The square root of the number "+n+" is = "+sqrt);
23+
}
24+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This repository contains all the popular Competitive Programming questions and I
2424
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/General%20Questions">General Questions</a></li>
2525
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Greedy">Greedy</a></li>
2626
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Hackerrank%20solutions">HackerRank Solutions</a></li>
27+
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Hashing">Hashing</a></li>
2728
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Numbers">Numbers</a></li>
2829
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/OOPs">OOPs</a></li>
2930
<li><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Passwords">Passwords</a></li>

Sorting Algorithms/HeapSortinC.C

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
void heapify(int arr[],int n,int i)
4+
{
5+
int max=i; //parent node index
6+
int left = 2*i +1; // left child node index
7+
int right =2*i+2; //right child node index
8+
int temp;
9+
if(left<n && arr[left]> arr[max]) //if left child node is greater than parent
10+
{
11+
max =left;
12+
}
13+
if(right<n && arr[right]> arr[max])
14+
{
15+
max =right;
16+
}
17+
if(max != i)
18+
{
19+
temp = arr[i]; //swapping the max element to the root node
20+
arr[i]=arr[max];
21+
arr[max]=temp;
22+
heapify(arr,n,max); //every node element
23+
}
24+
}
25+
26+
27+
void heapsort(int arr[],int n)
28+
{
29+
int i,temp;
30+
31+
for(i=n/2-1;i>=0;i--)
32+
{
33+
heapify(arr,n,i);
34+
}
35+
//swap the elements and heapify again
36+
for(i=n-1;i>=0;i--)
37+
{
38+
temp=arr[0];
39+
arr[0]=arr[i];
40+
arr[i]=temp;
41+
heapify(arr,i,0); //root node swap from 0th index
42+
}
43+
}
44+
int main()
45+
{
46+
int arr[100],n,i;
47+
printf("\n Enter the n of the elements:");
48+
scanf("%d",&n);
49+
printf("Enter the elements:");
50+
for(i=0;i<n;i++)
51+
{
52+
53+
scanf("%d",&arr[i]);
54+
}
55+
heapsort(arr,n);
56+
printf("The Heap Sorted Array is:");
57+
for(i=0;i<n;i++)
58+
{
59+
printf("\t %d",arr[i]);
60+
}
61+
}

0 commit comments

Comments
 (0)