7

Find the XOR of smallest and largest triangular number in a given range

 1 year ago
source link: https://www.geeksforgeeks.org/find-the-xor-of-smallest-and-largest-triangular-number-in-a-given-range/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Find the XOR of smallest and largest triangular number in a given range

Given a range, find the XOR of the smallest and largest triangular numbers within that range.

A triangular number is a number that can be represented as the sum of consecutive positive integers starting from 1. In other words, a triangular number is the sum of the first n natural numbers, where n is a positive integer. The formula for the nth triangular number is: Tn = 1 + 2 + 3 + … + n = n(n+1)/2

For example, the first few triangular numbers are:

T1 = 1
T2 = 1 + 2 = 3
T3 = 1 + 2 + 3 = 6
T4 = 1 + 2 + 3 + 4 = 10

Examples:

Input: Range [60, 195]
Output: 252
Explanation: Smallest triangular number in the given range is 66, and the largest triangular number in the given range is 190. The XOR of 66 and 190 is 252.

Input: Range [100, 280]
Output: 381
Explanation: Smallest triangular number in the given range is 105, and the largest triangular number in the given range is 276. The XOR of 105 and 276 is 381.

Approach: This can be solved with the following idea:

The idea behind this approach is to first define a function to find the nth triangular number using the formula n*(n+1)/2. Then, we define two functions to find the smallest and largest triangular numbers within a given range. To find the smallest triangular number within the range, we simply iterate through the triangular numbers until we find the first triangular number that is greater than or equal to the lower bound, and return that triangular number if it is also less than or equal to the upper bound. If there is no triangular number within the range, we return -1. To find the largest triangular number within the range, we again iterate through the triangular numbers, but this time we return the previous triangular number when we find the first triangular number that is greater than the upper bound. This is because the previous triangular number is the largest triangular number within the range. If there is no triangular number within the range, we return 0.

Steps of the above approach:

  • Define a function findTriangularNumber(n) that takes an integer n and returns the nth triangular number. The formula to find the nth triangular number is n*(n+1)/2.
  • Define a function findSmallestTriangularNumberInRange(lower_bound, upper_bound) that takes the lower and upper bounds of the range as input and returns the smallest triangular number within that range.
  • This can be done by iterating through the triangular numbers until you find the first triangular number that is greater than or equal to the lower bound, and returning that triangular number if it is also less than or equal to the upper bound. If there is no triangular number within the range, return -1.
  • Define a function findLargestTriangularNumberInRange(lower_bound, upper_bound) that takes the lower and upper bounds of the range as input and returns the largest triangular number within that range. 
  • This can be done by iterating through the triangular numbers until you find the first triangular number that is greater than the upper bound and returning the previous triangular number, which is the largest triangular number within the range. If there is no triangular number within the range, return 0. 
  • Finally, in the main() function, we call these two functions with the given range and check if either of them returns -1 or 0. If not, we XOR the smallest and largest triangular numbers and print the result.

Below is the implementation of the above approach:

// C++ code of the above approach:
#include <bits/stdc++.h>
using namespace std;

// Function to find triangular number
int findTriangularNumber(int n) { return n * (n + 1) / 2; }

// Function to find smallest triangular
// number above the smallest range
int findSmallestTriangularNumberInRange(int lower_bound,
                                        int upper_bound)
{
    for (int n = 1;; n++) {
        int triangular_number = findTriangularNumber(n);
        if (triangular_number >= lower_bound
            && triangular_number <= upper_bound) {
            return triangular_number;
        }
        else if (triangular_number > upper_bound) {
            return -1;
        }
    }
}

// Function to find largest triangular
// number below the range
int findLargestTriangularNumberInRange(int lower_bound,
                                       int upper_bound)
{
    int largest_triangular_number = 0;
    for (int n = 1;; n++) {
        int triangular_number = findTriangularNumber(n);
        if (triangular_number > upper_bound) {
            return largest_triangular_number;
        }
        else if (triangular_number >= lower_bound) {
            largest_triangular_number = triangular_number;
        }
    }
}

// Driver code
int main()
{
    int lower_bound = 100, upper_bound = 280;

    // Function call
    int smallest_triangular_number
        = findSmallestTriangularNumberInRange(lower_bound,
                                              upper_bound);
    int largest_triangular_number
        = findLargestTriangularNumberInRange(lower_bound,
                                             upper_bound);

    if (smallest_triangular_number == -1
        || largest_triangular_number == 0) {
        cout << "-1" << endl;
    }
    else {
        int result = smallest_triangular_number
                     ^ largest_triangular_number;
        cout << result << endl;
    }

    return 0;
}
Output
381

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK