diff --git a/Count inversion b/Count inversion new file mode 100644 index 0000000..821aa8c --- /dev/null +++ b/Count inversion @@ -0,0 +1,99 @@ +#include +using namespace std; + + + // } Driver Code Ends +class Solution{ + public: + // arr[]: Input Array + // N : Size of the Array arr[] + // Function to count inversions in the array. + long long int merge(long long arr[],long long low,long long mid,long long high){ + + long long i = low; + long long j = mid+1; + long long int count = 0; + vectortemp; + + while(i<=mid && j <= high){ + if(arr[i] <= arr[j]){ + temp.push_back(arr[i]); + i++; + } + else if(arr[j] < arr[i]){ + + temp.push_back(arr[j]); + j++; + count += (mid - i) +1 ; + } + + } + + if(i <= mid){ + for(int k = i;k<=mid;k++){ + temp.push_back(arr[k]); + } + } + else if(j <= high){ + for(int k = j;k<=high;k++){ + temp.push_back(arr[k]); + } + } + + long long k = 0; + for(int i = low;i<=high;i++){ + arr[i] = temp[k]; + k++; + } + + return count; + } + + long long int mergeSort(long long arr[],long long low ,long long high){ + + if(high == low){ + return 0; + } + + + long long mid = (high+low)/2; + + long long int count = 0; + + count += mergeSort(arr,low,mid); + count += mergeSort(arr,mid+1,high); + count += merge(arr,low,mid,high); + + return count; + } + + long long int inversionCount(long long arr[], long long N) + { + int count = 0; + return mergeSort(arr,0,N-1); + } + +}; + +// { Driver Code Starts. + +int main() { + + long long T; + cin >> T; + + while(T--){ + long long N; + cin >> N; + + long long A[N]; + for(long long i = 0;i> A[i]; + } + Solution obj; + cout << obj.inversionCount(A,N) << endl; + } + + return 0; +} + // } Driver Code Ends diff --git a/bharat/main.cpp b/bharat/main.cpp new file mode 100644 index 0000000..4fd6536 --- /dev/null +++ b/bharat/main.cpp @@ -0,0 +1,64 @@ +// +// main.cpp +// Codeforces_Div 637 +// +// Created by Bharat on 04/09/20. +// Copyright © 2020 Bharat Tandon. All rights reserved. +// + +#include +#include +#include +using namespace std; + +int main() { + int t; + cin>>t; + + while(t--){ + int a,b,i=0,j=0,k,p,z,l; + cin>>a>>b; + + if(b > a){ + while(a != b){ + + if(1<=b-a && b-a<= 10){ + a = a + (b-a); + i++; + } + else if(b-a > 10){ + z = b-a; + k = z/10; + a = a + (10*k); + + i = i+k; + } + + } + cout< 10){ + l = a-b; + p = l/10; + a = a - (10*p); + + j = j+p; + } + + } + cout< +using namespace std; + + // } Driver Code Ends +// User function Template for C++ + +class Solution{ +public: + int dp[201][201][2]; + // int solve(string S , int i ,int j,int isTrue){ + + // if(i>j){ + // return 0; + // } + + // if(i==j){ + // if(isTrue){ + // return S[i] = 'T' ?1:0; + // } + // else{ + // return S[i] = 'F'?1:0; + // } + // } + + + // if(dp[i][j][isTrue] != -1){ + // return dp[i][j][isTrue]; + // } + + + + // int total = 0; + + // for(int k = i+1;kj){ + return 0; + } + if(i == j){ + if(isTrue == 1){ + return S[i] == 'T'?1:0; + } + else{ + return S[i] == 'F'?1:0; + } + } + + if(dp[i][j][isTrue] != -1){ + return dp[i][j][isTrue]; + } + + int total = 0 ; + for(int k = i+1;k>t; + while(t--){ + int N; + cin>>N; + string S; + cin>>S; + + Solution ob; + cout< +using namespace std; +#define N 1000 + + + // } Driver Code Ends +// you are required to complete this function +// function should print the required range +class Solution{ + public: + pair findSmallestRange(int arr[][N], int n, int k){ + priority_queue>,vector>>,greater>>>q; + + int maxi = INT_MIN; + + for(int i = 0;i ans ; + + while(1){ + pair> temp = q.top(); + int min = temp.first; + int i = temp.second.first; + int j = temp.second.second; + q.pop(); + if(maxi-min < range){ + range = maxi-min; + ans = make_pair(min,maxi); + } + + if(j+1 < n){ + q.push({arr[i][j+1],{i,j+1}}); + if(arr[i][j+1] > maxi){ + maxi = arr[i][j+1]; + } + } + else{ + break; + } + + } + + return ans; + } + +}; + +// { Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, k; + cin>>n>>k; + int arr[N][N]; + pair rangee; + for(int i=0; i>arr[i][j]; + Solution obj; + rangee = obj.findSmallestRange(arr, n, k); + cout< +using namespace std; +#define N 1000 + + + // } Driver Code Ends +// you are required to complete this function +// function should print the required range +class Solution{ + public: + pair findSmallestRange(int arr[][N], int n, int k){ + priority_queue>,vector>>,greater>>>q; + + int maxi = INT_MIN; + + for(int i = 0;i ans ; + + while(1){ + pair> temp = q.top(); + int min = temp.first; + int i = temp.second.first; + int j = temp.second.second; + q.pop(); + if(maxi-min < range){ + range = maxi-min; + ans = make_pair(min,maxi); + } + + if(j+1 < n){ + q.push({arr[i][j+1],{i,j+1}}); + if(arr[i][j+1] > maxi){ + maxi = arr[i][j+1]; + } + } + else{ + break; + } + + } + + return ans; + } + +}; + +// { Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, k; + cin>>n>>k; + int arr[N][N]; + pair rangee; + for(int i=0; i>arr[i][j]; + Solution obj; + rangee = obj.findSmallestRange(arr, n, k); + cout< +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + // } Driver Code Ends +//User function template for C++ + +/* +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; +*/ +class Solution{ + public: + //Function to find the height of a binary tree. + + void solve1(struct Node* root ,int level,int & ans){ + + if(root == NULL){ + return; + } + + level++; + if(level > ans){ + ans = level; + } + + solve1(root->left,level,ans); + solve1(root->right,level,ans); + + } + + + int height(struct Node* node){ + int ans = 0,level = 0; + solve1(node,level,ans); + return ans; + } +}; + +// { Driver Code Starts. +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + string treeString; + getline(cin,treeString); + Node* root = buildTree(treeString); + Solution ob; + cout< +using namespace std; + + + // } Driver Code Ends +class Solution{ + public: + // arr: input array + // n: size of array + //Function to find the sum of contiguous subarray with maximum sum. + int maxSubarraySum(int nums[], int n){ + + int sum = nums[0]; + int maxi = nums[0]; + for(int i = 1;i sum){ + sum = maxi; + } + } + + return sum; + + } +}; + +// { Driver Code Starts. + +int main() +{ + int t,n; + + cin>>t; //input testcases + while(t--) //while testcases exist + { + + cin>>n; //input size of array + + int a[n]; + + for(int i=0;i>a[i]; //inputting elements of array + + Solution ob; + + cout << ob.maxSubarraySum(a, n) << endl; + } +} + // } Driver Code Ends diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6b55f0c --- /dev/null +++ b/main.cpp @@ -0,0 +1,104 @@ +// +// main.cpp +// BST +// +// Created by Bharat on 19/07/21. +// + +#include +#include +#include +#include + +#include + +using namespace std; + + +class Node{ +public: + int data; + Node *left; + Node* right; + + Node(int x){ + data =x; + left = NULL; + right = NULL; + } +}; + +Node* constructBst(vectorarr,int n,int *pos,int min,int max){ + + if(*pos >= n){ + return NULL; + } + + Node* new_node = NULL; + if(arr[*pos] > min && arr[*pos] < max){ + new_node = new Node(arr[*pos]); + *pos = *pos +1; + + if(*pos < n){ + new_node->left = constructBst(arr, n, pos, min, new_node->data); + } + if(*pos < n){ + new_node->right = constructBst(arr, n, pos, new_node->data, max); + } + } + return new_node; +} + +void preOrder(Node* root){ + if(root == NULL){ + return; + } + + cout<data<<" "; + preOrder(root->left); + preOrder(root->right); +} + +Node* inorderPredecessor(Node* root,Node* curr){ + while(root->right != NULL && root->right != curr){ + root = root->right; + } + return root; +} + +void morrisInorderTraversal(Node* root){ + + while(root != NULL){ + if(root->left == NULL){ + cout<data<<" "; + root = root->right; + } + else{ + Node* predecessor =inorderPredecessor(root->left,root); + if(predecessor->right == NULL){ + predecessor->right = root; + root = root->left; + } + else{ + predecessor->right = NULL; + cout<data<<" "; + root = root->right; + } + } + } + +} +void preOrderBST(vectorarr){ + int pos = 0; + Node* root = constructBst(arr, (int)arr.size(), &pos, INT_MIN,INT_MAX); + preOrder(root); + cout<A = {10, 5, 1, 7, 40, 50}; + preOrderBST(A); + return 0; +} + diff --git a/minimum sum b/minimum sum new file mode 100644 index 0000000..f43ebfb --- /dev/null +++ b/minimum sum @@ -0,0 +1,92 @@ +//Initial template for C++ + +#include +using namespace std; + + // } Driver Code Ends +//User function template for C++ + +class Solution{ +public: + string solve(int arr[], int n) { + sort(arr,arr+n); + stack s1,s2; + + int flag = 0,carry = 0; + string ans = ""; + for(int i = n-1;i>=0;i--){ + if(flag == 0){ + s1.push(arr[i]); + flag = 1; + } + else{ + s2.push(arr[i]); + flag = 0; + } + + if(s1.size() == 1 && s2.size() == 1){ + int a = s1.top(); + s1.pop(); + int b = s2.top(); + s2.pop(); + + int sum = a+b+carry; + + if(sum >=10){ + carry = sum/10; + sum = sum%10; + ans = to_string(sum) + ans; + } + else{ + carry = 0; + ans = to_string(sum)+ans; + } + } + + } + + if(s1.size() == 1){ + int a = s1.top(); + int sum = a+carry; + if(sum != 0){ + ans = to_string(sum)+ans; + } + + } + else{ + if(carry != 0){ + ans = to_string(carry)+ans; + } + } + + int i = 0; + while(ans[i] == '0' ){ + i++; + } + + if(ans[0] == '0'){ + ans.erase(0,i); + } + + return ans; + + } +}; + +// { Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + Solution ob; + auto ans = ob.solve(arr, n); + cout << ans << "\n"; + } + return 0; +} // } Driver Code Ends