Count permutations that produce positive result
Last Updated :
19 Apr, 2023
Given an array of digits of length n > 1, digits lies within range 0 to 9. We perform sequence of below three operations until we are done with all digits
- Select starting two digits and add ( + )
- Then next digit is subtracted ( – ) from result of above step.
- The result of above step is multiplied ( X ) with next digit.
We perform above sequence of operations linearly with remaining digits.
The task is to find how many permutations of given array that produce positive result after above operations.
For Example, consider input number[] = {1, 2, 3, 4, 5}. Let us consider a permutation 21345 to demonstrate sequence of operations.
- Add first two digits, result = 2+1 = 3
- Subtract next digit, result=result-3= 3-3 = 0
- Multiply next digit, result=result*4= 0*4 = 0
- Add next digit, result = result+5 = 0+5 = 5
- result = 5 which is positive so increment count by one
Examples:
Input : number[]="123"
Output: 4
// here we have all permutations
// 123 --> 1+2 -> 3-3 -> 0
// 132 --> 1+3 -> 4-2 -> 2 ( positive )
// 213 --> 2+1 -> 3-3 -> 0
// 231 --> 2+3 -> 5-1 -> 4 ( positive )
// 312 --> 3+1 -> 4-2 -> 2 ( positive )
// 321 --> 3+2 -> 5-1 -> 4 ( positive )
// total 4 permutations are giving positive result
Input : number[]="112"
Output: 2
// here we have all permutations possible
// 112 --> 1+1 -> 2-2 -> 0
// 121 --> 1+2 -> 3-1 -> 2 ( positive )
// 211 --> 2+1 -> 3-1 -> 2 ( positive )
Asked in : Morgan Stanley
We first generate all possible permutations of given digit array and perform given sequence of operations sequentially on each permutation and check for which permutation result is positive. Below code describes problem solution easily.
Note : We can generate all possible permutations either by using iterative method, see this article or we can use STL function next_permutation() function to generate it.
C++
#include<bits/stdc++.h>
using namespace std;
int countPositivePermutations( int number[], int n)
{
sort(number, number+n);
int count = 0;
do
{
int curr_result = number[0] + number[1];
int operation = 1;
for ( int i=2; i<n; i++)
{
switch (operation)
{
case 0:
curr_result += number[i];
break ;
case 1:
curr_result -= number[i];
break ;
case 2:
curr_result *= number[i];
break ;
}
operation = (operation + 1) % 3;
}
if (curr_result > 0)
count++;
} while (next_permutation(number, number+n));
return count;
}
int main()
{
int number[] = {1, 2, 3};
int n = sizeof (number)/ sizeof (number[0]);
cout << countPositivePermutations(number, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPositivePermutations( int number[],
int n)
{
Arrays.sort(number);
int count = 0 ;
do
{
int curr_result = number[ 0 ] + number[ 1 ];
int operation = 1 ;
for ( int i = 2 ; i < n; i++)
{
switch (operation)
{
case 0 :
curr_result += number[i];
break ;
case 1 :
curr_result -= number[i];
break ;
case 2 :
curr_result *= number[i];
break ;
}
operation = (operation + 1 ) % 3 ;
}
if (curr_result > 0 )
count++;
} while (next_permutation(number));
return count;
}
static boolean next_permutation( int [] p)
{
for ( int a = p.length - 2 ; a >= 0 ; --a)
if (p[a] < p[a + 1 ])
for ( int b = p.length - 1 ;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void main(String[] args)
{
int number[] = { 1 , 2 , 3 };
int n = number.length;
System.out.println(countPositivePermutations(number, n));
}
}
|
Python3
def countPositivePermutations(number, n):
number.sort()
count = 0 ;
while True :
curr_result = number[ 0 ] + number[ 1 ];
operation = 1 ;
for i in range ( 2 , n):
if operation = = 0 :
curr_result + = number[i];
else if operation = = 1 :
curr_result - = number[i];
else if operation = = 2 :
curr_result * = number[i];
operation = (operation + 1 ) % 3 ;
if (curr_result > 0 ):
count + = 1
if ( not next_permutation(number)):
break
return count;
def next_permutation(p):
for a in range ( len (p) - 2 , - 1 , - 1 ):
if (p[a] < p[a + 1 ]):
for b in range ( len (p) - 1 , - 1000000000 , - 1 ):
if (p[b] > p[a]):
t = p[a];
p[a] = p[b];
p[b] = t;
a + = 1
b = len (p) - 1
while (a < b):
t = p[a];
p[a] = p[b];
p[b] = t;
a + = 1
b - = 1
return True ;
return False ;
if __name__ = = '__main__' :
number = [ 1 , 2 , 3 ]
n = len (number)
print (countPositivePermutations(number, n));
|
C#
using System;
class GFG
{
static int countPositivePermutations( int []number,
int n)
{
Array.Sort(number);
int count = 0;
do
{
int curr_result = number[0] + number[1];
int operation = 1;
for ( int i = 2; i < n; i++)
{
switch (operation)
{
case 0:
curr_result += number[i];
break ;
case 1:
curr_result -= number[i];
break ;
case 2:
curr_result *= number[i];
break ;
}
operation = (operation + 1) % 3;
}
if (curr_result > 0)
count++;
} while (next_permutation(number));
return count;
}
static bool next_permutation( int [] p)
{
for ( int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for ( int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
static public void Main ()
{
int []number = {1, 2, 3};
int n = number.Length;
Console.Write(countPositivePermutations(number, n));
}
}
|
Javascript
<script>
function countPositivePermutations(number, n)
{
number.sort( function (a, b){ return a - b});
let count = 0;
do
{
let curr_result = number[0] + number[1];
let operation = 1;
for (let i = 2; i < n; i++)
{
switch (operation)
{
case 0:
curr_result += number[i];
break ;
case 1:
curr_result -= number[i];
break ;
case 2:
curr_result *= number[i];
break ;
}
operation = (operation + 1) % 3;
}
if (curr_result > 0)
count++;
} while (next_permutation(number));
return count;
}
function next_permutation(p)
{
for (let a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (let b = p.length - 1;; --b)
if (p[b] > p[a])
{
let t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
let number = [1, 2, 3];
let n = number.length;
document.write(countPositivePermutations(number, n));
</script>
|
Output:
4
Time Complexity: O(n*n!)
Auxiliary Space: O(1)
If you have better and optimized solution for this problem then please share in comments.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...