Friendlist in AIO challenge - c++

I am working on the problem at http://orac.amt.edu.au/cgi-bin/train/problem.pl?set=simple3&problemid=416. Below is the code which I came up. Sample data executed successfully but when I submit the code I am able to execute only 71% of the test cases. Not sure where went wrong. Please guide me.
Briefly the problem is as below:
I am trying to get the max friend list from the friend linking.
1 2
2 3
3 7
1 8
1 9
Output is 1 as it has more friend list
If the friend list match with max the list all.
1 2
2 3
3 7
1 8
9 10
Output should be. 1 2 3 as they have equal number of friends.
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("listin.txt","r",stdin);
freopen("listout.txt","w",stdout);
int f=0, a= 0, b= 0, m = 1;
long size = 1001;
cin >> f;
int arr[size] = {};
int x[size] = {};
for(int i = 0; i < f; i++){
cin >> a >> b;
arr[a] += 1;
arr[b] += 1;
}
for(int j = 0; j < size; j++){
if(m < arr[j]){
m = arr[j];
}
}
for(int j = 0; j < size; j++){
if(m == arr[j]){
x[j] = j;
}
}
for(int k = 0; k < size; k++){
if(x[k] > 0)
cout << x[k] << endl;
}
return 0;
}

Related

Finding largest value in every array's column

Currently I have a 2D array and I need to find largest value of every array's column and find their sum.
My code seems to not work properly because I'm getting largest values of every row, not column, also the program sometimes prints the same value two times.
I wrote this code based on a tutorial and I don't know where I made a mistake. There should be basic C++ functions (cycles, if statements, arrays).
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, m, a[10][10], sum = 0, max;
cin >> n >> m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[j][i];
}
}
for (int i = 0; i < m; i++) {
max = a[0][i];
for (int j = 0; j < n; j = j + 1) {
if (a[j][i] >= max) {
max = a[j][i];
sum = sum + a[j][i];
cout << max << endl; //control printing to see the values
}
}
}
cout << sum;
}
Input:
3 3
3 2 3
7 5 1
6 3 5
Output I need to get is 17 but I am getting 19 right now.
Assuming your array has m columns and n rows.
int sum = 0, max = 0;
for( int i = 0 ; i < m ; ++i )
{
max = 0;
for( int j = 0 ; j < n ; ++j )
{
if(max < a [j][i])
{
max = a[j][i];
}
}
sum += max;
}

Can't find path in Minimum Cost Path using Dynamic Programming

The algorithm is supposed to find the minimum cost path in NxN matrix given as an input. The starting cell is always left bottom and the destination is right top.
Each cell of the matrix represents a cost to traverse through that cell.
You can only move up and right.
I have managed to find the cost, however, I still struggle to backtrack the path.
I tried to start from top right cell and use the greedy algorithm to find my "way back", but the output was either completely wrong or skipping random columns/rows. I also tried to keep track of decisions I was making by creating an additional matrix, but I always end up stuck in the loop.
So how do I find the path?
Here's the code that works well (counts the cost and that's it):
#include <iostream>
using namespace std;
int main()
{
int tab[101][101], N, cost[101][101], backtrack[101][101];
cout << "N (size of NxN matrix) :" << endl;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> tab[i][j];
cost[i][j] = 0;
backtrack[i][j] = 0;
}
}
cost[N-1][0] = tab[N-1][0];
int a = N-1;
for(int i = N-2; i >= 0; i--) // column 0 can be chosen only in 1 way
{
cost[i][0] = cost[i+1][0] + tab[i][0];
backtrack[i][0] = 4; // came from down
}
for(int j = 1; j < N; j++) // row N-1 can be chosen only in 1 way
{
cost[a][j] = cost[a][j-1] + tab[a][j];
backtrack[a][j] = 3; // came from right
}
for(int i = N-2; i >= 0; i--)
{
for(int j = 1; j < N; j++)
{
if(cost[i][j-1] <= cost[i+1][j])
{
cost[i][j] = tab[i][j] + cost[i][j-1];
backtrack[i][j] = 3;
}
else
{
cost[i][j] = tab[i][j]+cost[i+1][j];
backtrack[i][j] = 4;
}
}
}
cout << "Cost: " << cost[0][a] << endl;
return 0;
}
Now, here's the function with flawed additional matrix that's supposed to give me the path, but ends up in an infinite loop:
(matrix backtrack from previous code was given as track here)
void path(int track[101][101], int N)
{
int help[101][101];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
help[i][j] = 0;
}
int w = 0, k = N-1;
help[w][k] = 1; // top right argument is included in the output
while(w < N || k >= 0)
{
if(track[w][k] == 3)
{
help[w][k-1] = 1; // 3 means I came from the previous column k-1
k--;
}
else if(track[w][k] == 4)
{
help[w+1][k] = 1; //4 means I came from the previous row w+1
w++;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(help[i][j] != 0)
cout << i << " " << j << endl;
}
}
}
Example input:
5
2 3 4 2 5
5 2 1 2 2
2 4 2 2 3
1 2 2 4 3
3 2 1 2 3
Expected output:
Cost: 20
4 0
4 1
4 2
3 2
2 2
1 2
1 3
0 3
0 4
Actual output
Cost: 20
And no path at all since it ends up in an infinite loop.
You have written the while loop in path() incorrectly:
while(w < N || k >= 0)
...
You intend this loop to continue until w = N-1 and k=0, which it does, but the loop doesn't terminate there, it just runs in place. (You could see this yourself by adding cout << w << " " << k << endl; to the loop.) The conditional I think you want is:
while(w < N-1 || k > 0)

Fill an array diagonally

I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/

Thread 1: EXC_BAD_ACCESS (code=1, adress=0x0)

I'm making Dijikstra algorithm in c++ (XCode).
I'm getting exc_bad_access code=1 at that moment:
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
Whole code:
#include <iostream>
#include <fstream>
int min(int[]);
int **Graf;
int *Label;
int *Active;
int i, j, k;
int Start, N, M, V, Last;
using namespace std;
int min(int array[]) {
int min, k, min_pos = -1;
min = 32767;
for (k=0; k<M; k++) {
if (array[k] < min && Active[k] == 1){
min = array[k];
min_pos = k;
}
}
return min_pos;
}
int main(){
ifstream input ("input.txt");
input>>N>>M>>Start>>Last;
if (!input){
cout << "File not found" << endl;
return 1;
}
Graf = new int *[N];
for (i=0; i<N; i++){
Graf[i] = new int [N];
}
for (i=0; i<N; i++){
for (j=0; j<N; j++){
Graf[i][j]=0;
}
}
Label = new int [N];
Active = new int [N];
for (i=0; i<N; i++){
Label[i] = 0;
Active[i] = 0;
}
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
for (i=0; i<M; i++){
Label[i] = 32767;
Active[Start] = 1;
}
Label[Start] = 0;
i = Start;
do
{
for (j=0; j<N; j++)
if (Graf[i][j] != 0 && Label[j] > Label[i] + Graf[i][j]){
Active[j] = 1;
Label[j] = Label[i] + Graf[i][j];
}
Active[i] = 0;
i=min(Label);
}
while (i != -1);
cout << Label[Last] << endl;
return 0;
}
I'm looking for a mistake for some days, but can't find.
Input.txt
5 9 0 4
0 0 5 2 4 0
0 0 0 7 0 6
5 0 0 9 1 15
2 7 9 0 0 12
4 0 1 0 0 8
0 6 15 12 8 0
I believe your error is in your input file: you read i and j and use it to access your arrays, without verifying that these value are within expected boundaries.
For instance, if you improve your questioned code with an error message, like:
for (k=0; k<M; k++){
input>>i>>j>>V;
if (i>=N || j>=N) {
cerr << "Bad Input for k="<<k<<": "<<i<<","<<j<<","<<V<<endl;
continue;
}
//assert(i<N && j<N); variant if you don't care for the values
Graf[i][j] = V;
}
You'll get the following results:
Bad Input for k=3: 7,0,6
Bad Input for k=4: 5,0,0
Bad Input for k=5: 9,1,15
Bad Input for k=6: 2,7,9

Finding prime numbers in specific interval

Find prime numbers in specific interval in specific amount of test cases.
Example is below:
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
Notice the little space between the answer also.
here's my code:
#include <iostream>
#include <cmath>
void prime (int x, int y);
using namespace std;
int main()
{
int t, x[10], y[10];
cin >> t;
for (int i = 0; i < t; i++)
//for (int j = 0; j < t; j++)
cin >> x[i] >> y[i];
while (t > 0){
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
t--;
}
}
void prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++){
prime = true;
if (i % j == 0)
prime = false;
}
if (prime == true)
cout << i << endl;
}
cout << endl;
}
Here's the output I get when I use the same input.
1
2
3
5
7
10
3
5
1
2
3
5
7
10
What am I doing wrong?
You should move prime = true outside of the for loop. You are currently resetting it at every iteration. As far as the printing, you don't need that << endl when you print each line. You only need a space.
Since no one's pointed it out yet, if you're wondering why you're getting three sets of output instead of two...
while (t > 0){
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
t--;
}
might be better phrased as
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
(The outer loop is what gives you extra output.)