Skip to main content

Posts

Showing posts with the label c

Input and Output Hackerrank Solution in C++

  Objective In this challenge, we practice reading input from stdin and printing output to stdout. In C++, you can read a single whitespace-separated token of input using  cin , and print output to stdout using  cout . For example, let's say we declare the following variables: string s ; int n ; and we want to use  cin  to read the input "High 5" from stdin. We can do this with the following code: cin >> s >> n ; This reads the first word ("High") from stdin and saves it as string  , then reads the second word (" ") from stdin and saves it as integer  . If we want to print these values to stdout, separated by a space, we write the following code: cout << s << " " << n << endl ; This code prints the contents of string  , a single space ( ), then the integer  . We end our line of output with a newline using  endl . This results in the following output: High 5 Task Read   numbe...

Say Hello, World! With CPP - Hacker Rank Solution

  Problem Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We're starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello, World!  to stdout. Input Format : You do not need to read any input in this challenge. Output Format : print the string Hello, World! Solution : #include   < iostream > #include   < cstdio > using   namespace  std; int  main() {      // code     cout<< "Hello, World!" <<endl;      return   0 ; } Video Solution : -

Variable Sized Arrays in C++ - Hacker Rank Solution

  Problem Consider an  -element array,  , where each index   in the array contains a reference to an array of   integers (where the value of   varies from array to array). See the  Explanation  section below for a diagram. Given  , you must answer   queries. Each query is in the format  i j , where   denotes an index in array   and   denotes an index in the array located at  . For each query, find and print the value of element   in the array at location   on a new line. Click  here  to know more about how to create variable sized arrays in C++. Input Format The first line contains two space-separated integers denoting the respective values of n (the number of variable-length arrays) and q (the number of queries). Each line i of the n subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the k-element ...