Recover the Arrays

Sort by

recency

|

34 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] file = new int[n];
            for(int file_i=0; file_i < n; file_i++){
                file[file_i] = in.nextInt();
            }
            int j=0;
            int count=0;
            while(j<file.length){
                
                for(int i = file[j];i>=0;i--){
                    j++;
                }
                count++;
            }
            System.out.println(count);
        }
    }
    
  • + 0 comments
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    
    int main(){
        int n; 
        scanf("%d",&n);
        int *file = malloc(sizeof(int) * n);
        for(int file_i = 0; file_i < n; file_i++){
           scanf("%d",&file[file_i]);
        }
        int c = 0;
        for(int i = 0; i < n; i++){
            i += file[i];
            c++;
        }
        printf("%d",c);
        return 0;
    }
    
  • + 0 comments

    Hi guys, this was my first chalange in hackerrank and I cannot understand the results, could you help me? That's my code:

    process.stdin.resume();
    process.stdin.setEncoding('ascii');
    
    var input_stdin = "";
    var input_stdin_array = "";
    var input_currentline = 0;
    
    process.stdin.on('data', function (data) {
        input_stdin += data;
    });
    
    process.stdin.on('end', function () {
        input_stdin_array = input_stdin.split("\n");
        main();    
    });
    
    function readLine() {
        return input_stdin_array[input_currentline++];
    }
    
    /////////////// ignore above this line ////////////////////
    
    function main() {
        var n = parseInt(readLine());
        file = readLine().split(' ');
        file = file.map(Number);
        
        console.log((function recourse(n, arr){
                if (arr.length <= 0)
                return n;
            return recourse(++n, arr.slice(arr.shift()));
        })(0, file));
        
        //  Print the number of arrays defined in 'file' to STDOUT.
    
    }
    

    I've only made the console.log part, the rest was the chalange boilerplate for JS.

    console.log((function recourse(n, arr){
                if (arr.length <= 0)
                return n;
            return recourse(++n, arr.slice(arr.shift()));
        })(0, file));
    

    Well, this works for me and a lot of test cases, the #4 seens to be wrong, but I've reproduce here in my computer and get the right result (1001)! Some other tests have been aborted... Someone could point my error or explain why this code fail the tests?

    Thanks!! Renan

  • + 0 comments
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main(){
        int n,a[100000];
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        int x=a[0], count=0, y=0;
        while(y<n)
            {
            y+=(x+1);
            x=a[y];
            count++;
        }
        cout<<count;
        return 0;
    }
    
  • [deleted]
    + 0 comments

    php solution

    <?php
    
    $handle = fopen ("php://stdin","r");
    fscanf($handle,"%d",$n);
    $file = array_map("intval", explode(" ",fgets($handle)));
    
    $i = 0;
    $s = 0;
    while($i < $n) {
        $i += ($file[$i] + 1);
        $s++;
    }
    echo $s;
    
    ?>