Skip to content

Commit

Permalink
Files Committed
Browse files Browse the repository at this point in the history
  • Loading branch information
KingFire25 committed Oct 4, 2022
1 parent ff09f6e commit e43f5f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
35 changes: 35 additions & 0 deletions coding_freshmen/C++/Ashutosh_Panda/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Problem Title: Print Matrix in Spiral order

## Problem Explanation

Ex:- Given Matrix :
1 2 3
4 5 6
7 8 9
Output- 1 2 3 6 9 8 7 4 5

## Logic and Intuition

* An optimized approach will be to create 4 variables that store the upper ,lower ,left and right bound of the Matrix which decreases after each iteration.
* We will iterate through every row and column in a spiral manner until the lower and right bound are greater than upper and left bound respectively.

## Time Complexity and Space Complexity

* Time Complexity: O(m*n)
* Space Complexity: O(1)


# Problem Title: Sieve of Eratosthenes

To find all the prime numbers upto a given number.

## Logic & Intuition

* We will create a bool array which is used to check if a number is prime or not (initially all are set to true).
* We will iterate the array $\sqrt{n}$ times and in each iteration we will mark all the multiples of i as false.
* At the end of the loop the remaining array will contain only the prime numbers.

## Time Complexity and Space Complexity

* Time Complexity: O(n * log(log(n)) )
* Space Complexity: O(n)
26 changes: 26 additions & 0 deletions coding_freshmen/C++/Ashutosh_Panda/SoErato.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;
void SioErato(int n) // Sieve of Eratosthenes
{
int i;
bool t[n + 1];
fill(t, t + n + 1, true);
t[0] = false, t[1] = false;
for (i = 2; i * i <= n; i++)
if(t[j])
for (int j = 2 * i; j <= n; j += i)
if (j % i == 0)
t[j] = false;

for (i = 1; i <= n; i++)
if (t[i] == true)
cout << i << " ";
}
int main()
{
int n;
cout<<"Enter upper limit: ";
// cin>>n;
SioErato(1000);
return 0;
}
40 changes: 40 additions & 0 deletions coding_freshmen/C++/Ashutosh_Panda/spiral.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;
void spiOrd(vector<vector<int>> &m)
{
int i, l = 0, u = 0, d = m.size(), r = m[0].size();
while (l < r and u < d)
{
for (i = l; i < r; i++)
cout<<m[u][i]<<" ";
u++;
for (i = u; i < d; i++)
cout<<m[i][r - 1]<<" ";
r--;
if (u < d)
{
for (i = r - 1; i >= l; i--)
cout<<m[d - 1][i]<<" ";
d--;
}
if (l < r)
{
for (i = d - 1; i >= u; i--)
cout<<m[i][l]<<" ";
l++;
}
}
}

int main()
{
int n,r,c,i,j;
cout<<"enter no. of rows and columns: ";
cin>>r>>c;
vector<vector<int>>v(r,vector<int>(c,0));
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>v[i][j];
spiOrd(v);
return 0;
}

0 comments on commit e43f5f4

Please sign in to comment.