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

Reverse Integer (DSA)

 #dsa #leetcode #problem_solving Given a signed 32-bit integer  x , return  x  with its digits reversed . If reversing  x  causes the value to go outside the signed 32-bit integer range  [-2 31 , 2 31 - 1] , then return  0 . Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: Input: x = 120 Output: 21 class Solution { public:     int reverse ( int x ) {         int num= 0 , temp;     for ( int i=x; i!= 0 ; i=i/ 10 )     { temp=i% 10 ;         if ((num > INT_MAX/ 10 )|| (num < INT_MIN/ 10 ))         return 0 ;       num= (num* 10 ) + temp;     }     return num;     } };

What is C++ STL?

 One of the most widely used high-level programming languages is C++. Developers have been using it for a long time, and because of its quicker execution time, all programmers have always enjoyed it, especially competitive programmers. One of C++'s special features that sets it apart from all other programming languages is STL. The term "STL" refers to the standard template library , which is full of pre-defined templates for classes and containers. This makes it very simple for programmers or developers to implement various data structures without having to write all of the code themselves or worry about space-time complexities. If you go a bit further into STL, you will need to grasp the workings of templates, which are among the most powerful tools available for the C++ programming language.

Recursion: Solving Problems through Recursive Functions

  Problem 1: Print Your Name n Times Let's start with a simple problem: printing your name n times using recursion. The generic way to achieve this would be to run a for loop and print your name n times. However, in this case, we want to use recursion. To solve this problem, we will define a recursive function called f(i, n) . The function takes two parameters: i , which represents the current iteration, and n , which represents the total number of times to print your name. We start by taking the input of n from the user. Then, we call the function f(0, n) , which will recursively print your name n times. Here's how the function works: Check if i is greater than n . If it is, return. Print your name. Call the function f(i + 1, n) to recursively print your name n times. By following these steps, we can easily print your name n times using recursion. Problem 2: Print Numbers from 1 to n Next, let's solve a problem where we need to print numbers from 1 to n in...