Vaishnavi Tripathi

Vaishnavi Tripathi

24 Articles Published

Articles by Vaishnavi Tripathi

Page 2 of 3

Largest number that is not a perfect square

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 05-Oct-2023 253 Views

In this article, we will discuss two different approaches to find out the largest number smaller than a given number which is not a perfect square. In the first approach, we will run a loop to check for every number until we find the desired number while in the second approach, we will use the concept of square root to generate the perfect square number just smaller than the given number and based on this, we will find out the largest number smaller than "nums" which is not a perfect square. Let us first understand the problem statement. Problem Statement ...

Read More

k smallest elements in same order using O(1) extra space

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 05-Oct-2023 180 Views

We have an array "nums" consisting of "size" elements and an integer "number" denoting the number of smallest elements we have to return. Our task is to find out the "number" smallest elements from the given array. The order of the elements should be preserved and we are not allowed to use any extra variable space for the solution i.e., the space complexity of the solution should be O(1). Let us understand this using an example, nums = { 4, 2, 6, 5, 1 } The solution should return 4, 2, 5 as they are the smallest 3 ...

Read More

Total numbers with no repeated digits in a range

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 16-Aug-2023 3K+ Views

In this article, we will discuss different approaches to calculate the number of positive integers which have no repeated digits between a given range Low to high. The first approach is a brute force approach which iterates over all the numbers in the range and check if they contain repeated digits. In our second approach, we calculated the desired count using prefix array while in our last approach we used the concept of memorization in dynamic programming to get the desired result. Problem Statement: We are given two numbers low and high and we have to find the count of ...

Read More

Tomohiko Sakamoto’s Algorithm- Finding the day of the week

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 16-Aug-2023 747 Views

In this article, we will discuss what is Tomohiko Sakamoto’s algorithm and how this algorithm is used to identify which day of the week does the given date occurs. There are multiple algorithms to know the day of the week but this algorithm is the most powerful one. This algorithm finds the day of the month on which the date occurs in least possible time and least space complexity. Problem statement − We are given a date as per Georgian calendar and our task is to find out which day of the week occurs on the given date using ...

Read More

Recursive Practice Problems with Solutions

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 16-Aug-2023 4K+ Views

In this article, we will discuss a few recursive practice problems with their detailed solutions. Let us first understand what recursion is and how it works: Recursion − Recursion is a programming technique in which a function or method calls itself multiple times in order to solve a problem. The function breaks down the problem into smaller sub-problems and solves them until it reaches a base case. The base case is a stopping condition that makes sure that the function stops calling itself and returns a result in finite time. Recursion is a powerful technique for solving complex ...

Read More

Swap every two bits in bytes

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 16-Aug-2023 668 Views

In this article, we will discuss the code solution to swap every alternate bit in a given number and return the resultant number. We will use the concept of bit manipulation in order to solve the problem in constant time without using any loops. Problem statement − We are given a number n, we have to swap the pair of bits that are adjacent to each other. In other words, we have to swap every odd placed bit with its adjacent even placed bit. Constrain: While solving the problem, we have to keep In mind that we cannot use ...

Read More

Sum of Series (n^2-1^2) + 2(n^2-2^2) +….n(n^2-n^2)

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 16-Aug-2023 213 Views

In this article, we will study different approaches to calculate the sum of the series- (n^2 - 1^2) + 2(n^2 - 2^2) + …. n(n^2 - n^2). In the first approach, we will calculate the series sum one by one for each i in the range 1 to n and keep adding it to the final sum. In the second approach, we will derive a mathematical formula to calculate the sum of the given series which will result in the reduced time complexity of the program from O(n) to O(1). Problem statement − We are given a number “n “and ...

Read More

Sort an array containing two types of elements

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 11-Apr-2023 385 Views

There are different approaches to sort an array containing only two types of elements i.e., only 1’s and 0’s. We will discuss three different approaches to do so. First approach simply uses a predefined sort() function to sort the given array. Second approach is a count sort approach in which we will count the number of zeroes and ones and then update the array by first writing zero for the number of times 0 was counted and then writing 1’s for the number of times we counted one. In the last approach, we used the two pointer method. Problem statement ...

Read More

Expressing factorial n as sum of consecutive numbers

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 11-Apr-2023 415 Views

We will discuss two approaches to find out how we can express the factorial of a number as the sum of consecutive numbers. First one is a straightforward and simple approach while in the other approach we use the concept of arithmetic progression to make it less complex in terms of time and space occupied. Problem statement We are given a number and we need to find out the number of ways in which we can represent the factorial of the number as a sum of consecutive natural numbers. This involves two different functions − To find the ...

Read More

Sorting array except elements in a subarray

Vaishnavi Tripathi
Vaishnavi Tripathi
Updated on 11-Apr-2023 527 Views

This article is about how we can sort an array by neglecting a subarray of elements present in the same array. We will discuss two approaches for the same. The first approach is a brute force approach with time complexity O(n*n) while the second approach is by using an additional space to keep the sorted part of array other than the subarray. The time complexity of second approach is better i.e., O(nlogn). Problem Statement We are given an array of positive integers “nums” and two indices of the same array namely- left and right and we have to partially sort ...

Read More
Showing 11–20 of 24 articles
Advertisements