Programming Articles

Page 11 of 2544

Binary search in C#

Ravi Ranjan
Ravi Ranjan
Updated on 21-Aug-2025 12K+ Views

The binary search algorithm works on the divide and conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, the array should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left sub-array, right sub-array, or return the middle element. In this article, we are given a sorted array of integers, and our task is to search for the given target element using binary ...

Read More

Difference Between Linear Search and Binary Search

Ravi Ranjan
Ravi Ranjan
Updated on 21-Aug-2025 5K+ Views

A linear search compares the target element with each array element, while a binary search uses divide-and-conquer method to efficiently search for the target element. In this article, we will compare linear search and binary search. What is Linear Search? Linear search is a sequential searching algorithm where we traverse every element within the input array and compare it with the target element to be found. If the target element is found, we return true and the index, and return false if the element is not found in the given array. Below is an animation of working of linear ...

Read More

C++ Programs To Create Pyramid and Pattern

Nishu Kumari
Nishu Kumari
Updated on 21-Aug-2025 2K+ Views

In this article, we will show you how to write C++ programs to create different pyramids and patterns. You can create many kinds of patterns using stars, numbers, and alphabets. Below is the list of patterns we will cover in this article- Simple Pyramid Pattern in C++ Flipped Simple Pyramid Pattern in C++ Inverted Pyramid Pattern in C++ Flipped Inverted Pyramid Pattern in C++ Triangle Pattern in C++ Inverted Triangle Pattern in C++ ...

Read More

Constructor overloading in enumerations in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 1K+ Views

Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it. Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it. Constructor Overloading in Java Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in ...

Read More

What is the return type of a Constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 7K+ Views

A Constructor in Java is similar to a method, and it is invoked at the time of creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. The main purpose of a constructor is to initialize the instance variables of a class. Return Type of Constructor In Java, constructors do not have a return type, not even void. They are used to initialize the object when it is created. A constructor doesn’t have any return type. ...

Read More

Write a program in C++ to find the length of the largest subarray with zero sum

Nishu Kumari
Nishu Kumari
Updated on 20-Aug-2025 399 Views

We are given an array of N integers and the task is to find the length of the longest subarray whose elements sum up to zero. A subarray is a continuous sequence of elements within the array. If no such subarray exists, return 0. Let's look at a few example scenarios to understand the problem better- Scenario 1- Input: N = 7 arr = {1, 2, -3, 3, -1, 2, -2} Output: 5 Explanation: The subarray {2, -3, 3, -1, 2} has a sum of 0 and its length is 5, which is the longest such ...

Read More

Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers

Nishu Kumari
Nishu Kumari
Updated on 20-Aug-2025 5K+ Views

We are given an array of unsorted integers of size N. The task is to find the distinct maximum and second maximum elements which are present in the array. The array may contain duplicate elements also, so we have to find only distinct elements. If there is no second maximum, we will return -1 for the second maximum. Let's look at some example scenarios to understand the problem clearly - Scenario 1- Input: N = 5, A[] = {2, 2, 1, 3, 4} Output: 4 and 3 Explanation: From the given array, we can see that '4' is ...

Read More

Write a program in C++ to check if a string can be obtained by rotating another string by two places

Nishu Kumari
Nishu Kumari
Updated on 20-Aug-2025 556 Views

We are given two strings, a and b and the task is to find whether we can obtain the string b by rotating string a exactly two places in either an anticlockwise (left) or clockwise (right) direction. Let's take an example scenario to understand the problem clearly — Scenario 1- Input: a = "google", b = "legoog" Output: True Explanation: Rotating "google" two places anticlockwise gives "legoog", which matches string b. Thus, we return True. Scenario 2- Input 2: a = "tuorialst", b = "tutorials" Output: False Explanation: String "tuorialst" cannot be ...

Read More

Write a program in C++ to find the missing positive number in a given array of unsorted integers

Nishu Kumari
Nishu Kumari
Updated on 20-Aug-2025 527 Views

We are given an array of unsorted integers, and the task is to find the missing positive integer. The given array may contain negative numbers, zeros, and duplicate values, all in any random order. Let's look at some example scenarios to understand the problem clearly- Scenario 1- Input: arr = [3, -2, 5, 1, -7, 4, -1, 8] Output: 2 Explanation: The number 2 is not present in the array, so the missing positive is 2 Scenario 2- Input: arr = [0] Output: 1 Explanation: In the given array, '1' is the only positive ...

Read More

C++ Program to Find the Number of occurrences of a given Number using Binary Search approach

Ravi Ranjan
Ravi Ranjan
Updated on 20-Aug-2025 387 Views

In this article, our task is to find the number of occurrences of a given number using binary search. The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element. Following are some example scenarios: Scenario ...

Read More
Showing 101–110 of 25,433 articles
« Prev 1 9 10 11 12 13 2544 Next »
Advertisements