This is my code
long long int inversionCount(long long arr[], long long N)
{
long long cnt=0;
for(long long i=0;i<N-1;i++)
if(arr[i] > arr[i+1]){
swap(arr[i],arr[i+1]);
cnt++;
i=0;
}
return cnt;
}
This is in solution
int arr[]={2,4,1,3,5};
int N = 5;
int cnt=0;
int i=0,j=0;
while(j!=N){
if(arr[i] > arr[j] && i<j){
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
cnt++;
j=i=0;
}else{
i=j;
j++;
}
}
i am using one pointer variable and when (i>i+1) i simply swap and make i=0..
correct me where I wrong...!!
Related
Can anybody please explain the calculation of time and space complexity of this code(finding determinant of a matrix) in detail?
I am not able to understand the exact time complexity of this program using recurrence tree.
class Solution
{
static void getCofactor(int arr[][], int temp[][], int n, int i, int j){
int x = 0, y = 0;
for(int row=0; row<n; row++){
for(int col=0; col<n; col++){
if(row!=i && col!=j){
temp[x][y++] = arr[row][col];
if(y == n-1){
y = 0;
x++;
}
}
}
}
}
static int determinantOfMatrix(int matrix[][], int n)
{
// code here
if(n == 1)
return matrix[0][0];
int temp[][] = new int[n-1][n-1];
int determinant = 0;
int sign = 1;
for(int i=0; i<n; i++){
getCofactor(matrix, temp, n, 0, i);
determinant+=sign*matrix[0][i]*determinantOfMatrix(temp, n-1);
sign=-sign;
}
return determinant;
}
}
All of the Lomuto Partion implementations of Quick Sort have two swaps. Can we just use one swap, and delete the swap codes that outside the for loop? When j == high, the code can swap arr[high] with arr[i]. Following is my code:
1. Original LomutoPartion() implementation:
int lomutoPartition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j = low; j < high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[high - 1];
arr[high - 1] = arr[i];
arr[i] = temp;
return i;
}
2. My LomutoPartition() implementation:
int lomutoPartition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j = low; j <= high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
return i;
}
When I compiled in devc++ compiler, output is not printed. Is there any logical error? This program should accept a number from the user, print the lowest palindrome greater than the given number.
It is a problem from SPOJ PALIN
include
using namespace std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{long long int i;
long long int k;
int flag=1;
long long int n;
int a[10000000];
cin>>n;
n++;
start:
i=0;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;}
i--;
k=i;
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{flag=0;break;}
else
i--;
}
if(flag==1)
cout<<n<<endl;
else
{n++;goto start;}
}
return 0;
}
You have not initialized the flag variable. if(a[i]==a[j]) then the flag will remain uninitialized and may create problems while cheking the if condition unless the default value will be 0 for it.(I am not sure that it would be). Also I can see that the value of flag will never turn 1 according to your code.
#include<iostream>
using namespace::std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{
long int i, k, n; int a[1000]; int flag = 1, duplicaten;
cin>>n;
n++;
start:
//cout<<"inside start : n = "<<n<<"\n";
flag=1; i=0;
duplicaten=n;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;
}
n=duplicaten;
i--;
k=i;
//cout<<"number of digits of n = "<<i+1<<"\n";
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
i--;
}
//cout<<"flag = "<<flag<<endl;
if(flag==1)
{
//cout<<"FOUND!!\n";
cout<<n<<endl;
}
else
{
n++;
//cout<<"NOT found\n";
goto start;
}
}
return 0;
}
You can change the size of your data types yourself and atleast vote up this answer.
< Output before using duplicaten : link to image >
I am not getting where i am going wrong implementing quicksort algorithm.
Below is the code:
#include <bits/stdc++.h>
using namespace std;
int part(vector<int> &arr,int i,int j)
{
int pivot=i;
i++;
while(i<j)
{
while(arr[i]<arr[pivot])
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
swap(arr[i],arr[j]);
}
swap(arr[j],arr[pivot]);
return j;
}
void quickSort(vector <int> &arr,int p,int r) {
if(p<r)
{
int t=part(arr,p,r);
quickSort(arr,p,t-1);
quickSort(arr,t+1,r);
}
}
int main()
{
int n;
cin >> n;
vector <int> arr(n);
for(int i = 0; i < (int)n; ++i) {
cin >> arr[i];
}
quickSort(arr,0,arr.size()-1);
for(int i=0;i<arr.size();i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
i am giving input as
7
5 8 1 3 7 9 2
but getting output as :
2 1 3 7 5 8 9
Can anyone please point out where i am going wrong.
In the "part" function you're swapping at the end, even if the values are already in place.
Just check the values before swapping:
int part(vector<int> &arr, int i, int j)
{
int pivot = i;
i++;
while (i < j)
{
while (arr[i] < arr[pivot])
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j)
swap(arr[i], arr[j]);
}
if (arr[j] < arr[pivot]) {
swap(arr[j], arr[pivot]);
}
return j;
}
If i'm not mistaken, the condition in
if(i<j)
swap(arr[i],arr[j]);
in the function part is not correct; it should check the relation of the array values arr[i] and arr[j] instead of i and j to decide whether the array entries are to be swapped.
I was doing this problem on SPOJ. www.spoj.com/problems/TIP1.
I have written this code but I am getting time limit exceeded when judged. Can anyone help me with any optimization or a better approach.
if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10000010
#define MAXN 10000010
int phi[MAXN + 1], prime[MAXN/10], sz=0;
vector<bool> mark(MAXN + 1);
int ans[10000011];
vector<int> a(10);
vector<int> b(10);
bool isprm(long int x)
{
for(int s=0; s<10; s++)
{
a[s]=b[s]=0;
}
long int y=phi[x];
int i=0,j=0;
while(x>0)
{
int rem=x%10;
x=x/10;
a[i]=rem;
i++;
}
while(y>0)
{
int rem=y%10;
y=y/10;
b[j]=rem;
j++;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(i!=j)
return false;
for(int s=0; s<10; s++)
{
if(a[s]!=b[s])
return false;
}
return true;
}
void precompute_again()
{
for(int i=0; i<=20; ++i)
ans[i]=0;
ans[21]=21;
for(long int i=22; i<10000005; ++i){
bool chk=false;
chk=isprm(i);
if(chk==true)
{
if(i*phi[ans[i-1]]==phi[i]*ans[i-1])
{
ans[i]=i;
}
else
{
if(i*phi[ans[i-1]]>phi[i]*ans[i-1])
{
ans[i]=ans[i-1];
}
else
{
ans[i]=i;
}
}
}
else
{
ans[i]=ans[i-1];
}
}
}
int main()
{
phi[1] = 1;
for (int i = 2; i <= MAXN; i++ ){
if(!mark[i]){
phi[i] = i-1;
prime[sz++]= i;
}
for (int j=0; j<sz && prime[j]*i <= MAXN; j++ ){
mark[prime[j]*i]=1;
if(i%prime[j]==0){
int ll = 0;int xx = i;
while(xx%prime[j]==0)
{
xx/=prime[j];
ll++;
}
int mm = 1;
for(int k=0;k<ll;k++)mm*=prime[j];
phi[i*prime[j]] = phi[xx]*mm*(prime[j]-1);
break;
}
else phi[i*prime[j]] = phi[i]*(prime[j]-1 );
}
}
precompute_again();
int t;
scanf("%d",&t);
while(t--)
{
long int m;
scanf("%ld",&m);
cout<<ans[m]<<endl;
}
return 0;
}
Try to use a variant of Sieve of Eratosthenes.
The following code computes all phi[N] up to MAXN. For MAXN = 1e7 it runs in the blink of an eye.
int i, j;
int * phi = new int [MAXN];
for (i = 1; i < MAXN; i ++)
phi[i] = i;
for (i = 2; i < MAXN; i ++)
{
if (phi[i] != i) continue;
for (j = i; j < MAXN; j += i)
phi[j] = phi[j] / i * (i - 1);
}