I am trying to make my inversion counting program run faster since I am continuously getting TLE.
I used C++ and this is my code
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n;
int count = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
if(arr[i] > arr[j])
{
count++;
}
}
cout << count;
}
Help please!
Related
Firstly when I have code this program it was running perfectly but running it again, it is not showing expected output can someone tell what's wrong with it
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
int loc,min;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n - 1;i++){
min = arr[i];
for (int j = i + 1; j < n; j++)
{
if(min>arr[j]){
min = arr[j];
loc = j;
}
swap(arr[loc],arr[i]);
}
}
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
}
Forgoing the fact that variable-length arrays are not part of standard C++ (and thus code tutorials that use them should be burned), the code has two main problems.
On an already sorted sequence, the inner-most if body will never be entered, and therefore loc will never receive a determinate value.
The swap is in the wrong place..
Explanation
Within your code...
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
int loc,min; // loc is INDETERMINATE HERE
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n - 1;i++){
min = arr[i];
for (int j = i + 1; j < n; j++)
{
if(min>arr[j]){
min = arr[j];
loc = j; // loc ONLY EVER SET HERE
}
swap(arr[loc],arr[i]); // loc IS USED HERE EVEN IF NEVER SET
}
}
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
}
The purpose of the inner loop is to find the location (loc) of the most extreme value (smallest, largest, whatever you're using for your order criteria) within the remaining sequence. No swapping should be taking place in the inner loop, and the initial extreme value location (again, loc) should be the current index of the outer loop (in this case i)
Therefore...
We don't need min. It is pointless.
We must initialize loc to be i before entering the inner loop.
We swap after the inner loop, and then only if loc is no longer i.
The result looks like this.
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n - 1; i++)
{
int loc = i;
for (int j = i + 1; j < n; j++)
{
if (arr[loc] > arr[j])
loc = j; // update location to new most-extreme value
}
// only need to swap if the location is no longer same as i
if (loc != i)
swap(arr[loc], arr[i]);
}
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
The line swap(arr[loc],arr[i]); should be outside the inner for loop, so move it one line down.
Also, you will want to initialize loc to i at the start of the outer for loop.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
int loc,min;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n - 1;i++){
min = arr[i];
loc=i;
for (int j = i + 1; j < n; j++)
{
if(min>arr[j]){
min = arr[j];
loc = j;
}
swap(arr[i],arr[loc]);
}
}
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
}
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, a[n];
cin >> n;
float b = n / 2;
for (int i = 1; i <= n; i++)
cin >> a[i];
// the code seems to be not working from here till the point marked below
if (n % 2 == 0)
{
for (int k = 1; k <= b; k++)
{
for (int j = (b + 1); j <= n; j++)
{
if (a[k] > a[j])
swap(a[k], a[j]);
}
}
}
// till this point(pls tell me what's wrong with this part of the code)
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
getch();
return 0;
}
1
3 2
6 5 4
10 9 8 7
I want to print the following pattern. I have tried very hard but couldn't make the code for it. I have tried everything which came up to my mind.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1;i <= n; i++) {
for (j = 1; j <= i; j++) {
k++;
printf("%d ", k);
}
printf("\n");
}
}
the other code which i tried is this.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1; i <= n; i++) {
for (j = i; j >= 1; j--) {
k++;
printf("%d ",j);
}
printf("\n");
}
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int previousRow = 0;
for(int row = 1; row <= 4; row++)
{
int rowTracker = row;
for(int col = 0; col < row; col++)
{
cout<<rowTracker - col + previousRow<<" ";
}
previousRow += row;
cout<<endl;
}
return 0;
}
#include<iostream>
void printPattern(unsigned numlevels)
{
unsigned last_num = 1;
for(unsigned i = 0; i < numlevels; ++i)
{
unsigned next_num = i + last_num;
for(unsigned j = next_num; j >= last_num; --j)
{
std::cout << j << ' ';
}
std::cout << '\n';
last_num = next_num + 1;
}
}
int main()
{
unsigned n;
std::cin >> n;
printPattern(n);
return 0;
}
You may also use a stack to implement this. Here is a working answer:
#include <iostream>
#include <stack>
using namespace std;
int main() {
int i, j, n;
stack<int> st;
cin >> n;
int k = 0;
for(i = 1;i <= n; i++) {
for(j = 1; j <= i; j++) {
k++;
st.push(k);
}
while(!st.empty()){
printf("%d ", st.top());
st.pop();
}
printf("\n");
}
}
Hope it helps!
Before reading the code blow, you should really try to do it yourself. This problem is obviously for practice and to develop the programming muscle. Just getting the answer is not going to help.
The issue with your code is that for each row, the range you want to print is not being determined correctly. You should first find the range and then print the numbers. Ther can be multiple approaches to it. Below is one of them.
for(i=1;i<=n;i++){
int max = i*(i+1)/2;
int min = i*(i-1)/2 + 1;
for(j=max;j>=min;j--){
printf("%d ",j);
}
printf("\n");
}
Here is a simple method
int main(int argc, char* argv[])
{
int n = 4; // suppose print 4 lines
for (int i = 1; i <= n; ++i)
{
int i0 = (i + 1) * i / 2; // first number of line i
for (int j = 0; j < i; j++)
cout << i0 - j << " ";
cout << endl;
}
return 0;
}
thanx everyone for your responses. I was able to do it on my own. Below is what I did. if there is any correction let me know
#include<iostream>
using namespace std;
int main(){
int i,j,n,temp;
cin>>n;
int k=0;
for(i=1;i<=n;i++){
k=k+i,temp=k;
for(j=1;j<=i;j++){
cout<<temp<<+" ";
temp--;
}
cout<<("\n");
}
}
I have been struggling with it for 2 days.Please, could anyone tell me why does it exceed the time limit when i use as input 20000 and 0 and 40000 numbers afterwards ? I tried to make the variables type as large as possible, but that does not seem to help either.
#include <bits/stdc++.h>
using namespace std;
int main()
{
/*freopen("file.in", "r", stdin);
freopen("file.out", "w" , stdout);*/
long long int aux,i, n, k, j, total = 0;
cin >> n >> k;
long long int a[n], b[n], order[n];
signed long long int profit[n];
for(i = 0; i < n; i++)
cin >> a[i];
for(i = 0; i < n; i++)
cin >> b[i];
for(i = 0; i < n; i++)
profit[i] = a[i] - b[i];
for(i = 0; i < n; i++)
order[i] = i;
for(i = 0; i < n; i++)
for(j = i + 1; j < n; j++)
if(profit[order[i]] > profit[order[j]])
{
aux = order[i];
order[i] = order[j];
order[j] = aux;
}
if(k > 0)
for(i = 0; i < k; i++)
{
total += a[order[i]];
}
for(i = k; i < n; i++)
{
if(profit[order[i]] < 0)
total += a[order[i]];
else
total += b[order[i]];
}
cout << total;
return 0;
}
The complexity of your code is O(n^2), which is too much for N=20000. Reduce the complexity, replacing your bubble sort with Qsort. Try std::sort with custom comparison function.
I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}