Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Array Modification problem solution

YASH PAL, 31 July 2024
In this HackerEarth Array Modification problem solution, You are given an array A of N integers. For every index i of the array A, you need to select an index j such that j != i and the product of A[i] and A[j] is maximum for that i. Finally, you need to print the sum of A[i] * A[j] for all the indices i. Since this sum can be large, you need to print it to the modulo 10^9 + 7.
Note: Please refer to the sample explanation section.
HackerEarth Array Modification problem solution

HackerEarth Array Modification problem solution.

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){ a%=m;LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
LL a[100001];
LL ans[100001];int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
assert(cin >> n);
assert(n >= 1 && n <= 100000);
for(int i = 1; i <= n; i++) {
assert(cin >> a[i]);
assert(a[i] >= -1000000000000000000LL && a[i] <= 1000000000000000000LL);
}
vector<LL> positive, negative;
for(int i = 1; i <= n; i++) {
if(a[i] >= 0) {
positive.push_back(a[i]);
}
else {
negative.push_back(a[i]);
}
}
sort(positive.begin() , positive.end());
sort(negative.begin(), negative.end(), greater<LL>());
int positive_cnt = positive.size();
int negative_cnt = negative.size();
for(int i = 1; i <= n; i++) {
if(a[i] == 0) {
ans[i] = 0;
}
else if(a[i] < 0) {
if(negative.size() == 1) {
ans[i] = (a[i] % M) * (positive[0] % M);
}
else{
if(negative.back() == a[i])
ans[i] = (a[i] % M) * (negative[negative_cnt - 2] % M);
else
ans[i] = (a[i] % M) * (negative.back() % M);
}
}
else {
if(positive.size() == 1) {
ans[i] = (a[i] % M) * (negative[0] % M);
}
else{
if(positive.back() == a[i])
ans[i] = (a[i] % M) * (positive[positive_cnt - 2] % M);
else
ans[i] = (a[i] % M) * (positive.back() % M);
}
}
ans[i] %= M;
if(ans[i] < M)
ans[i] += M;
}
LL final_answer = 0;
for(int i = 1; i <= n; i++) {
final_answer += ans[i];
final_answer %= M;
}
cout << final_answer;
}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes