Skip to main content

Minimise Z (Coding Ninjas)


Problem statement

Given an array ‘A’. Your task is to choose any two different elements of the array say ‘X’ and ‘Y’, such that ‘Z’ is minimum possible. Where ‘Z’ = ‘X’ × ’Y’.


Return the minimum possible value of ‘Z’.


Example:
‘N’ = [5, 3, 9, 6, 3]

‘X’ can be chosen as 3(index 1) and ‘Y’ can be chosen as 3(index 4). 'Z' = 3*3 = 9.
No other combination can have a smaller value of ‘Z’.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= ‘T’ <= 10
2 <= ‘N’ <= 10^5
-10^4 <= ‘A[i]’ <= 10^4

Time Limit: 1 sec
Sample Input 1:
2
6
-6 10 -1 2 10 -1 
7
-7 5 -1 -4 -10 -8 10 
Sample Output 1:
-60
-100
Explanation of sample input 1:
For 1st Testcase :
‘X’ can be chosen as -6(index 0)  and ‘Y’ can be chosen as 10(index 1). 'Z' = -6*10 = -60.
No other combination can have a smaller value of ‘Z’.

For 2nd Testcase :
‘X’ can be chosen as -10(index 4) and ‘Y’ can be chosen as 10(index 6). 'Z' = -10*10 = -100.
No other combination can have a smaller value of ‘Z’.
Sample Input 2:
3
6
-5 -10 5 7 -8 -8 
6
-10 6 1 -6 7 5 
8
0 0 3 -8 -10 -7 4 -5 
Sample Output 2:
-70
-70
-40

Solution:
int minimiseZ (vector<int> &a){
    // Write your code here.
int mini=a[0],mini=INT_MAX;
int maxi=a[0],maxi=INT_MIN;

//Finding the minimum, second minimum, maximum and second maximum.
for(int i=1;i<a.size();i++){
if(a[i]<=mini){
smini=mini;
mini=a[i];
}
else if(a[i]<=smini)
smini=a[i];
if(a[i]>=maxi){
smaxi=mini;
mini=a[i];
}
else if (a[i]>=smaxi)
smaxi=a[i];
}
if(mini<0 && maxi>0)
return mini*maxi;
else if(mini>0)
return mini*smini;
else if(maxi<0)
return maxi*smaxi;
}

Comments

Popular posts from this blog

The Journey of Ram Mandir Ayodhya

  Introduction Welcome to this blog where we will explore the complete journey of the Ram Temple in Ayodhya, from its humble beginnings to its grand existence. Ayodhya holds a significant place in the religious and cultural tapestry of India, as it is believed to be the birthplace of Lord Ram, one of the most revered deities in Hinduism. The history of the Ram Temple is deeply intertwined with the socio-political landscape of India, with numerous debates and legal battles that have shaped its destiny. Let's delve into the rich history and significance of this sacred place. Ancient Origins According to Hindu mythology, Ayodhya is considered the birthplace of Lord Ram, the seventh avatar of Lord Vishnu. The ancient epic, Ramayana, narrates the story of Lord Ram's birth in Ayodhya and his journey to become a revered deity. The city of Ayodhya remained an important religious and cultural center throughout history, with various rulers and dynasties paying homage to Lord Ram and u...

Palindrome number (DSA)

Problem statement Check whether a given number   ’n’   is a palindrome number.   Example: Input: 'n' = 51415 Output: true Explanation: On reversing, 51415 gives 51415. Checking for Palindrome Numbers Palindrome numbers are numbers that read the same backward as forward. For example, 121 and 12321 are palindrome numbers. To check if a number is a palindrome, we can reverse the number and compare it with the original number. If they are the same, the number is a palindrome; otherwise, it is not. Using the concepts of extracting digits and reversing a number, we can efficiently check for palindrome numbers. bool  palindrome(int n) {      // Write your code here     int num= 0 ,temp;      for (int i=n; i> 0 ;i=i/ 10 )     {         temp=i% 10 ;         num=(num* 10 )+temp...

“Emerging AI: The Future of Technology Unveiled!”

  Today, we’re diving deep into the cutting-edge world of Artificial Intelligence — specifically, exploring the latest and most exciting developments in the field. Get ready to witness the wonders of Emerging AI as we unravel how it’s reshaping our world and what we can expect in the near future. If you’re as fascinated as I am about the potential of AI, don’t forget to hit that like button and subscribe, so you won’t miss any of our tech explorations. Let’s jump right in! AI-Powered Healthcare One of the most revolutionary applications of Emerging AI is in healthcare. From diagnosing diseases with unprecedented accuracy to assisting in surgeries, AI is transforming patient care. Imagine a world where AI-driven medical imaging can detect diseases at earlier stages, potentially saving countless lives. Today, we’ll witness some incredible AI-powered healthcare solutions that are making waves in the medical community. AI and Creative Industries AI’s capabilities are not limited to tec...