Geeks for Geeks — Questions and Solution (Practice/School) — 1

Table of contents

Sum of Series

Sum of Series | Practice | GeeksforGeeks

Write a program to find the sum of the given series 1+2+3+ . . . . . .(N terms) Example 1: Input: N = 1 Output: 1…

www.geeksforgeeks.org

class Solution {
  public:
    long long seriesSum(int n) {
        // code here
        long long n1 = n;
        return (n1*(n1+1))/2;
    }
};

Find the median

Find the median | Practice | GeeksforGeeks

Given an array arr[] of N integers, calculate the median. NOTE: Return the floor value of the median. Example 1: Input…

www.geeksforgeeks.org

class Solution
{
public:
 public:
  int find_median(vector<int> v)
  {
      // Code here.
      sort(v.begin(),v.end());
      int l = v.size();
      if(l%2==0){
              return (v[(l/2)-1]+v[(l/2)])/2;
          }
          else {
              return v[l/2];
          }
  }
};

Check for Binary

Check for Binary | Practice | GeeksforGeeks

Given a non-empty sequence of characters str, return true if sequence is Binary, else return false Example 1: Input…

www.geeksforgeeks.org

#include <bits/stdc++.h>
using namespace std;

bool isBinary(string str);

// Driver program to test above functions
int main()
{
    string str;
    int t;
    scanf("%d\n", &t);
    while (t--)
    {
       cin >> str;
       cout << isBinary(str) << endl;
    }
    return 0;
}

// } Driver Code Ends


// Return true if str is binary, else false
bool isBinary(string str)
{
   // Your code here

    for(int i=0;i<str.size();i++){
        if (str[i] != '0' && str[i] != '1')
            return false;
    }
    return true;

}

Sum of Array

Sum of Array | Practice | GeeksforGeeks

Given an integer array arr[] of size n. The task is to find sum of it. Example 1: Input: n = 4 arr[] = {1, 2, 3, 4}…

www.geeksforgeeks.org

class Solution {
    int sum(int arr[], int n) {
        // code here
        int total=0;
        for(int i=0;i<n;i++)
            total += arr[i];
        return total;
    }
}

Print Elements of Array

Print Elements of Array | Practice | GeeksforGeeks

Given an array arr of size n, print all its elements space-separated. Note: You don't need to move to the next line…

www.geeksforgeeks.org

class Solution{
public:
    //Just print the space seperated array elements
 void printArray(int arr[], int n) {
     // code here
     for(int i=0;i<n;i++)
         cout<<arr[i]<<" ";
 }
};

Find Index

Find Index | Practice | GeeksforGeeks

Given an unsorted array arr[] of n integers and a key which is present in this array. You need to write a program to…

www.geeksforgeeks.org

class Solution
{
  public:
    vector<int> findIndex(int arr[], int n, int key)
    {
        //code here.
        vector <int>v1;
        for(int i=0;i<n;i++)
        {
            if (arr[i] == key){
                v1.push_back(i); break;
            }
        }
        for(int i=n;i>=0;i--)
        {
            if (arr[i] == key){
                v1.push_back(i); break;}
        }
        if (v1.size() == 0 )
            return {-1,-1};
        return v1;
    }

};

Palindrome

Palindrome | Practice | GeeksforGeeks

Given an integer, check whether it is a palindrome or not. Note : Strings which read the same backwards as forwards …

www.geeksforgeeks.org

class Solution
{
 public:
  string is_palindrome(int n)
  {
      // Code here.
      int rev = 0;
      int n1 = n;
      while(n != 0)
      {
          rev = rev *10 + (n %10);
          n = n /10;
      }
      if (rev == n1)
          return "Yes";
      else
          return "No";
  }
};

Reverse digits

Reverse digits | Practice | GeeksforGeeks

You are given an integer N, reverse the digits of given number N, ensuring that the reversed number has no leading…

www.geeksforgeeks.org

class Solution
{
 public:
  long long int reverse_digit(long long int n)
  {
      long long int rev = 0;
            int n1 = n;
            while(n != 0)
            {
                rev = rev *10 + (n %10);
                n = n /10;
            }
            return rev;
  }
};

GCD of two numbers

GCD of two numbers | Practice | GeeksforGeeks

Given two positive integers a and b, find GCD of a and b.

www.geeksforgeeks.org

class Solution {
  public:
    int gcd(int a, int b) {
        while(a != b){
            if( a > b){
                a = a - b;
            }
            else{
                b = b - a;
            }
        }
        return a;
    }
};

Another Approach

class Solution {
  public:
    int gcd(int a, int b) {
        if( b == 0){
            return a;
        }
        return gcd(b, a % b);
    }
};

Swap kth elements

Swap kth elements | Practice | GeeksforGeeks

Given an array arr of size n, swap the kth element from the beginning with kth element from the end.

www.geeksforgeeks.org

class Solution {
  public:
    void swapKth(int n, int k, vector<int> &arr) {
        int temp = arr[k-1];
        arr[k-1] = arr[n-k];
        arr[n-k] = temp;
    }
};

Convert String to LowerCase

Convert String to LowerCase | Practice | GeeksforGeeks

Given a string s. The task is to convert characters of string to lowercase. Example 1: Input: s = "ABCddE" Output…

www.geeksforgeeks.org

class Solution {
  public:
    string toLower(string s) {
        // string str = "";
        for(int i=0;i<s.size();i++)
            s[i] = tolower(s[i]);
        return s;
    }
};

Remove Spaces

Remove Spaces | Practice | GeeksforGeeks

Given a string, remove spaces from it. Example 1: Input: S = "geeks for geeks" Output: geeksforgeeks Explanation: All…

www.geeksforgeeks.org

class Solution
{
  public:
    string modify (string s)
    {
        //code here.
        string s1 ="";
        for(int i=0;i<s.size();i++)
        {
            if(s[i] != ' ')
                s1 = s1 + s[i];
        }
        return s1;
    }
};

C++ Array (print an element) | Set 2

C++ Array (print an element) | Set 2 | Practice | GeeksforGeeks

Given an array arr of n integers and an index key(0-based index). Your task is to return the element present at the…

www.geeksforgeeks.org

class Solution {
  public:
    int findElementAtIndex(int n, int key, vector<int> &arr) {
        // code here
        return arr[key];
    }
};

if-else (Decision Making)

if-else (Decision Making) | Practice | GeeksforGeeks

Given two integers, n and m. The task is to check the relation between n and m. Example 1: Input: n = 4 m = 8 Output…

www.geeksforgeeks.org

class Solution {
  public:
    string compareNM(int n, int m){
        // code here
        if (n < m)
            return "lesser";
        else if (n > m)
            return "greater";
        else
            return "equal";
    }
};

Perfect Arrays

Perfect Arrays | Practice | GeeksforGeeks

Given an array arr of size n and you have to tell whether the arr is perfect or not. An array is said to be perfect if…

www.geeksforgeeks.org

class Solution {
  public:
    bool isPerfect(int n, vector<int> &arr) {
        // code here
        for(int i=0, j=arr.size()-1; i<j;i++,j--)
        {
            if (arr[i] != arr[j])
                return false;
        }
        return true;
    }
};

Odd or Even

Odd or Even | Practice | GeeksforGeeks

Given a positive integer N, determine whether it is odd or even. Example 1: Input: N = 1 Output: odd Explanation: The…

www.geeksforgeeks.org

class Solution{   
public:
    string oddEven(int N){
        // code here 
        if(N % 2 ==0)
            return "even";
        else
            return "odd";
    }
};