Array Questions Part — 3(CPP)

Table of contents

Why is Melody so chocolaty?

Why is Melody so chocolaty? | Practice | GeeksforGeeks

Chunky gets happy by eating Melody. Given an array of N elements, each element represents happiness chunky get by…

www.geeksforgeeks.org

int max_adjacent_sum(int arr[], int n)
{
    // Complete the function
    int max = arr[0] + arr[1];
    for(int i=1;i<n-1;i++)
    {
        if(max < (arr[i]+ arr[i+1]))
            max = arr[i] + arr[i+1];
    }
    return max;
}

Sum of distinct elements

Sum of distinct elements | Practice | GeeksforGeeks

You are given an array Arr of size N. Find the sum of distinct elements in an array. Example 1: Input: N = 5 Arr[] =…

www.geeksforgeeks.org

class Solution{
public: 
 // Find the sum of all non-repeated elements
 // in an array
 int findSum(int arr[], int n) {
     // code here
     sort(arr, arr+n);
     int sum = 0;
     for (int i=0;i<n;i++)
     {
         if (arr[i] != arr[i+1])
             sum += arr[i];

     }
     return sum;
 }
};

Using Unordered_map STL

class Solution{
public: 
 // Find the sum of all non-repeated elements
 // in an array
 int findSum(int arr[], int n) {
     // code here
     unordered_map<int, int> m1;
     int sum=0;
     for(int i=0;i<n;i++)
     {
         auto search =m1.find(arr[i]);
         if ( search != m1.end())
             m1[arr[i]]++;
         else
             m1[arr[i]] = 1;

     }
     for(auto i : m1)
         sum += i.first;
     return sum;
 }
};

Using set STL

class Solution{
public: 
 // Find the sum of all non-repeated elements
 // in an array
 int findSum(int arr[], int n) {
     set<int>s;
        for(int i=0;i<n;i++)
        {
            s.insert(arr[i]);
        }

        return accumulate(s.begin(),s.end(),0);
 }
};

Value equal to index value

Value equal to index value | Practice | GeeksforGeeks

Given an array arr. Your task is to find the elements whose value is equal to that of its index value ( Consider…

www.geeksforgeeks.org

class Solution{
public:

 vector<int> valueEqualToIndex(int arr[], int n) {
     // code here
     vector<int> temp;
     for(int i=0;i<n;i++)
     {
         if( i+1 == arr[i])
             temp.push_back(arr[i]);
     }
     return temp;
 }
};

Missing number in shuffled array

Missing number in shuffled array | Practice | GeeksforGeeks

Given an array A of size N. The contents of A are copied into another array B and numbers are shuffled. Also, one…

www.geeksforgeeks.org

class Solution{
    public:
    long long findMissing(long long a[], long long b[], int n)
    {
        long long int sum = 0 ;
        for(int i=0;i<n;i++)
            sum += a[i];
        for(int i=0;i<n-1;i++)
            sum -= b[i];
        return sum;
    }
};

Check Equal Arrays

Check Equal Arrays | Practice | GeeksforGeeks

Given two arrays arr1 and arr2 of equal size, the task is to find whether the given arrays are equal. Two arrays are…

www.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;
    }
};

Binary Array Sorting

Binary Array Sorting | Practice | GeeksforGeeks

Given a binary array A[] of size N. The task is to arrange the array in increasing order. Note: The binary array…

www.geeksforgeeks.org

class Solution{
  public:

    // A[]: input array
    // N: input array
    //Function to sort the binary array.
    void binSort(int arr[], int N)
    {
       //Your code here
       int c0=0, c1=0;
       for(int i=0;i<N;i++){
            if(arr[i] == 0)
                c0++;
            else if(arr[i]==1)
                c1++;
       }
       for(int i=0;i<c0;i++)
            arr[i] =0;
       for(int i=c0;i<c0+c1;i++)
            arr[i] =1;

    }
};

Binary Search

Binary Search | Practice | GeeksforGeeks

Given a sorted array arr and an integer k, find the position(0-based indexing) at which k is present in the array using…

www.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…

www.geeksforgeeks.org

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;
    }
};

or

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;
}

Array Search

Array Search | Practice | GeeksforGeeks

Given an array, arr of n integers, and an integer element x, find whether element x is present in the array. Return the…

www.geeksforgeeks.org

class Solution {
  public:
    // Function to search x in arr
    // arr: input array
    // X: element to be searched for
    int search(vector<int>& arr, int x) {

        // Your code here
        for(int i=0;i<arr.size(); i++)
            if( arr[i] == x)
                return i;
        return -1;
    }
};

Array Subset

Array Subset | 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…

www.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";
}

Reverse array in groups

Reverse array in groups | Practice | GeeksforGeeks

Given an array arr of positive integers. Reverse every sub-array group of size k. Note: If at any instance, k is…

www.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);
        }
    }
};