You are currently viewing Alex and His Array: Coding Challenge on TechGig with INR 1,50,000 in Prizes

Alex and His Array: Coding Challenge on TechGig with INR 1,50,000 in Prizes

Introduction:

In today’s blog post, we’ll dive into an intriguing array challenge that Alex, a bright student, is facing. He’s in a rush to solve this puzzle before heading to school, and he’s seeking our assistance. The challenge involves an array of integers, and Alex can perform a unique operation on it. Let’s explore the problem, understand the constraints, and find a solution together.

Problem Description:

Alex has been given an array, a1, a2, …, aN, of length N. He can perform a single operation in which he selects two indices, l and r, and reverses the signs of all the elements in the subarray [al, al+1, …, ar]. His goal is to maximize the sum of the array elements and determine the minimum number of operations required to achieve this maximum sum.

Input Format:

The input consists of two lines:

  1. The first line contains an integer N, representing the length of the array.
  2. The second line contains N space-separated integers, a1, a2, …, an, which are the array elements.

Constraints:

  • 1 ≤ N ≤ 2 * 10^5
  • -10^9 ≤ ai ≤ 10^9

Output Format:

Print two space-separated integers:

  1. The maximum possible sum of integers in the array.
  2. The minimum number of operations required to achieve this maximum sum.

Sample Problem and Solution:

Let’s take an example to understand the problem and solution better:

Sample Input:

4
-1 0 -2 -1

Sample Output:

4 1

Explanation:

To maximize the sum, we should select indices l = 0 and r = 3. After one operation, the array becomes: [1, 0, 2, 1]. The sum of the array elements is 1 + 0 + 2 + 1 = 4. Therefore, the maximum sum is 4, and the minimum number of operations required to achieve this sum is 1.

SOLUTION USING PYTHON:

n = int(input().strip())
arr = list(map(int, input().strip().split()))

# Initialize variables
max_sum = 0
operations = 0

for num in arr:
    max_sum += abs(num)  # Calculate the maximum sum

# Count the number of negative elements in the array
negative_count = sum(1 for num in arr if num < 0)

# If there are negative numbers, we need to perform operations to make them all positive
if negative_count % 2 != 0:
    # Find the minimum absolute value in the array
    min_abs = min(abs(num) for num in arr)
    # Subtract twice the minimum absolute value from the maximum sum
    max_sum -= 2 * min_abs

# Output the maximum sum and the number of operations
print(max_sum, 1 if negative_count % 2 != 0 else 0)

The above code passed only one Test cases.

ONLY FOR GIRLS. ClickHere.

Prizes at a Glance:

  1. 1st Prize: INR 1,00,000
    • The ultimate title of “Coding Champion” awaits the winner of the first prize. Claim your spot at the top and win INR 1,00,000!
  2. 2nd Prize: INR 75,000
    • The runner-up is also in for a treat! Secure the second position and walk away with an impressive prize of INR 75,000.
  3. 3rd Prize: INR 50,000
    • The third spot is nothing to scoff at. Grab the bronze medal and a prize of INR 50,000.
  4. 4th Prize: INR 25,000
    • Even the fourth position isn’t left empty-handed. Win INR 25,000 and prove your coding skills.
  5. International Coding Champion: INR 20,000
    • If you’re up for the international challenge, you could be crowned the “International Coding Champion” and win an impressive INR 20,000!
price

TechGig’s coding championship is your chance to shine in the coding world and be recognized for your exceptional skills. Don’t miss this opportunity to showcase your talent, compete with the best, and claim your share of the impressive prize pool.

Join us in this thrilling coding competition, where victory not only brings recognition but also substantial rewards. Get ready to code, compete, and conquer!

Stay tuned for more details and mark your calendars for the upcoming TechGig Coding Championship. It’s time to code your way to success!

Explore more on our site SixMedium.

Leave a Reply