Geeks for Geeks — Questions and Solution (Practice/School) — 2
Smaller and Larger
Smaller and Larger | Practice | GeeksforGeeks
Given a sorted array Arr of size N and a value X, find the number of array elements less than or equal to X and…
class Solution{
public:
vector<int> getMoreAndLess(int arr[], int n, int x) {
int c1=0, c2=0;
for(int i=0;i<n;i++)
if (x >=arr[i])
c1++;
for(int i=n-1;i>=0;i--)
if (x <=arr[i])
c2++;
return {c1,c2};
}
};
C++ if-else (Decision Making)
C++ if-else (Decision Making) | Practice | GeeksforGeeks
Given an integer N. Your task is to check if the integer is greater than, less than or equal to 5. If the integer is…
class Solution{
public:
string compareFive(int n){
// code here
if (n > 5)
return "Greater than 5";
else if (n < 5)
return "Less than 5";
else
return "Equal to 5";
}
};
Sum of Digit is Pallindrome or not
Sum of Digit is Pallindrome or not | Practice | GeeksforGeeks
Given a number n.Find if the digit sum(or sum of digits) of n is a Palindrome number or not. Note: A Palindrome number…
class Solution {
public:
int isDigitSumPalindrome(int n) {
// code here
int sum = 0, n2, rev =0;
while(n>0)
{
sum += (n %10);
n = n/10;
}
n2 = sum;
while(sum>0)
{
rev = rev*10 +(sum%10);
sum /= 10;
}
if (n2 == rev)
return 1;
else
return 0;
}
};
Addition of Two Numbers
Addition of Two Numbers | Practice | GeeksforGeeks
Given two numbers A and B. Your task is to return the sum of A and B. Example 1: Input: A = 1, B = 2 Output: 3…
class Solution{
public:
int addition(int A, int B){
// code here
return (A + B);
}
};
Series AP
Series AP | Practice | GeeksforGeeks
Given the first 2 terms a1 and a2 of an Arithmetic Series.Find the nth term of the series. Example 1: Input: a1 = 2 a2…
class Solution {
public:
int nthTermOfAP(int a1, int a2, int n) {
if(n == 1)
return a1;
int sum=a1;
for(int i =2 ; i<=n ;i++)
sum = sum + (a2-a1);
return sum ;
}
};
Print 1 to n without using loops
Print 1 to n without using loops | Practice | GeeksforGeeks
You are given an integer N. Print numbers from 1 to N without the help of loops. Example 1: Input: N = 5 Output: 1 2 3…
class Solution
{
public:
void printTillN(int N)
{
// Write Your Code here
if(N==0)
return;
else
{
printTillN(N-1);
cout<<N<<" ";
}
}
};
C++ Input / Output
C++ Input / Output | Practice | GeeksforGeeks
Given two integers A and B. Your task is to return their product. Example 1: Input: A = 1, B = 2 Output: 2 Explanation…
class Solution{
public:
int multiplication(int A, int B){
return (A * B);
}
};
At least two greater elements
At least two greater elements | Practice | GeeksforGeeks
Given an array of N distinct elements, the task is to find all elements in array except two greatest elements in sorted…
vector<int> findElements(int a[], int n)
{
sort(a , a+n);
vector <int> v1;
for(int i=0;i<n-2;i++)
v1.push_back(a[i]);
return v1;
}
Multiplication Table
Multiplication Table | Practice | GeeksforGeeks
Create the multiplication table of a given number N and return the table as an array. Example 1: Input: N = 9 Output: 9…
class Solution
{
public:
vector<int> getTable(int N)
{
vector <int> v1;
for (int i=1;i<=10;i++)
v1.push_back(i*N);
return v1;
}
};
Swap two numbers
Swap two numbers | Practice | GeeksforGeeks
Swap given two numbers and print them. (Try to do it without a temporary variable.) and return it. Example 1: Input: a…
class Solution{
public:
pair<int, int> get(int a, int b){
//complete the function here
return pair<int,int>({b,a});
}
};