IT’S MAGIC!

Sussutu is a world-renowned magician. And recently, he was blessed with the power to remove EXACTLY ONE element from an array.

Given, an array A (index starting from 0) with N elements. Now, Sussutu CAN remove only that element which makes the sum of ALL the remaining elements exactly divisible by 7.

Throughout his life, Sussutu was so busy with magic that he could never get along with maths. Your task is to help Sussutu find the first array index of the smallest element he CAN remove.

Example:

Input:  n = 5, a = {14,7,8,2,4}
Output: 1

Approach

C++

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

long long itsMagic(long long nlong long a[])
{
    long long sum = 0;
    for (long long i = 0i < ni++)
    {
        sum += a[i];
    }
    long long ans = INT_MAXindex = -1;
    for (long long i = 0i < ni++)
    {
        if ((sum - a[i]) % 7 == 0)
        {
            if (a[i] < ans)
            {
                ans = a[i];
                index = i;
            }
        }
    }
    return index;
}
int main()
{
    long long n = 5;

    long long a[n] = {147824};

    cout << itsMagic(na<< "\n";

    return 0;
}


Top Company Interview Questions

  1. Top Google Interview Questions
  2. Top Facebook Interview Questions
  3. Top Amazon Interview Questions
  4. Top Microsoft Interview Questions
  5. Top Apple Interview Questions

Top Links




No comments:

Post a Comment