Skip to content
Open

Dev #114

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Count inversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <bits/stdc++.h>
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;
vector<long long>temp;

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<N;i++){
cin >> A[i];
}
Solution obj;
cout << obj.inversionCount(A,N) << endl;
}

return 0;
}
// } Driver Code Ends
64 changes: 64 additions & 0 deletions bharat/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// main.cpp
// Codeforces_Div 637
//
// Created by Bharat on 04/09/20.
// Copyright © 2020 Bharat Tandon. All rights reserved.
//

#include <iostream>
#include <stack>
#include <vector>
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<<i<<endl;
}

else{
while(a != b){
if(1<=a-b && a-b<= 10){
a = a- (a-b);
j++;
}
else if(a-b > 10){
l = a-b;
p = l/10;
a = a - (10*p);

j = j+p;
}

}
cout<<j<<endl;
}
}




return 0;
}
Loading