Skip to main content

Posts

Showing posts from February, 2024

Count of smaller elements (GFG)

Question:  Count of smaller elements School Accuracy:  54.54% Submissions:  91K+ Points:  0 Internship Alert! Become an SDE Intern by topping this monthly leaderboard!  Given an sorted array  A  of size  N . Find number of elements which are less than or equal to given element  X .   Example 1: Input: N = 6 A[] = {1, 2, 4, 5, 8, 10} X = 9 Output: 5   Example 2: Input: N = 7 A[] = {1, 2, 2, 2, 5, 7, 9} X = 2 Output: 4   Your Task:   You don't need to read input or print anything. Your task is to complete the function  countOfElements()  which takes the array  A[] , its size  N  and an integer  X  as inputs and returns the number of elements which are less than or equal to given element.   Expected Time Complexity:  O(N) Expected Auxiliary Space:  O(1)   Constraints: 1 <= N <= 10 5 1 <= A i  <= 10 5 0 <= X <= 10 5 Ans:  Time Complexity of this solution: O(n); public class Solution {     public int countOfElements(int[] arr, int n, int x) {         int count = 0;

Wave Array (GFG)

Question:  Wave Array Easy Accuracy:  63.69% Submissions:  235K+ Points:  2 Internship Alert! Become an SDE Intern by topping this monthly leaderboard!  Given a  sorted  array  arr[]  of distinct integers. Sort the array into a wave-like array(In Place). In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5]..... If there are multiple solutions, find the lexicographically smallest one. Note: The given array is sorted in ascending order, and you don't need to return anything to make changes in the original array itself. Example 1: Input: n = 5 arr[] = {1,2,3,4,5} Output: 2 1 4 3 5 Explanation: Array elements after sorting it in wave form are 2 1 4 3 5. Example 2: Input: n = 6 arr[] = {2,4,7,8,9,10} Output: 4 2 8 7 10 9 Explanation: Array elements after sorting it in wave form are 4 2 8 7 10 9. Your Task: The task is to complete the function  convertToWave (), which converts the given array to a wave array. Expe

Missing Number in Array

Missing number in array Easy Accuracy:  29.59% Submissions:  1M Points:  2 Internship Alert! Become an SDE Intern by topping this monthly leaderboard!  Given an array of size  N-1  such that it only contains distinct integers in the range of  1 to N . Find the missing element. Example 1: Input: N = 5 A[] = {1,2,3,5} Output: 4 Example 2: Input: N = 10 A[] = {6,1,2,8,3,4,7,10,5}  Output: 9   Ans:  1)I'm trying to solve this problem using 2 for loops 2) Time complexity will be O(N); 3) ham kya karenge phle sare element ko sort kar denge 4) sort krne ke bad saare element ka sum ek sum1 variable me store kar lenge 5) ek loop lagayenge jo ki 1 se n ke barabar tak chalega aur 1 ke n ke barabar tak ke count ka sum, sum2 variable me store kar dega. 6) sum2 se sum1 ko minus kar denge then this will differ of array means missing element of array. class Solution {     int missingNumber(int array[], int n) {         // Your Code Here                  Arrays.sort(array);                int sum1

Find the Frequency

Given a vector of  N  positive integers and an integer  X . The task is to find the  frequency  of X in vector.   Example 1: Input: N = 5 vector = {1, 1, 1, 1, 1} X = 1 Output: 5  Explanation: Frequency of 1 is 5.\\ Company Tag: Google Ans: In this problem we are using single loop Time Complexity: O(n) //{ Driver Code Starts //Initial Template for Java import java.io.*; import java.util.*; class FastReader{      BufferedReader br;      StringTokenizer st;      public FastReader(){          br = new BufferedReader(new InputStreamReader(System.in));      }      String next(){          while (st == null || !st.hasMoreElements()){              try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); }          }          return st.nextToken();      }      String nextLine(){          String str = "";          try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }          return str;      }           Integer nextInt(){    

Stack Data Structure

Question: What is stack ? Ans: Stack is a Linear data structure , Operations in Stack is performed in LIFO order   Stack: Stack is a Last in first out data structure [LIFO MODEL] Stack is Linear data structure in which insertion and deletions are allowed only at the end,called the top of the stack. As a stack is a Linear data structure , we can implement it using Array and LinkedList . bcse we know that Array and linkedlist is a linear data structure so can implement stack on Array and Linkedlist Operations In Stacks:  1)  Push elements onto the stack or push Operation : Kisi cheej ko push karke stack ke sabse upar dal dena Push keh lata hai , Iski time complexity O(1) hoti hai. 2)  Pop elements from the stack or pop Operations : To remove and return the top element from the stack, Stack ke top se element ko nikal lena Pop kehlata hai. Time Complexity O(1). pop will remove last inserted elements from stack Kisi bhi element ko stack se delete krne ko ham PoP bolte hai 3) Top/ Peek at t

Data Structures

Question: What is Data Structure ? "Data structure" ek programming aur computer science concept hai jo data ko organize aur store karne ka tarika hai taaki usse efficently access aur manipulate kiya ja sake. Data structure ek particular way mein data ko arrange karta hai jisse data ko retrieve aur update karna aasani se ho. Yeh data ko organize karne ke liye various data types aur operations ka use karta hai. Data structures ka use algorithms ko optimize karne mein hota hai, jisse ki data ko efficiently process kiya ja sake aur space aur time complexity ko minimize kiya ja sake. Yeh programming languages mein data ko store aur access karne ke tarike ko define karte hain. Question: Give me explanation of Data Structures Type. Types Of Data Structures: 1) Linear Data Structure 2) Non-Linear Data Structure 1) Linear data Structure: Linear data structure ek aise data structure hai jisme data elements linearly, yaani sequence mein store kiye jaate hain, ek ke baad ek. Yani, har el