Find Total Imbalance Amazon OA 2023 Solution

Want to solve Find Total Imbalance Amazon OA 2023?, if yes then this article is for you.

We have research and collected a bundle of questions which was asked in Amazon OA in the year of 2022 and 2023. Today, we are going to see another problem from amazon oa called Find Total Imbalance which was asked in 2022, this questions might be still active in some of the Amazon OA so save all the solutions and lets see how can we solve this problem.

Find Total Imbalance Amazon OA 2023 Solution

Given an array ranks of ranks of students in a school. All students need to be split into groups k. Find the total ‘imbalance’ of all groups. An imbalance of a group can be found as :

  1. Sorting each group in the order of their ranks.
  2. A group contributes to imbalance if any 2 students in the sorted array have a rank difference of more than 1.

Find the total sum of imbalance of all such groups.

This is the example that was given :
[4,1,3,2]
[1] contributes 0 to imbalance
[2] contributes 0 to imbalance
[3] contributes 0 to imbalance
[4] contributes 0 to imbalance
[4,1] contributes 1 to imbalance
[4,3] contributes 0 to imbalance
[4,2] contributes 1 to imbalance
[4,1,3,2] contributes 0 to imbalance
[1,3] contributes 1 to imbalance
[1,2] contributes 0 to imbalance
[3,2] contributes 0 to imbalance
Answer = 1 + 1 + 1 = 3

Looped over the array to find subarrays and then loop over them to check condition. Passed 8 out of 14 test cases. TLE for rest.

Related: Google Online Assessment 2022 Questions List

Find Total Imbalance Amazon OA 2022
Find Total Imbalance Amazon OA 2022

SOLUTION

Program: Find Total Imbalance Amazon OA Solution in Java

public static long imbalance(List<Integer> rank) {
	long imbalance = 0;
	int r = 0;
	TreeSet<Integer> set = new TreeSet<>();
	while(r < rank.size()-1) {
		set.clear();
		set.add(rank.get(r));
		long curImbalance = 0;
		for(int i=r+1; i<rank.size(); i++) {
			Integer e = rank.get(i);
			set.add(e);
			Integer f = set.lower(e);
			Integer c = set.higher(e);
			
			if(f == null) { // added at beginning
				curImbalance += (((c - e) > 1) ? 1 : 0);
			}
			else if(c == null) {// added at end
				curImbalance += (((e - f) > 1) ? 1 : 0);
			}
			else {
				curImbalance += (c - f) > 1 ? -1 : 0;
				curImbalance += (((c - e) > 1) ? 1 : 0);
				curImbalance += (((e - f) > 1) ? 1 : 0);
			}
			imbalance += curImbalance;
		}
		r++;
	}
	return imbalance;
}

Program: Find Total Imbalance Amazon OA Solution in Python

result = 0
ranks = sorted(ranks)
num_of_ranks = len(ranks)

    for index in range(num_of_ranks - 1):
        if ranks[index + 1] - ranks[index] > 1:
            result += num_of_ranks - index - 1
        elif ranks[index + 1] - ranks[index] == 1:
            result += num_of_ranks - index - 2

    return result

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Valid Discount Coupons Amazon OA 2023 Solution
  13. Count Maximum Teams Amazon OA 2023
  14. Minimum Coin Flips Amazon OA 2023
  15. Max Average Stock Price Amazon OA 2023 Solution
  16. Robot Bounded In Circle Amazon OA 2023
  17. Shopping Options Amazon OA 2023 Solution
  18. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  19. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  20. Slowest Key Amazon OA 2023 Solution
  21. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  22. Split String Into Unique Primes Amazon OA 2023 Solution
  23. Storage Optimization Amazon OA 2023 Solution
  24. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  25. Autoscale Policy Utilization Check Amazon OA 2023
  26. Optimal Utilization Solution Amazon OA 2023
  27. Merge Two Sorted Lists Solution Amazon OA 2023
  28. Two Sum Unique Pairs Solution Amazon OA 2023
  29. Amazon Music Pairs Amazon OA 2023 Solution
  30. Class Grouping Amazon OA 2023 Solution
  31. Find Max products Amazon OA 2023 Solution
  32. Get encrypted number Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution