The Wayback Machine - https://web.archive.org/web/20241116073224/https://www.geeksforgeeks.org/python-find-all-pairs-of-consecutive-odd-positive-integer-smaller-than-a-with-sum-greater-than-b/
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
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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)



Similar Reads

Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
Python program to find the sum of all even and odd digits of an integer list
The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Ex
5 min read
Sum of all array elements less than X and greater than Y for Q queries
Given a sorted array arr[], and a set Q having M queries, where each query has values X and Y, the task is to find the sum of all integers less than X and greater than Y present in the array. Note: X and Y may or may not be present in the array. Examples: Input: arr[] = [3 5 8 12 15], Q = {{5, 12}, {4, 8}} Output: 18 30 Explanation: For query 1, X
9 min read
Python - Consecutive Ranges of K greater than N
Given a list of elements, the task is to write a Python program to get all ranges of K greater than N. Input : [2, 6, 6, 6, 6, 5, 4, 6, 6, 8, 4, 6, 6, 6, 2, 6], K = 6, N = 3 Output : [(1, 4), (11, 13)] Explanation : 6 is consecutive from index 1 to 4, hence 1-4 in result. 7-8 also has 6, but its less than 3 size range, hence not included in result.
4 min read
Python Program to check if elements to the left and right of the pivot are smaller or greater respectively
Given a list and an index, the task is to write a Python program to first select the element at that index as the pivot element and then test if elements are greater to its right and smaller to its left or not. Examples: Input : test_list = [4, 3, 5, 6, 9, 16, 11, 10, 12], K = 4 Output : True Explanation : Elements at Kth index is 9, elements befor
3 min read
Python - Test if Values Sum is Greater than Keys Sum in dictionary
Given a Dictionary, check if the summation of values is greater than the keys sum. Input : test_dict = {5:3, 1:3, 10:4, 7:3, 8:1, 9:5} Output : False Explanation : Values sum = 19 < 40, which is key sum, i.e false.Input : test_dict = {5:3, 1:4} Output : True Explanation : Values sum = 7 > 6, which is key sum, i.e true. Method #1: Using loop I
8 min read
Reverse bits of a positive integer number in Python
Given an positive integer and size of bits, reverse all bits of it and return the number with reversed bits.Examples: Input : n = 1, bitSize=32 Output : 2147483648 On a machine with size of bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648, bitSize=32 Output : 1 We can solve this problem quickly in Python. Approach is very simple,
4 min read
Python - Maximum length consecutive positive elements
Sometimes, while working with Python lists, we can have a problem in which we monitor a sequence and we need to find what was the maximum length when just positive elements occur. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [4, 5, 4, 1, 7, 2, 5, 6, -
4 min read
Python3 Program to Count triplets with sum smaller than a given value
Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. The expected Time Complexity is O(n2).Examples: Input : arr[] = {-2, 0, 1, 3} sum = 2.Output : 2Explanation : Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3) Input : arr[] = {5, 1, 3, 4, 7} sum = 12.Output : 4Explanat
3 min read
Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
Given an integer X and a binary tree, the task is to count the number of triplet triplets of nodes such that their sum is greater than X and they have a grandparent -> parent -> child relationship. Example: Input: X = 100 10 / \ 1 22 / \ / \ 35 4 15 67 / \ / \ / \ / \ 57 38 9 10 110 312 131 414 / \ 8 39 Output: 6 The triplets are: 22 -> 15
15+ min read
Python Map | Length of the Longest Consecutive 1's in Binary Representation of a given integer
Given a number n, find length of the longest consecutive 1s in its binary representation. Examples: Input : n = 14 Output : 3 The binary representation of 14 is 1110. Input : n = 222 Output : 4 The binary representation of 222 is 11011110. We have existing solution for this problem please refer Length of the Longest Consecutive 1s in Binary Represe
3 min read
Python - Maximum of Consecutive Pair in integer list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the maximization of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using loop + max() This is the b
5 min read
Python | Check if all the values in a list that are greater than a given value
Given a list, print all the values in a list that are greater than the given value Examples: Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : YesMethod 1: Traversal of list By traversing in the list, we can compare every element and check if all the elements in the given l
4 min read
Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 3, 4, 1], [4, 5, 6,
8 min read
Python - Extract dictionaries with values sum greater than K
Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 15 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gf
8 min read
Program to check if a number is Positive, Negative, Odd, Even, Zero
Given a number n, the task is to check whether the given number is positive, negative, odd, even, or zero. Method 1 : A number is positive if it is greater than zero. We check this in the expression of if.If it is False, the number will either be zero or negative.This is also tested in subsequent expressions.In the case of odd and even A number is
15 min read
Python | Find smallest element greater than K
Given a list, write a Python program to find the smallest number which is greater than a specific element K. Let's see all the approaches to solve this problem, from naive to one-liners so that they can be used in programming whenever required. Method #1: Naive Method Using loop we keep on re-initializing the named variable if we find the element s
6 min read
Python - Find the frequency of numbers greater than each element in a list
Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3, 7] Output : [2, 3, 1] Explanation : 6, 7 are great
8 min read
Python | N consecutive Odd or Even Numbers
The problem focused on in this article is quite specific and may be less useful in different domains. But the way this is going to solve may open doors to solve potentially like problems, hence making it worth a read. This article solves the problem of testing if a list contains a series of odd or even elements. Let's discuss certain ways in which
5 min read
Python program to find sum of absolute difference between all pairs in a list
Given a list of distinct elements, write a Python program to find the sum of absolute differences of all pairs in the given list.Examples: Input : [9, 2, 14] Output : 24 Explanation: (abs(9-2) + abs(9-14) + abs(2-14)) Input : [1, 2, 3, 4] Output : 10 Explanation: (abs(1-2) + abs(1-3) + abs(1-4) + abs(2-3) + abs(2-4) + abs(3-4)) The first approach i
5 min read
Python program to find all possible pairs with given sum
Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K. Examples: Input : lst =[1, 5, 3, 7, 9] K = 12 Output : [(5, 7), (3, 9)] Input : lst = [2, 1, 5, 7, -1, 4] K = 6 Output : [(2, 4), (1, 5), (7, -1)] Method #1: Pythonic Naive This is a naive approach to the above problem. First,
7 min read
Reduce the array by deleting elements which are greater than all elements to its left
Given an array arr[] of N integers, the task is to delete the element from the given array if element to it's left is smaller than it. Keep on deleting the elements from the array until no element has a smaller adjacent left element. Print the resultant array after above operation. Examples: Input: arr[] = {2, 4, 1, 3, 4} Output: 2 1 Explanation: S
5 min read
Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
Given a number N, the task is to find the minimum value K such that the sum of cubes of the first K natural number is greater than or equal to N. Examples: Input: N = 100 Output: 4 Explanation: The sum of cubes of first 4 natural number is 100 which is equal to N = 100 Input: N = 15 Output: 3 Explanation: The sum of cubes of first 2 natural number
11 min read
Python - Product of consecutive pairs in list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using loop This is the brute force me
7 min read
Python - Consecutive Pairs comma removal
Sometimes, while working amongst lists, we can have a problem in which we need to pair up elements from two lists. In this, we can have pairs in which we find that there is comma that is printed in tuple and we wish to avoid it usually. Lets discuss certain ways in which this task can be performed. Method #1 : Using zip() + list comprehension + rep
8 min read
Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Find words which are greater than given length k
A string is given, and you have to find all the words (substrings separated by a space) which are greater than the given length k. Examples: Input : str = "hello geeks for geeks is computer science portal" k = 4Output : hello geeks geeks computer science portalExplanation : The output is list of all words that are of length more than k.Input : str
11 min read
Python program to print all positive numbers in a range
Given start and end of a range, write a Python program to print all positive numbers in given range. Example: Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 Input: start = -3, end = 4 Output: 0, 1, 2, 3, 4 Example #1: Print all positive numbers from given list using for loop Define start and end limit of range. Iterate from start till the rang
4 min read
Python - Test if common values are greater than K
Sometimes, while working with Lists, we can have a problem in which we need to track common elements in list. This is quite common and we often have constructs to solve this. But sometimes, we require to consider two list as matching if they contain atleast K matching elements. Lets discuss certain ways in which this can be performed. Method #1 : U
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg