Created
November 12, 2018 11:02
-
-
Save rsmahmud/5134e5aa2363afdf6f2951448e0d84c4 to your computer and use it in GitHub Desktop.
CSE330 ETP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int comparefun (const void *a, const void * b) { | |
return ( *(int *)a - *(int *)b ); | |
} | |
void find(int a[], int n, int k){ | |
int ll, rr; | |
qsort (a, n, sizeof(a[0]), comparefun); | |
for (int i = 0; i < n - 3; i++){ | |
for (int j = i+1; j < n - 2; j++){ | |
ll = j + 1; | |
rr = n - 1; | |
while (ll < rr){ | |
if( a[i] + a[j] +a[ll] + a[rr] == k){ | |
cout<<a[i]<<" "<<a[j]<<" "<<a[ll]<<" "<<a[rr]<<" $"; | |
ll++; rr--; | |
} | |
else if (a[i] + a[j] + a[ll] + a[rr] < k) | |
ll++; | |
else | |
rr--; | |
} | |
} | |
} | |
} | |
int main() { | |
int t; | |
cin>>t; | |
for(int k=0;k<t;k++){ | |
int n,q; | |
cin>>n>>q; | |
int a[n]; | |
for(int i=0;i<n;i++) | |
cin>>a[i]; | |
find(a,n,q); | |
cout<<endl; | |
} | |
return 0; | |
} | |
//Second | |
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
int T; | |
cin >> T; | |
while(T--){ | |
int N; | |
cin>>N; | |
int arr[N], step = 0, j; | |
for(int i=0;i<N;i++) | |
cin >> arr[i]; | |
for(int i=1;i<N;i++){ | |
if(arr[i]<arr[i-1]){ | |
int temp=arr[i]; | |
for( j=i;arr[j]>=arr[i];j--) | |
arr[j]=arr[j-1]; | |
arr[j]=temp; | |
step++; | |
} | |
} | |
cout<<step<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment