0

Consider an n element array,a , where each index i in the array contains a reference to an array of ki integers (where the value of varies from array to array). See the Explanation section below for a diagram.

Given a, you must answer q queries. Each query is in the format i j, where i denotes an index in array and j denotes an index in the array located at a[i]. For each query, find and print the value of element j in the array at location a[i] on a new line.

Problem is described on hackerrank here i don't want the solution, i have solved it successfully using double pointers. please explain why this solution is wrong. it works fine for its sample test case, and many of custom inputs i have tried,but it fails for large inputs maybe.

works successfully for sample input .. ie

2 2

3 1 5 4

5 1 2 8 9 3

0 1

1 3

even works for other inputs in this range. i have tried to use long int as well everywher (in correct form) ,but no use. still getting segmentation fault.

#include<iostream>
using namespace std;
int main() {
    
    int temp1,temp2,num,queries,numofarrs;

    scanf("%d %d",&numofarrs,&queries);
    int matrix[100000][100000]={0};
    //printf("%d",matrix[1][200]);
    
    for(int i=0;i<numofarrs;i++){
         
        scanf("%d",&num);
        for(int j=0;j<num;j++){
            scanf("%d",&matrix[i][j]);

        }
        //printf("array %d done",i);
    }
   
    for(int i=0;i<queries;i++){
        
        scanf("%d %d",&temp1,&temp2);
        printf("%d",matrix[temp1][temp2]);
        printf("\n");
    }
    
    return 0;
}

Melebius
  • 6,183
  • 4
  • 39
  • 52
rushikesh chaskar
  • 642
  • 1
  • 7
  • 12

0 Answers0