R

@Ruchirm702

Best Time to Buy and Sell Stocks II

Java
2 years ago
/* Problem Description Say you have an array, A, for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). Problem Constraints 0 <= len(A) <= 1e5 1 <= A[i] <= 1e7 */

Leaders in an array : The rightmost element is always a leader.

Java
2 years ago
/* Problem Description Given an integer array A containing N distinct integers, you have to find all the leaders in array A. An element is a leader if it is strictly greater than all the elements to its right side. NOTE: The rightmost element is always a leader. Problem Constraints 1 <= N <= 105 1 <= A[i] <= 108 */ import java.util.*; import java.lang.*; import java.io.*;

Count Pairs : Special Subsequences "AG" :(String)

Java
2 years ago
/* Problem Description You have given a string A having Uppercase English letters. You have to find the number of pairs (i, j) such that A[i] = 'A', A[j] = 'G' and i < j. Problem Constraints :- 1 <= length(A) <= 105 */ import java.util.*; import java.lang.*; import java.io.*;

Best Time to Buy and Sell Stocks

Java
2 years ago
/* Say you have an array A , for which ith element is the price of a given stock on day i . If you were only permitted to complete at most one transaction (i.e. buy one and sell one share of the stock) , design an algorithm to find the maximum profit . Return the maximum possile profit. Problem Constraints :- 0 <= A.size() <= 10^5 1 <= A[i] <= 10^9

Leaders Count in a Array

Java
2 years ago
/* Given an array ar[N] , you have to find a number of leaders in the arry . Leader : An elemet ar[i] is said to be a leader if it is greater than the maximum of all elements present on the left of it i.e. [0,i-1] Note : ar[0] is already considered a leader Constraints : 1 <= N <= 10^5 1 <= ar[i] <= 10^9 Arr : [ 3 , 2 , 4 , 5 , 2 , 7 , -1 , 15]

Count Pairs

Java
2 years ago
/* Given a character array ch[N] of size N , we have to calculate number of pairs of indices (i,j) where i<j && ch[i] == 'a' && ch[j] == 'g' . Constrains :- 1 <= N <= 10^5 'a' <= ch[i] <= 'z' Arr = [ b , a , a , g , d , c , a , g ] My Observation :- 1) Maximum possible length of array --> 10^5 2) Charcter array have lower case alphabet

Sum of odd indices

Java
2 years ago
/*Problem Description You are given an array A of length N and Q queries given by the 2D array B of size Q*2. Each query consists of two integers B[i][0] and B[i][1]. For every query, the task is to calculate the sum of all odd indices in the range A[B[i][0]…B[i][1]]. Note : Use 0-based indexing Problem Constraints 1 <= N <= 105 1 <= Q <= 105 1 <= A[i] <= 100 0 <= B[i][0] <= B[i][1] < N

Sum of even indices

Java
2 years ago
/* Problem Description You are given an array A of length N and Q queries given by the 2D array B of size Q*2. Each query consists of two integers B[i][0] and B[i][1]. For every query, the task is to calculate the sum of all even indices in the range A[B[i][0]…B[i][1]]. Note : Use 0-based indexing Problem Constraints 1 <= N <= 105 1 <= Q <= 105 1 <= A[i] <= 100

Even numbers in a range and queries given by the 2D array

Java
2 years ago
/* Problem Description You are given an array A of length N and Q queries given by the 2D array B of size Q×2. Each query consists of two integers B[i][0] and B[i][1]. For every query, your task is to find the count of even numbers in the range from A[B[i][0]] to A[B[i][1]]. Problem Constraints 1 <= N <= 105 1 <= Q <= 105 1 <= A[i] <= 109 0 <= B[i][0] <= B[i][1] < N */

Find the equilibrium index of the given array

Java
2 years ago
/* Problem Description You are given an array A of integers of size N. Your task is to find the equilibrium index of the given array The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. If there are no elements that are at lower indexes or at higher indexes, then the corresponding sum of elements is considered as 0. Note: Array indexing starts from 0. If there is no equilibrium index then return -1.

Equilibrum Index Count

Java
2 years ago
/* Given an array arr[] of n elements , count the number of equilibrium indices . An index i is said to be equilibrium index if sum of all indices on left of ith index is equal to the sum of all indices on right of ith index . NOTE : If i == 0 , left sum = 0 if i = n-1 , right sum = 0 ; Constraints :- 1<=n<=10^5 1<=arr[i]<=10^9 */

Range Sum Query WIth 2D Array

Java
2 years ago
/* Problem Description You are given an integer array A of length N. You are also given a 2D integer array B with dimensions M x 2, where each row denotes a [L, R] query. For each query, you have to find the sum of all elements from L to R indices in A (0 - indexed). More formally, find A[L] + A[L + 1] + A[L + 2] +... + A[R - 1] + A[R] for each query. Problem Constraints 1 <= N, M <= 105 1 <= A[i] <= 109

Range Sum Query WIth 2D Array

Java
2 years ago
/* Problem Description You are given an integer array A of length N. You are also given a 2D integer array B with dimensions M x 2, where each row denotes a [L, R] query. For each query, you have to find the sum of all elements from L to R indices in A (0 - indexed). More formally, find A[L] + A[L + 1] + A[L + 2] +... + A[R - 1] + A[R] for each query. Problem Constraints 1 <= N, M <= 105 1 <= A[i] <= 109

Even number in a Given Range

Java
2 years ago
/* Given an array arr[] of n elements and q queries . For each query given indices l and r calculate and print the no. of even no in the given range (l and r both inclusive) Constraints :- 1 <= n <= 10^5 1 <= arr[i] <= 10^9 0 <= l <= r < n */ import java.util.*; import java.lang.*; import java.io.*;

Range Queries

Java
2 years ago
/* Given an arr[] of n elements and q queries . For each query , given indices l and r . Print Sum of all elements in range [l,r]( l and r are inclusive) Note : l and r are array indices Constraints :- 1 <= n , q<= 10^5 1<= arr[i] <= 10^9 0 <= r < n */

Prefix Sum Array

Java
2 years ago
// Prefix Sum import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static long[] Prefixsum(int [] arr){ int n = arr.length;

Rotate Array from Right To Left

Java
2 years ago
/* Given an Array A[] and an int value L , Rotate array K times from Left to Right */ import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main".

Reverse Array

Java
2 years ago
/* Given an array A[] , Reverse the array . Constraints : 1) Number of Iteration allowed is N ( single iteration only ) 2) Using Extra Array is not Allowed . */ import java.util.*; import java.lang.*; import java.io.*;

Check if it is PRIME or NOT.

Java
2 years ago
import java.util.*; /* Given a number N , check if it is PRIME or NOT. */ import java.lang.*; import java.io.*; // The main method must be in a class named "Main".

Count of Factors - Optimised

Java
2 years ago
/* Given an integer A , you need to find the count of its factors . Constraints : 1<= A <= 10^9 */ import java.util.*; import java.lang.*;