Simplifying the code to avoid TLE while rotating 2D matrix clockwise - c++

I have working code for rotating 2D matrix in clockwise direction but I'm having TLE (time limit exceeded) problem when k reaches big numbers. I don't know how to simplify my code, I'm guessing the for cycles are causing the problem but I can't see a way to make my code work without them. Is there any other way to simplify my code to avoid TLE?
#include <iostream>
#include <fstream>
using namespace std;
void funk(int a[][101], int n,int k);
int main()
{
int a[101][101],n,k;
ifstream ived;
ived.open("15.txt");
ived>>n>>k;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
ived>>a[i][j];
}
}
ived.close();
funk(a, n, k);
ofstream isved;
isved.open("15rez.txt");
for (int i=0;i<n; i++) {
for (int j=0; j<n; j++) {
isved<<a[i][j]<<" ";
}
isved<<endl;
}
isved.close();
return 0;
}
void funk(int a[][101], int n, int k) {
for (int pak=0; pak<k; pak++) {
for (int i=0; i<n/2; i++) {
for (int j=i; j<n-i-1; j++) {
int prad=a[i][j];
a[i][j] = a[n-1-j][i];
a[n-1-j][i] = a[n-1-i][n-1-j];
a[n-1-i][n-1-j] = a[j][n-1-i];
a[j][n-1-i]=prad;
}
}
}
}

If k is the number of times the matrix should be rotated, then you can use the fact that after 4 rotations it transforms into itself. Hence, the result will be the same if you replace k with k % 4, thereby replacing O(k) algorithm with O(1) one.

Related

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

Complexity of the program that finds number of products for given n

I have the code for finding the number of products for a given number n. I think that complexity is sqrt(n^3), but some of them think that it is n^2. Here is the code:
int f(int n)
{
int i,j,k,p,r=0;
k=sqrt(n);
p=n/2;
for (i=2; i<=p; i++)
for(j=2; j<=k; j++)
if(i*j==n)
r++;
return r;
}
The reasoning behind my logic is next:
T=C1+(n/2-1)C2+(n/2-1)(sqrt(n)-1)C3
but I'm not completely sure
(Assuming the inner loop is meant to say j<=k rather than i<=k.)
Your logic and answer are both correct (in the big-Oh sense).
Another way to look at this is that the outer loop is O(n/2) (which is the same as O(n)) and the inner loop is O(sqrt(n)). Multiplying the two gives O(n*sqrt(n)), which is algebraically equivalent to O(sqrt(n^3)).
Lets refactor the code a bit. First, some density:
int f(int n)
{
int r=0;
int counter = 0;
for (int i=2; i<= n/2; i++)
for(int j=2; j<=sqrt(n); j++) {
if(i*j==n) r++;
++counter;
}
return r;
}
I already introduced a counter and now we will move that counter up out of the loops:
int f(int n)
{
int r=0;
int counter = 0;
for (int i=2; i<= n/2; i++)
counter += (sqrt(n) - 1);
for(int j=2; j<=sqrt(n); j++) {
if(i*j==n) r++;
}
return r;
}
one more step:
int f(int n)
{
int r=0;
int counter = 0;
counter += (n/2 - 1) * (sqrt(n) -1);
for (int i=2; i<= n/2; i++)
for(int j=2; j<=sqrt(n); j++) {
if(i*j==n) r++;
}
return r;
}
Conclusion: Your reasoning is correct. Them probably didnt realize that you only have to consider factors up to sqrt(n) which would leave you with worse complexity.
The complexity of this code is O(Nsqrt(N)), because your first loop has N/2 steps, and your second loop has sqrt(N) steps, multiply those, and you'll get N/2*sqrt(N), hence the O(NsqrtN).

How to add two binary numbers in the array c++

I think I made a mistake somewhere but I can't seem to find it. I think the problem lies in adding or entering data in the wrong order. I apologize for any mistakes, English is not my primary language. enter image description here
#include <iostream>
using namespace std;
int main()
{
int d, n, m, carry;
int a[10000];
int b[10000];
int addition[10001];
cin>>d;
for(int i=0; i<d; i++)
{
int a[10000]={0};
int b[10000]={0};
int addition[10001]={0};
cin>>n;
for(int i=n; i>=1; i--)
{
cin>>a[i];
}
cin>>m;
for (int i=m; i>=1; i--)
{
cin>>b[i];
}
if(n<m)
{
n=m;
}
carry=0;
for (int i=1; i<=n; i++)
{
addition[i]=(a[i]+b[i]+carry)%2; //way my teacher
carry=(a[i]+b[i]+carry)/2;
}
addition[n+1]=carry;
// if(addition[n+1]==0)n--;
for(int i=n; i>=0; i--)
{
cout<<addition[i];
}
}
return 0;
}
I think there are at least two errors.
1) In
for(int i=n; i>=0; i--)
you're counting down to 0. But in every other loop, you've counted down to 1. Since you're counting down too far, your output would have an extra zero at the end. (For example, it would show 1000 when it should have shown 100.)
2) Also in that loop, you're starting at n. But you've potentially put a carry into n+1, aren't you forgetting to output it too?

Bad Access C++ Error

Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.
It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.

Why is my program pausing after a selection sort function?

I have a simple sorting program being compiled by Dev-C++ 4.9.8.0. I ran the program (yes this compiles) and it simply stops after displaying the line where the vector is displayed for the first time. Note - it does not freeze, it seems to just be taking a pause. In the code, the selection sort comes next so I assume that the error happens there, but there is no error message for me to even figure out what to do!
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
void bubbleSort (vector<int>& data)
{
if(data.size() <= 1)
return;
int flag=1;
int temp;
for(int i=1; (i<=data.size()) && flag; i++)
{
flag=0;
for(int j=0; (j<data.size()-1); j++)
{
if(data[j+1] > data[j])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
flag=1;
}
}
}
}
void selectionSort(vector<int>& data)
{
int min, temp, n=data.size();
for (int i=0; i<n; i++)
{
min = i;
for (int j=i+1; j<n; j++)
{
if (j<min)
{
temp=i;
i=min;
min=temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
selectionSort(data);
cout<<"Sorted Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
system("Pause");
return 0;
}
selectionSort() method has variable 'n' that is completely a random value that happens to be on the stack at that location. You haven't initialized it!
You have a nested loop, which is O(n^2). Say n is 1982734 or some such arbitrarily large number. You are simply looping over 1982734 * 1982734 times. EVENTUALLY it will complete. Why don't you print the value of 'n' inside selectionSort(). Just initialize it with the size of the vector.
As others commented this whole work is in progress.