Open In App

Python – Find all pairs of consecutive odd positive integer smaller than a with sum greater than b

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers a and b, the task is to write a program in python to find all pairs of consecutive odd numbers which are smaller than the first number a and their sum should be greater than the second number b

Examples:

Input:
a = 60
b = 100
Output:
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59

Input:
a = 20
b = 200
Output:
None

Approach:

Two numbers are given and then check if they are a positive integer and checked for the first number to be greater than the half of the second number. Then we will check for odd positive integer and assigned in a variable a. In the while statement, pairs of the odd consecutive integers are found and printed.

Example 1:

Python3




# input first and second number
a = 60
b = 100
 
print('a =', a)
print('b =', b)
 
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
 
    # to ensure value in firstNum variable
    # must be odd positive integer
    if(a % 2 == 0):
        a -= 1
    else:
        a -= 2
 
    b //= 2
    print("Pairs of consecutive number are:")
 
    # find the pairs of odd
    # consecutive positive integer
    while(b <= a):
        if(b % 2 != 0):
            x = b
            if(x + 2 <= a):
                print(x, ',', x+2)
 
        b += 1
else:
  print("None")


Output:

a = 60
b = 100
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59

Time complexity: O(n)
Auxiliary space: O(1)

Example 2:

Python3




# input first and second number
a = 20
b = 200
 
print('a =', a)
print('b =', b)
 
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
 
    # to ensure value in firstNum variable
    # must be odd positive integer
    if(a % 2 == 0):
        a -= 1
    else:
        a -= 2
 
    b //= 2
    print("Pairs of consecutive number are:")
 
    # find the pairs of odd
    # consecutive positive integer
    while(b <= a):
        if(b % 2 != 0):
            x = b
            if(x + 2 <= a):
                print(x, ',', x+2)
                
 
        b += 1
else:
    print("None")


Output:

a = 20
b = 200
None

Complexity analysis for all examples : 

Time complexity: O(n) where n is the length of the string.
Auxiliary Space: O(1) 

Iterative Search for Consecutive Odd Integer Pairs

Steps:

  1. Iterate through all odd integers from 1 to a-1.
  2. For each odd integer i, check if the next odd integer i+2 is also less than a.
  3. If i and i+2 are both less than a, check if their sum is greater than b.
  4. If the sum of i and i+2 is greater than b, add the pair (i, i+2) to a list of pairs.
  5. Return the list of pairs.

Python3




def find_pairs(a, b):
    pairs = []
    for i in range(1, a-1, 2):
        if i+2 < a:
            if i + i+2 > b:
                pairs.append((i, i+2))
    return pairs
 
a = 60
b = 100
pairs = find_pairs(a, b)
print("Pairs of consecutive number are:")
for pair in pairs:
    print(pair[0], ",", pair[1])


Output

Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59

The time complexity of this approach is O(a). 

The auxiliary space required is O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads