UVa online judge - 3n + 1 - wrong answer? - c++

Here's my C++ code for the 3n+1 problem from UVA online judge which runs fine here, but each submission is judged as the wrong solution. I believe it has something to do with input, or output formatting. I just don't know exactly what the issue is. Could anyone help me investigate this issue?
#include <iostream>
using namespace std;
int main(){
int i, j, ori, orj, complexity = 0;
while(!cin.eof()){
cin >> i >> j;
ori = i;
orj = j;
if (i > j){
int temp = i;
i = j;
j = temp;
}
for (int k = i; k <= j; k++){
int c = 1;
int n = k;
do{
c++;
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
} while (n != 1);
if (c > complexity)
complexity = c;
}
cout << ori << " " << orj << " " << complexity << endl;
}
return 0;
}
Each submission has come under the time limit, and when I debug i get correct outputs.

!cin.eof() is not a good way to determine when to exit from the loop.
Try changing
while(!cin.eof()){
cin >> i >> j;
to
while(cin >> i >> j){

You have to clear value of complexity variable for every new input numbers. Your code also doesn't handles correctly values of i and j equal to 1. You have to add special condition and not to run the loop in this case. So this it the whole code change:
int main() {
int i, j, ori, orj, complexity;
while (!cin.eof()) {
complexity = 0;
cin >> i >> j;
ori = i;
orj = j;
if (i > j) {
int temp = i;
i = j;
j = temp;
}
if (j > 1) {
for (int k = i; k <= j; k++) {
int c = 1;
int n = k;
do {
c++;
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
} while (n != 1);
if (c > complexity)
complexity = c;
}
} else {
complexity = 1;
}
cout << ori << " " << orj << " " << complexity << endl;
}
return 0;
}

Related

Getting runtime error on codechef (SIGTSTP)

When I run this code in VScode I don't get any error but the program doesn't take input and ends.
#include <iostream>
using namespace std;
int main()
{
int t, N, a, b, i, j, k, n, l = 1, m = 1;
int number[N];
cin >> t;
for (k = 1; k <= t; k++)
{
cin >> N >> a >> b;
for (i = 1; i <= N; i++)
{
cin >> number[i];
}
for (j = l; j <= N; j++)
{
if (number[n] % a != 0)
{
cout << "ALICE" << endl;
break;
}
if (number[j] % a == 0)
{
l = j;
break;
}
}
for (n = m; n <= N; n++)
{
if (number[n] % b != 0)
{
cout << "BOB" << endl;
break;
}
if (number[n] % b == 0)
{
m = n;
break;
}
}
}
return 0;
}
Please explain why I am getting this error.
The reason you are getting the error because of the line int number[N]; Here N is not defined and N can have any garbage value. You have to either statically define the value of N which will result in memory leakage because you initially doesn't know how much length array is required to avoid this leakage you can use vectors for dynamic allocation.
For vector implementation you can refer this.

program which print a matrix in spiral way. ( is not working fine)

I need to write a program which displays the elements of a matrix in a spiral way.My program does not work fine.Here's the code:
#include <iostream>
using namespace std;
void citireMatrice(int a[100][100], int n) // function to read a matrix
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
cout<<"a[" << i << "][" << j << "]=";
cin >> a[i][j];
}
}
}
void spiral(int a[100][100], int n)
{
int i, j, k;
if (n % 2==0)
{
k = n / 2;
}
else
{
k = n / 2 + 1;
}
for (i = 1; i <= k; ++i)
{
for (j = 1; j <= n - i + 1; ++j)
{
cout << a[i][j] << " ";
}
for (j = i + 1; j <= n - i + 1; ++j)
{
cout << a[j][n - i + 1] << " ";
}
for (j = n-i; j >= i; j--)
{
cout << a[n - i + 1][j] << " ";
}
for (j = n-1;j>=i+1;j--)
{
cout << a[j][i];
}
}
}
int main()
{
int a[100][100];
int n;
cout << "n=";
cin >> n;
citireMatrice(a, n);
spiral(a, n);
return 0;
}
If I enter n=2 with the elements 1, 2, 3, 4 it displays 4 -858993460 and other numbers like this.Where's my mistake?
You're properly using zero based indexing of arrays in citireMatrice but in spiral you're using one based indexing.
You need to start your loops at 0, and end at < n. (Consider what element of a will be first to be printed out.)

