Time complexity related to subArray problem - time-complexity

How we can decrees time complexity of finding subarray of a given array in O(n) from O(n^3)
void subArray(int arr[],int n)
{
int i=0,j,k;
while(i<n)
{
j=i;
while(j<n)
{
k=i;
while(k<=j)
{
cout<<arr[k]<<" ";
k++;
}
cout<<endl;
j++;
}
i++;
}
}

Related

space complexity when a vector is copied

what will be the space complexity of this class? and why?
what will be the space complexity used by the function moveZeroes
class Solution {
public:
void moveZeroes(vector<int>& nums) {
vector<int>nums2(nums);
int j=0;
int numOfZeros=0;
for(int i=0;i<nums2.size();i++){
if(nums2[i]!=0){
nums[j]=nums2[i];
j++;
}
else{
numOfZeros++;
}
}
for (int k =numOfZeros; k >0 ; k--)
{
nums[j]=0;
j++;
}
}
};

time complexity of the following three loop dependent condition

Can anyone find me the big O notation of the following code?
sum=0;
for(i=0;i<n;i++)
{
for(int j=0;j<i*i;j++)
{
for(int k=0;k<j;k++)
sum++;
}
}

What is time complexity of below function?

int sumOfDigits(int n)
{
if(n<=9)
return n;
else
{
int r=0;
while(n!=0)
{
r=r+n%10;
n=n/10;
}
sumOfDigits(r);
}
}
This function finds sum of digits of a number till it becomes less than 10.
e.g.if n=12345
then output=6
as 1+2+3+4+5=15,
again 1+5=6.
I guess what you try to achieve is something like this:
class St2 {
static int sumOfDigits(int n) {
if(n<=9) {
return n;
}
else {
int d = n%10;
return d + sumOfDigits((n-d)/10);
}
}
public static void main(String[] args) {
System.out.println(St2.sumOfDigits(345678));
}
}
This would sum the digits. The complexity is linear in the number of digits thus logarithmic in the scale of the input number.

sudoku algorithm analysis

This is the code for solving sudoku (9x9):
#include <iostream>
#define dim 9
#define br_polja dim*dim
using namespace std;
bool Fsearch (int tab[dim][dim],int *x,int *y, int cand[dim], int *num_cand, int &free) {
int min=9;
*x=-1;
*y=-1;
free=0;
for (int i=0;i<dim;i++) {
for (int j=0;j<dim;j++) {
if (tab[i][j]!=0) continue;
free+=1;
*num_cand=0;
int cand_f[dim];
for (int w=0;w<dim;w++)
cand_f[w]=1;
int p,q;
p=i+3-(i%3);
q=j+3-(j%3);
for (int w=p-3;w<p;w++)
for (int r=q-3;r<q;r++)
if (tab[w][r]!=0){
cand_f[tab[w][r]-1]=0;
}
for (int w=0;w<dim;w++) {
if (tab[w][j]!=0) cand_f[tab[w][j]-1]=0;
if (tab[i][w]!=0) cand_f[tab[i][w]-1]=0;
}
for (int w=0;w<dim;w++)
if (cand_f[w]!=0)
*num_cand+=1;
if (*num_cand==1) {
*x=i;
*y=j;
int z=0;
for (int w=0;w<dim;w++)
if (cand_f[w]!=0) {
cand[z]=w+1;
z++;
}
return true;
}
else if (*num_cand<min && *num_cand>0) {
int z=0;
for (int w=0;w<dim;w++)
if (cand_f[w]!=0) {
cand[z]=w+1;
z++;
}
min=*num_cand;
*x=i;
*y=j;
}
else if (*num_cand==0) {
return false;
}
}
}
*num_cand=min;
if (free<1) return false;
else return true;
}
bool Fsudoku(int tab[dim][dim]) {
int free=0;
int x,y;
int cand[dim];
int num_cand;
brojacK=0;
if (!Fsearch(tab,&x,&y,cand, &num_cand,free)) {
if (free==0) return true;
else return false;
}
for (int i=0;i<num_cand;i++) {
tab[x][y]=cand[i];
if (Fsudoku(tab)) return true;
else tab[x][y]=0;
}
return false;
}
Can someone tell help me classify this algorithm in Big O or Big Theta notation. I got O(n^3) but as we can see that's not true! So, give me a tip or an advice how to start, I would be very thankful.
And if you like, I can post this in pseudo-code too!
Thanks,
MB
Big O is about which terms dominate.
So how many times do each of the loops run?
Are they of fixed length, or do they vary?
What parameters can change that would effect the run time?

Factorial in bignum library

Ive tried to create my own implementation of a bignum library
I cant seem to get the factorial to work. If I ask it to solve 4!, it gives out 96. It multiplies 4 twice. similarly, 5! is 600, not 120. I haven't implemented division, so I cant/dont want to divide the answer by the number
//bignum project
#include <iostream>
using namespace std;
class bignum
{
public:
int number[100];
int dpos;
int operator/ (bignum);
bignum operator- (bignum);
bignum operator* (bignum);
bignum operator+ (bignum);
bignum operator= (string);
void output()
{
int begin=0;
for(int i=0; i<=99; i++)
{
if(number[i]!=0 || begin==1)
{
cout<<number[i];
begin=1;
}
}
}
};
bool num_is_zero(bignum k)
{
for(int a=0; a<=99; a++)
{
if(k.number[a]!=0)
{
return false;
}
}
return true;
}
bignum factorial(bignum a)
{
bignum j;
bignum fact;
fact="1";
while(!num_is_zero(a))
{
j="1";
fact=fact*a;
a=a-j;
}
return fact;
}
bignum bignum::operator= (string k)
{
int l;
l=k.length()-1;
for(int h=0; h<=99; h++)
{
number[h]=0;
}
for(int a=99; a>=0 && l>=0; a--)
{
number[a]=k[l]-'0';
l--;
}
}
bignum bignum::operator+ (bignum b)
{
bignum a;
int carry=0;
for(int k=0; k<=99; k++)
{
a.number[k]=0;
}
for(int i=99; i>=0; i--)
{
a.number[i]= number[i]+b.number[i]+a.number[i];
if(a.number[i]>9)
{
carry=(a.number[i]/10);
a.number[i-1]+=carry;
a.number[i]=(a.number[i]%10);
}
}
return (a);
}
bignum bignum::operator- (bignum c)
{
bignum a;
int sign=0;
for(int k=0; k<=99; k++)
{
a.number[k]=0;
}
for(int i=99; i>=0; i--)
{
if(number[i]<c.number[i])
{
number[i]+=10;
if(i!=0)
number[i-1]--;
}
a.number[i]=number[i]-c.number[i];
}
return (a);
}
bignum bignum::operator* (bignum b)
{
bignum ans;
int ans_grid[100][100],x,lines=0,carry,sum[100];
for(int a=0; a<=99; a++)
{
for(int b=0; b<=99; b++)
{
ans_grid[a][b]=0;
}
}
for(int i=99; i>=0; i--)
{
for(int j=i,x=99; j>=0; j--,x--)
{
ans_grid[lines][j]=(number[i]*b.number[x]);
}
lines++;
}
//------------------------------------------------Carry Forward and assign to ans------------------------------------------------//
for(int j=99; j>=0; j--)
{
for(int i=99; i>=0; i--)
{
if(ans_grid[j][i]>9 && i!=0)
{
carry=(ans_grid[j][i]/10);
ans_grid[j][i-1]+=carry;
ans_grid[j][i]%=10;
}
}
}
for(int col=99; col>=0; col--)
{
for(int row=99; row>=0; row--)
{
sum[col]+=ans_grid[row][col];
}
}
for(int i=99; i>=0; i--)
{
if(sum[i]>9 && i!=0)
{
carry=(sum[i]/10);
sum[i-1]+=carry;
sum[i]%=10;
}
}
for(int l=0; l<=99; l++)
ans.number[l]=sum[l];
//-------------------------------------------------------------------------------------------------------------------------------//
return (ans);
}
I think your problem is that sum is uninitialized in your operator*. I'm reluctant to give a partial answer, since I took the code and fiddled with it a bit, so here's my version which appears to work (and also prints "0" correctly):
Update: I couldn't resist making several structural improvements. I didn't touch your multiplication routine, so you should still be able to extract the bugfix even if you don't care for the rest.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class bignum
{
public:
int number[100];
bignum() { std::fill(number, number + 100, 0); }
bignum(const bignum & other) { std::copy(other.number, other.number + 100, number); }
bignum(const std::string &);
bignum & operator-=(const bignum &);
inline bignum operator-(const bignum & c) const { return bignum(*this) -= c; }
bignum operator*(const bignum &) const;
inline bignum & operator= (const std::string & k) { return *this = bignum(k); }
inline operator bool() const
{
for (size_t a = 0; a < 100; ++a)
if (number[a] != 0) return true;
return false;
}
};
std::ostream & operator<<(std::ostream & o, const bignum & b)
{
bool begun = false;
for (size_t i = 0; i < 100; ++i)
{
if (begun || b.number[i] != 0)
{
cout << b.number[i];
begun = true;
}
}
if (!begun) o << "0";
return o;
}
bignum::bignum(const std::string & k)
{
std::fill(number, number + 100, 0);
for(size_t h = 0; h < std::min(k.length(), 100U); ++h)
number[99 - h] = k[k.length() - 1 - h] - '0';
}
bignum & bignum::operator-=(const bignum & c)
{
for (int i = 99; i >= 0; --i)
{
if (number[i] < c.number[i])
{
number[i] += 10;
if (i != 0) --number[i-1];
}
number[i] -= c.number[i];
}
return *this;
}
bignum bignum::operator*(const bignum & b) const
{
bignum ans;
int ans_grid[100][100], lines = 0, carry, sum[100];
std::fill(sum, sum + 100, 0);
for (size_t i = 0; i < 100; ++i) std::fill(ans_grid[i], ans_grid[i] + 100, 0);
for(int i=99; i>=0; i--)
{
for(int j=i,x=99; j>=0; j--,x--)
{
ans_grid[lines][j]=(number[i]*b.number[x]);
}
lines++;
}
//------------------------------------------------Carry Forward and assign to ans------------------------------------------------//
for(int j=99; j>=0; j--)
{
for(int i=99; i>=0; i--)
{
if(ans_grid[j][i]>9 && i!=0)
{
carry=(ans_grid[j][i]/10);
ans_grid[j][i-1]+=carry;
ans_grid[j][i]%=10;
}
}
}
for(int col=99; col>=0; col--)
{
for(int row=99; row>=0; row--)
{
sum[col]+=ans_grid[row][col];
}
}
for(int i=99; i>=0; i--)
{
if(sum[i]>9 && i!=0)
{
carry=(sum[i]/10);
sum[i-1]+=carry;
sum[i]%=10;
}
}
for(int l=0; l<=99; l++)
ans.number[l]=sum[l];
//-------------------------------------------------------------------------------------------------------------------------------//
return (ans);
}
bignum factorial(bignum a)
{
bignum j; j = "1";
bignum fact; fact="1";
while(a)
{
fact = fact * a;
a = a-j;
}
return fact;
}
int main(int argc, char * argv[])
{
if (argc < 2) return 0;
bignum a;
a = std::string(argv[1]);
bignum b = factorial(a);
cout << a << std::endl << b << std::endl;
}
The string assignment is still broken for strings with more than one digit, but that wasn't your question I suppose...