Skip to main content

Posts

Showing posts with the label DSA Problem Solve Apna College

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 Complexit...

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 (), wh...

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) {       ...

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(); }          }      ...

Print alternate elements of an array DSA Problem

  Print alternate elements of an array School Accuracy:  52.74% Submissions:  161K+ Points:  0 Three 90 Challenge Extended On Popular Demand! Don't Miss Out Now  You are given an array  A  of size  N . You need to print elements of A in alternate order (starting from index 0). Example 1: Input: N = 4 A[] = {1, 2, 3, 4} Output: 1 3 Example 2: Input: N = 5 A[] = {1, 2, 3, 4, 5} Output: 1 3 5 Your Task: Since this is a function problem, you just need to complete the provided function  print()  which takes  A  and  n  as input parameters and print the resultant array in the function itself. You have to print the final number of array ending with a space as Printing of newline character is taken care in driver code itself. Constraints: 1 <= N <= 10 5 1 <= A i  <= 10 5 Expected Time Complexity:  O(n) Expected Auxiliary Space:  O(1) Ans:  Used Approach: Iterative Time Complexity of Approa...

Value equal to index value DSA Problem

Value equal to index value School Accuracy:  54.83% Submissions:  186K+ Points:  0 Three 90 Challenge Extended On Popular Demand! Don't Miss Out Now  Given an array  Arr  of  N  positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ). Note : There can be more than one element in the array which have the same value as its index. You need to include every such element's index. Follows 1-based indexing of the array. Example 1: Input: N = 5 Arr[] = {15, 2, 45, 12, 7} Output: 2 Explanation: Only Arr[2] = 2 exists here. Example 2: Input: N = 1 Arr[] = {1} Output: 1 Explanation: Here Arr[1] = 1 exists. Your Task:   You don't need to read input or print anything. Your task is to complete the function  valueEqualToIndex()  which takes the array of integers  arr[]   and  n  as parameters and returns an array of indices wher...

Sum of Series DSA Problem

Write a program to find the sum of the given series 1+2+3+ . . . . . .( N  terms)  Example 1: Input: N = 1 Output: 1 Explanation: For n = 1, sum will be 1. Example 2: Input: N = 5 Output: 15 Explanation: For n = 5, sum will be 15.  1 + 2 + 3 + 4 + 5 = 15.   Ans:  class Solution {     public static long seriesSum(int n) {         // code                            if(n == 1){             return 1;         }                  long s = 0;                   for(int i = 1; i <= n; i++){             s = s+i;            } return s;                       } }          Note...

Contains Duplicate

Given an integer array  nums , return  true  if any value appears  at least twice  in the array, and return  false  if every element is distinct.   Example 1: Input: nums = [1,2,3,1] Output: true  Ans:  1) is Problem ko ham O(N) me solve kar rhe  2) Jiske liye ham single loop use kar rhe aur use 1 se start kr rhe  3) compare 0 index ko i (1) se kar rhe class Solution {     public boolean containsDuplicate ( int [] nums ) {         Arrays . sort (nums);         for ( int i = 1 ; i < nums . length ;i++){             if (nums[i- 1 ] == nums[i]){                 return true ;             }     } return false ; } }

Remove Duplicate Node From Sorted Linked List

  /**  * Definition for singly-linked list.  * public class ListNode {  *     int val;  *     ListNode next;  *     ListNode() {}  *     ListNode(int val) { this.val = val; }  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }  * }  */ class Solution {     public ListNode deleteDuplicates ( ListNode head ) {         if (head == null ){             return head;         }         ListNode newHead = head;         while ( newHead . next != null ){             if ( newHead . val == newHead . next . val ){                 newHead . next = newHead . next . next ;             }             else {     ...

Delete Node Recursively

Another Approach is Iterative>> Time Complexity O(N);   import java.util. * ; public class removeNode_recursively { public static void print ( Node < Integer > head ){ System . out .println( "New Node:" ) ; while ( head != null ){ System . out .print( head . data + " " ) ; head = head . next ; } } public static Node < Integer > removeRecursively ( Node < Integer > head , int pos ){ if ( head == null ){ return head ; } if ( pos == 0 ){ head = head . next ; } Node < Integer > smallInput = removeRecursively ( head . next , pos - 1 ) ; head . next = smallInput ; return head ; } public static Node < Integer > takeInput (){ Scanner scan = new Scanner( System . in ) ; Node < Integer > head = null ; Node < Integer > tail = null ; int data = 0 ; ...