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 n, long long a[]){long long sum = 0;for (long long i = 0; i < n; i++){sum += a[i];}long long ans = INT_MAX, index = -1;for (long long i = 0; i < n; i++){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] = {14, 7, 8, 2, 4};cout << itsMagic(n, a) << "\n";return 0;}
Top Company Interview Questions
- Top Google Interview Questions
- Top Facebook Interview Questions
- Top Amazon Interview Questions
- Top Microsoft Interview Questions
- Top Apple Interview Questions
Top Links
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment