Arrays Introduction

Problem Statement

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Declaration:Movie Fifty Shades Darker (2017)

int arr[10]; //Declares an array named arr of size 10, i.e; you can store 10  integers.

Accessing elements of an array:

Indexing in arrays starts from 0.So the first element is stored at arr[0],the second element at arr[1]...arr[9]

You’ll be an given array of N integers and you have to print the integers in the reverse order.

Input Format

The first line of the input contains N,where N is the number of integers.The next line contains N integers separated by a space.

Constraints

1<=N<=1000

1<=Ai<=10000, where Ai is the ith integer in the array.

Output Format

Print the N integers of the array in the reverse order in a single line separated by a space.

Sample Input

4
1 4 3 2

Sample Output

2 3 4 1

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,i,a[max];
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=n-1;i>=0;i--)
        cout<<a[i]<<" ";
    return 0;
}


Input and Output

Problem Statement

For any program that we write the basic things that we need to do is take the input and print the expected output.

In C++ you can take the input using cin and print the output using cout.Unlike C where you need the format specifier in printf and scanf, here you can use cin and cout.

Taking Input:

If you want to input a number: cin>>n ,  where n is the number.
If you want to input a number and a string: cin>>n>>s, where s is the string.

Printing output:

If you want to output a single number: cout<<n
If you want to output a number and a string separated by a new line: cout<<n<<endl<<s (where endl moves the pinter to the new line and then string gets printed.)

Take three numbers as inputs and print the sum of the three numbers.

Input Format

The first line of the input contains three integers A,B and C.

1A,B,C1000

Output Format

In a single line print the sum of the three numbers.

Sample Input

1 2 7

Sample Output

10

Explanation

Sum of the three numbers 1,2 and 7 is 10.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a+b+c;
    return 0;
}

Flipping bits

Problem Statement

You will be given a list of 32 bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. unset bits must be set, and set bits must be unset).

Input Format

The first line of the input contains the list size T, which is followed by T lines, each line having an integer from the list.

Constraints

1T100
0integer<232

Output Format

Output one line per element from the list with the requested result.

Sample Input

3 
2147483647 
1 
0

Sample Output

2147483648 
4294967294 
4294967295

Explanation

Take 1 for example, as unsigned 32-bits is 00000000000000000000000000000001 and doing the flipping we get 11111111111111111111111111111110 which in turn is 4294967294.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t,n;
    unsigned int a;
    cin>>t;
    while(t--)
    {
        cin>>a;
        cout<<~a<<endl;      
    }
    return 0;
    
}


Funny String

Problem Statement

Suppose you have a string S which has length N and is indexed from 0 to N1. String R is the reverse of the string S. The string S is funny if the condition |SiSi1|=|RiRi1| is true for every i from 1 to N1.

(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)

Input Format

First line of the input will contain an integer T. T testcases follow. Each of the next T lines contains one string S.

Constraints

  • 1<=T<=10
  • 2<=length of S<=10000

Output Format

For each string, print Funny or Not Funny in separate lines.

Sample Input

2
acxz
bcxz

Sample Output

Funny
Not Funny

Explanation

Consider the 1st testcase acxz

here

|c-a| = |x-z| = 2
|x-c| = |c-x| = 21
|z-x| = |a-c| = 2

Hence Funny.

Consider the 2nd testcase bcxz

here

|c-b| != |x-z|

Hence Not Funny.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int t,count;
    string s,r;
    cin>>t;
    while(t--)
    {
        count=0;
        r.clear();
        s.clear();
        cin>>s;
        r=s;
        reverse(r.begin(),r.end());
        for(int i=1;i<s.length();i++)
        {
            if(abs(s[i]-s[i-1])==abs(r[i]-r[i-1]))
            {
                count++;
            }
        }
        if(count==(s.length()-1))
            cout<<"Funny"<<endl;
        else cout<<"Not Funny"<<endl;
    }
    return 0;
}


Pangrams

Problem Statement

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)

After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams.

Given a sentence s, tell Roy if it is a pangram or not.

Input Format Input consists of a line containing s.

Constraints
Length of s can be at most 103 (1|s|103) and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.

Output Format Output a line containing pangram if s is a pangram, otherwise output not pangram.

Sample Input #1

We promptly judged antique ivory buckles for the next prize    

Sample Output #1

pangram

Sample Input #2

We promptly judged antique ivory buckles for the prize    

Sample Output #2

not pangram

Explanation
In the first test case, the answer is pangram because the sentence contains all the letters of the English alphabet.

Solution in Python:

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
print 'pangram' if set(raw_input().lower().replace(' ', '')) == set('abcdefghijklmnopqrstuvwxyz') else 'not pangram'

Manasa and Stones

Problem Statement

Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of either a or b. Legend has it that there is a treasure trove at the end of the trail and if Manasa can guess the value of the last stone, the treasure would be hers. Given that the number on the first stone was 0, find all the possible values for the number on the last stone.

Note: The numbers on the stones are in increasing order.

Input Format
The first line contains an integer T, i.e. the number of test cases. T test cases follow; each has 3 lines. The first line contains n (the number of stones). The second line contains a, and the third line contains b.

Output Format
Space-separated list of numbers which are the possible values of the last stone in increasing order.

Constraints
1T10
1n,a,b103

Sample Input

2
3 
1
2
4
10
100

Sample Output

2 3 4 
30 120 210 300 

Explanation

All possible series for the first test case are given below:

  1. 0,1,2
  2. 0,1,3
  3. 0,2,3
  4. 0,2,4

Hence the answer 2 3 4.

Series with different number of final steps for second test case are the following:

  1. 0, 10, 20, 30
  2. 0, 10, 20, 120
  3. 0, 10, 110, 120
  4. 0, 10, 110, 210
  5. 0, 100, 110, 120
  6. 0, 100, 110, 210
  7. 0, 100, 200, 210
  8. 0, 100, 200, 300

Hence the answer 30 120 210 300.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t,n,a,b,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>a>>b;
        if(b<a){
            int temp=a;
            a=b;
            b=temp;
            
        }
        //cout<<a<<" "<<b<<endl;
        if(a==b)
        {
            cout<<(n-1)*a<<endl;
            
        }
        else 
        {
            for(i=0;i<=n-1;i++)
            {
                cout<<(n-1-i)*a+(i)*b<<" ";
            
            }
        cout<<endl;
        }   
    }
    return 0;
}