C++ Dijkstra's algorithm program with initial vertices problem and resulting one less vertex

I'm working on C++ representation/implementation of Dijkstra's algorithm and I found this program online which fails to execute properly on TurboC++.
Any one know the solution? I also want to know why there is a minimum value of 31999 and the coding runs on a mobile emulator but refuses to run on PC TurboC++
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//using namespace std;
int shortest(int, int);
int cost[10][10], dist[20], i, j, n, k, m, S[20], v, totcost, path[20], p;
int main()
{
int c;
cout << "enter no of vertices";
cin >> n;
cout << "enter no of edges";
cin >> m;
cout << "\nenter\nEDGE Cost\n";
for (k = 1; k <= m; k++)
{
cin >> i >> j >> c;
cost[i][j] = c;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (cost[i][j] == 0)
cost[i][j] = 31999;
cout << "enter initial vertex";
cin >> v;
cout << v << "\n";
shortest(v, n);
}
int shortest(int v, int n)
{
int min;
for (i = 1; i <= n; i++)
{
S[i] = 0;
dist[i] = cost[v][i];
}
path[++p] = v;
S[v] = 1;
dist[v] = 0;
for (i = 2; i <= n - 1; i++)
{
k = -1;
min = 31999;
for (j = 1; j <= n; j++)
{
if (dist[j] < min && S[j] != 1)
{
min = dist[j];
k = j;
}
}
if (cost[v][k] <= dist[k])
p = 1;
path[++p] = k;
for (j = 1; j <= p; j++)
cout << path[j];
cout << "\n";
//cout <<k;
S[k] = 1;
for (j = 1; j <= n; j++)
if (cost[k][j] != 31999 && dist[j] >= dist[k] + cost[k][j] && S[j] != 1)
dist[j] = dist[k] + cost[k][j];
}
}
Arrays are 0 based so all the loops from 1 to <= n are suspect.

Another approach for finding longest increasing subsequence

I am aware that there are many algorithms for finding longest increasing subsequence, but the algorithms using DP all have this is common - they recurse/ dynimacally calculate longest subsequence "ENDING" at a particular element of the array.
I wrote a solution which resurses taking longest subsequence "starting" at a particular array, which seems to work fine too.
#include<iostream>
using namespace std;
#define boostio ios_base::sync_with_stdio(0);
int n;
int a[100000];
int t[100000] = {-1};
int lis(int i){
if(t[i]!= -1){
return t[i];
}
if(i == n){t[i] = 1; return 1;
}
for (int x = i+1; x <= n ; ++x)
{
if(a[x] > a[i] and 1 + lis(x) > t[i]){
t[i] = 1 + lis(x);
//return t[i];
}
}
if(t[i] != -1){
return t[i];
}
t[i] = 1; return 1;
}
int main(){
boostio;
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
fill(t, t + n+2 ,-1);
Int m = 0;
for (int i = 1; i <= n; ++i)
{
//cout << i << " " << lis(i) << endl;
if(lis(i) >m) m= lis(i);
}
cout << m;
return 0;
}
I am wondering, is this in any way worse than if we recurse on "last element" of subsequence instead of the first. They both appear to be order n square algorithms to me, so why isnt this algorithm more circulated. To me it seems more intuitive. Am I missing something?

Forming Heap Using array

I am trying to form a heap using the following code ,But not sure why its not showing the correct output.
#include <iostream>
using namespace std;
int h[10], n;
void heapbottom()
{
int i, j;
for (i = n / 2; i >= 1; i--) {
int k = i;
int v = h[k];
bool heap = false;
while (!heap && 2 * k <= n) {
cout << "\n i value is :" << i;
j = 2 * k;
if (j < n) //there sre 2 children
{
if (h[j] < h[j + 1])
j++;
}
if (v >= h[j])
heap = true;
else {
h[k] = h[j];
k = j;
}
h[k] = v;
} //end of while
}
cout << "\n HEAP GENERATED \n";
for (int i = 0; i < n; i++)
cout << "\n ELEMENT IS:" << h[i];
}
int main()
{
cout << "\n Enter the maximum number of array elements \n";
cin >> n;
cout << "\n Enter the array to perform heap sort \n";
for (int i = 0; i < n; i++)
cin >> h[i];
heapbottom();
return 0;
}
If I change the outer loop to be
for (i = n / 2; i >= 0; i--)
I get 9 8 7 6 5 2 as a result, which I believe is a valid heap.