Geeks for Geeks - Array - Questions and Solution (Practice/Basic) - 1
Solutions :
Search an Element in an array
Search an Element in an array | Practice | GeeksforGeeks
Given an integer array and another integer element. The task is to find if the given element is present in array or…geeksforgeeks.org
for(int i=0;i<N; i++)
if( arr[i] == X)
return i;
return -1;
Binary Search | Practice | GeeksforGeeks
Given a sorted array of size N and an integer K, find the position(0-based indexing) at which K is present in the array…geeksforgeeks.org
class Solution {
public:
int binarysearch(int arr[], int n, int k) {
// code here
int i =0, j = n-1;
int m;
while(i<=j)
{
m=(i+j)/2;
if(arr[m] == k)
return m;
else if(arr[m] > k)
j= m-1;
else
i = m+1;
}
return -1;
}
};
Peak element
Peak element | Practice | GeeksforGeeks
Given an 0-indexed array of integers arr[] of size n, find its peak element and return it's index. An element is…geeksforgeeks.org
int peakElement(int arr[], int n)
{
// Your code here
int result=0 ;
for(int i=0;i<n-1;i++)
if (arr[i+1] > arr[i])
result = i+1;
return result;
}
class Solution
{
public:
int peakElement(int arr[], int n)
{
int s, e, m;
s=0;
e = n-1;
while(s<e)
{
m = s +(e-s)/2;
if (arr[m] > arr[m+1])
e=m;
else
s = m+1;
}
return s;
}
};
class Solution
{
public:
int peakElement(int arr[], int n)
{
int s, e, m;
s=0;
e = n-1;
while(s<e)
{
m = s +(e-s)/2;
if (arr[m] > arr[m+1])
e=m;
else
s = m+1;
}
return s;
}
};
Power of 2
Power of 2 | Practice | GeeksforGeeks
Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed…geeksforgeeks.org
class Solution{
public:
// Function to check if given number n is a power of two.
bool isPowerofTwo(long long n){
// Your code here
if (n == 0)
return false;
if(n == 1)
return true;
while(n != 1)
{
if(n%2!=0)
return false;
n = n / 2;
}
return true;
}
};
Union of two arrays
Union of two arrays | Practice | GeeksforGeeks
Given two arrays a[] and b[] of size n and m respectively. The task is to find the number of elements in the union…geeksforgeeks.org
class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
//code here
set<int> val;
for(int i=0;i<n;i++)
val.insert(a[i]);
for(int i=0;i<m;i++)
val.insert(b[i]);
return val.size();
}
};
class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
//code here
map<int, int> m1;
for(int i=0;i<n;i++)
m1[a[i]]++;
for(int i=0;i<m;i++)
m1[b[i]]++;
return m1.size();
}
};
Reverse a String
Reverse a String | Practice | GeeksforGeeks
You are given a string s. You need to reverse the string. Example 1: Input: s = Geeks Output: skeeG Example 2: Input: s…geeksforgeeks.org
string reverseWord(string str){
//Your code here
if (str =="")
return "";
return (reverseWord(str.substr(1, str.size())) + str[0] );
}
string reverseWord(string str){
//Your code here
// if (str =="")
// return "";
// return (reverseWord(str.substr(1, str.size())) + str[0] );
for(int i=0, j=str.size()-1; i <j ; i++, j--)
{
char ch = str[i];
str[i] = str[j];
str[j] = ch;
}
return str;
}
Check if two arrays are equal or not
Check if two arrays are equal or not | Practice | GeeksforGeeks
Given two arrays A and B of equal size N, the task is to find if given arrays are equal or not. Two arrays are said to…geeksforgeeks.org
class Solution{
public:
//Function to check if two arrays are equal or not.
bool check(vector<ll> A, vector<ll> B, int N) {
//code here
if (A.size()== B.size())
{
sort(A.begin(), A.end());
sort(B.begin(), B.end());
for(int i=0;i<A.size();i++)
{
if(A.at(i)!= B.at(i))
{
return 0;
}
}
return 1;
}
return 0;
}
};
Reverse array in groups
Reverse array in groups | Practice | GeeksforGeeks
Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K. Note: If at any instance…geeksforgeeks.org
class Solution{
public:
//Function to reverse every sub-array group of size k.
void reverseInGroups(vector<long long>& arr, int n, int k){
// code here
int i;
for(int i =0;i<n;i+=k)
{
if(i+k>n)
reverse(arr.begin()+i, arr.end());
else
reverse(arr.begin()+i, arr.begin()+i+k);
}
}
};
Array Subset of another array
Array Subset of another array | Practice | GeeksforGeeks
Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m, where both arrays may contain duplicate elements. The…geeksforgeeks.org
string isSubset(int a1[], int a2[], int n, int m) {
map<int, int> freq;
for(int i=0;i<n;i++)
freq[a1[i]]++;
for(int i=0;i<m;i++)
{
if(freq[a2[i]] > 0)
freq[a2[i]]--;
else
return "No";
}
return "Yes";
}
Cyclically rotate an array by one
Cyclically rotate an array by one | Practice | GeeksforGeeks
Given an array, rotate the array by one position in clock-wise direction. Example 1: Input: N = 5 A[] = {1, 2, 3, 4, 5}…geeksforgeeks.org
void rotate(int arr[], int n)
{
int temp = arr[n-1];
for(int i=n-1;i>0;i--)
arr[i] = arr[i-1];
arr[0]= temp;
}