Skip to content

Commit

Permalink
Create 01.04.2024.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Masked-coder11 authored Mar 31, 2024
1 parent 7498eb6 commit e9afe6b
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions 01.04.2024.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// User function Template for C++

/*// A Tree node
struct Node
{
int data;
struct Node *left, *right;
};*/

class Solution {
public:

long long int merge(vector<int>&arr, long long low, long long mid, long long high){
long long temp[high-low+1];
long long int k=0;
long long int i=low;
long long int j=mid+1;
long long int inv=0;

while(i<=mid && j<=high){
if(arr[i]<=arr[j]){
temp[k++]=arr[i++];
}
else{
inv+= mid-i +1;
temp[k++]=arr[j++];
}
}

while(i<=mid){
temp[k++]=arr[i++];
}

while(j<=high){
temp[k++]=arr[j++];
}

for(int x=low; x<=high; x++){
arr[x]=temp[x-low];
}

return inv;
}

long long int mergeSort(vector<int>&arr, long long int low, long long int high){
if(low>=high) return 0;
long long int inv=0;

long long mid= (low+high)/2;

inv += mergeSort(arr, low, mid);
inv += mergeSort(arr, mid+1, high);
inv += merge(arr, low, mid, high);

return inv;
}

long long int inversionCount(vector<int>&arr, long long N)
{
// Your Code Here

long long int ans= mergeSort(arr, 0, N-1);

return ans;


}
void inorder(Node*root, vector<int>&arr){
if(root){
inorder(root->left, arr);
arr.push_back(root->data);
inorder(root->right, arr);
}
return;
}

/*You are required to complete below function */
int pairsViolatingBST(int n, Node *root) {
// your code goes here
// store inorder traversal
vector<int>arr;
inorder(root, arr);

// i just want to retun count inversion of arr
long long N=n;
return int(inversionCount(arr, N));


}
};

0 comments on commit e9afe6b

Please sign in to comment.