├── Abstract in OOPS(java) ├── Addition of odd ├── Anagram (g2g) ├── Anjay and Street Lamps(Day 18) ├── Binary searching (day 7) ├── Binary tree(day 34) ├── Bubble sort (Day 9)( DSA) ├── Complete linked list application ├── Day 15 ├── Day 16 ├── Day 17 ├── Day 23 ├── Day 29 ├── Day 32(Linked list) ├── Day 33(Middle element in linked list) ├── Day 34 (Rturning the frequent element in an array) ├── Day 34 (linked list removing duplicates) ├── Day(19) ├── Day14 ├── Duplicated element(Leet code) ├── Duplicates ├── Encapsulation in OOPS(Java) ├── Exception handling using java(User defined exception) ├── Find Target Indices After Sorting Array(day 11) ├── Finding smallest and largest element in array using Selection sort (Day 4) ├── First and last occurance of the target value (Day 8) ├── First unique character(Dat 27) ├── HashMap ├── Insertionsort ├── Interface in OOPS (JAVA) ├── Matrix value finding(day 25) ├── Max ocxurance of an element ├── Maximum sum of concecutive element in an array (Using Two pointer | sliding window) ├── Median of the merged sorted array ├── Method overloading (Complie time polymorphism) oops in java ├── Minimum badness ├── Missing and Repeated Values ├── Missing value in consecutive number(Day 1) ├── Move zero (leet code) ├── Multi- dimensioal array ├── Neither Minimum nor Maximum (Day 11) ├── Occurance of the number using the hashmap (Collections) ├── Quick sort usign java ├── README.md ├── Recursion using cpp(Day 5) ├── Revering an array ├── Reversing a number using module:(Day 2) ├── Run time polymorphism in oops java ├── Selection shorting in java DSA (Day 9) ├── Sorted set union (Using Treeset) ├── Stack (validating paranetheses) day 28) ├── Sum of even elements in an array(Day 10) ├── Treeset in java ├── Triangle validator (Day 13) ├── Two pointer sum (day 32) ├── Valid parentheses (G2G problem ) using stack ├── Vector manipulation In Data Structures (Day 3) ├── addition of an element in an array (using long method) ├── contains set in the binary value ├── day 20 ├── day 21 ├── day 22 ├── day 24 ├── day 26 ( Frequent array) ├── day 30 ├── day 31 ├── day 31 (leet code 58) ├── day 33 ├── day 34 ├── day(26) Tribanocci ├── day27 ├── first occurance of the character ├── light management application using oops ├── maximum length of concecutive subelement in an array(Brute force) ├── maximum length of subset of an array (using two pointer method) ├── occuracne of the number in the array ├── one's complement ├── pretty number ├── second largest and second smallest (day 6) ├── stack └── sum of maximum subset /Abstract in OOPS(java): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | abstract class paytm{ 3 | public abstract void display(); 4 | public abstract void showbalance(); 5 | public abstract void sendmoney(); 6 | 7 | public void greet(){ 8 | System.out.println ("Welcome to paytm"); 9 | } 10 | } 11 | 12 | class subclass extends paytm{ 13 | public void display(){ 14 | System.out.println("Hello Muthukumar!"); 15 | } 16 | public void showbalance(){ 17 | System.out.println("Your current Balance is : 20000"); 18 | 19 | } 20 | public void sendmoney(){ 21 | System.out.println("1000rs sent!"); 22 | } 23 | 24 | 25 | 26 | } 27 | public class Main 28 | { 29 | public static void main(String[] args) { 30 | subclass s = new subclass(); 31 | subclass s1 = new subclass(); 32 | s.greet(); 33 | s.display(); 34 | s.showbalance(); 35 | s.sendmoney(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Addition of odd: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int n; 7 | n=scan.nextInt(); 8 | int[] arr=new int[n]; 9 | for(int i=0;i 0) { 13 | String s1 = br.readLine(); // first string 14 | String s2 = br.readLine(); // second string 15 | 16 | Solution obj = new Solution(); 17 | 18 | if (obj.areAnagrams(s1, s2)) { 19 | System.out.println("true"); 20 | } else { 21 | System.out.println("false"); 22 | } 23 | System.out.println("~"); 24 | } 25 | } 26 | } 27 | // } Driver Code Ends 28 | 29 | 30 | class Solution { 31 | public static boolean areAnagrams(String s1, String s2) { 32 | char[] c1 = s1.toCharArray(); 33 | char[] c2 = s2.toCharArray(); 34 | Arrays.sort(c1); 35 | Arrays.sort(c2); 36 | return Arrays.equals(c1,c2); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Anjay and Street Lamps(Day 18): -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | int n, l; 13 | cin >> n >> l; 14 | 15 | vector lanterns(n); 16 | for (int i = 0; i < n; ++i) { 17 | cin >> lanterns[i]; 18 | } 19 | 20 | sort(lanterns.begin(), lanterns.end()); 21 | 22 | double max_gap = max(lanterns[0] - 0, l - lanterns[n - 1]); 23 | 24 | for (int i = 1; i < n; ++i) { 25 | double gap = (lanterns[i] - lanterns[i - 1]) / 2.0; 26 | max_gap = max(max_gap, gap); 27 | } 28 | 29 | cout << fixed << setprecision(10) << max_gap << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Binary searching (day 7): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sort(int key,int arr[],int srt,int end) 5 | { 6 | while(srt<=end){ 7 | int mid=(srt+end)/2; 8 | 9 | if(key==arr[mid]){ 10 | return mid; 11 | } 12 | else if(arr[mid] 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | Node* left, * right; 7 | 8 | Node(int d) { 9 | data = d; 10 | left = nullptr; 11 | right = nullptr; 12 | } 13 | }; 14 | void inOrderDFS(Node* node) { 15 | if (node == nullptr) {return; 16 | } 17 | else{ 18 | inOrderDFS(node->left); 19 | cout << node->data << " "; 20 | inOrderDFS(node->right); 21 | } 22 | } 23 | void preOrderDFS(Node* node) { 24 | if (node == nullptr) { 25 | return; 26 | } 27 | else{ 28 | cout << node->data << " "; 29 | inOrderDFS(node->left); 30 | inOrderDFS(node->right); 31 | } 32 | 33 | } 34 | void postOrderDFS(Node* node) { 35 | if (node == nullptr) { 36 | return; 37 | } 38 | else{ 39 | inOrderDFS(node->left); 40 | inOrderDFS(node->right); 41 | cout << node->data << " "; 42 | } 43 | } 44 | int main(){ 45 | Node * firstnode=new Node(2); 46 | Node * secondnode=new Node(3); 47 | Node * thirdnode=new Node(4); 48 | Node * fourthnode=new Node(5); 49 | 50 | firstnode->left=secondnode; 51 | firstnode->right=thirdnode; 52 | secondnode->left=fourthnode; 53 | cout<<"Inorder: "; 54 | inOrderDFS(firstnode); 55 | cout<=0;i--){ 12 | for(int j=0;jarr[j+1]){ 14 | arr[j]=arr[j]+arr[j+1]; 15 | arr[j+1]=arr[j]-arr[j+1]; 16 | arr[j]=arr[j]-arr[j+1]; 17 | } 18 | else{ 19 | continue; 20 | } 21 | } 22 | 23 | } 24 | for(int i=0;i"); 138 | current = current.link; 139 | } 140 | System.out.println(); 141 | } 142 | 143 | //Search element 144 | 145 | public int search(int val){ 146 | Node current =head; 147 | int count =0; 148 | while(current!=null){ 149 | if(current.data == val){ 150 | return count; 151 | } 152 | else{ 153 | current=current.link; 154 | count ++; 155 | } 156 | 157 | } 158 | return -1; 159 | } 160 | 161 | //Get the last element 162 | 163 | public Node getlast(){ 164 | Node temp = head; 165 | while(temp.link!=null){ 166 | temp = temp.link; 167 | } 168 | return temp; 169 | } 170 | } 171 | 172 | public class Main 173 | { 174 | public static void main(String[] args) { 175 | Scanner scan = new Scanner(System.in); 176 | LinkedList l = new LinkedList(); 177 | try{ 178 | while(true){ 179 | System.out.println("1 -> Add the element in the First : "); 180 | System.out.println("2 -> Add the element in the specified position : "); 181 | System.out.println("3 -> Add the element in the Last : "); 182 | System.out.println("4 -> Delete the element in the first : "); 183 | System.out.println("5 -> Delete the element in the specified value : "); 184 | System.out.println("6 -> Delete the element in the last : "); 185 | System.out.println("7 -> Search the element : "); 186 | System.out.println("8 -> last element in the LinkedList : "); 187 | System.out.println("9 -> Print the element in the linked LinkedList : "); 188 | System.out.println("10 -> Exit "); 189 | int c = scan.nextInt(); 190 | int n; 191 | switch(c){ 192 | case 1: 193 | System.out.println("Enter the element : "); 194 | n=scan.nextInt(); 195 | System.out.println( l.addFirst(n)); 196 | break; 197 | 198 | case 2: 199 | System.out.println("Enter the element and the position : "); 200 | n=scan.nextInt(); 201 | int pos=scan.nextInt(); 202 | System.out.println(l.addAtSpec(n,pos-1)); 203 | break; 204 | case 3: 205 | System.out.println("Enter the element : "); 206 | n=scan.nextInt(); 207 | System.out.println( l.addLast(n)); 208 | break; 209 | case 4: 210 | System.out.println(l.deleteFirst()); 211 | break; 212 | case 5: 213 | System.out.println("Enter the element to delete : "); 214 | n=scan.nextInt(); 215 | System.out.println(l.deleteAtSpec(n)); 216 | break; 217 | case 6: 218 | System.out.println(l.deleteLast()); 219 | break; 220 | case 7: 221 | System.out.println("Enter the element to serach the postion : "); 222 | n=scan.nextInt(); 223 | System.out.println("position : "+l.search(n)+1); 224 | break; 225 | case 8: 226 | System.out.println("Last Element : "+l.getlast().data); 227 | break; 228 | case 9: 229 | l.traverse(); 230 | break; 231 | case 10: 232 | System.out.println("program Exit!"); 233 | System.exit(0); 234 | default: 235 | System.out.println("Enter the valid chioce!"); 236 | break; 237 | } 238 | } 239 | } 240 | catch(Exception e){ 241 | System.out.println(e); 242 | } 243 | 244 | } 245 | } 246 | -------------------------------------------------------------------------------- /Day 15: -------------------------------------------------------------------------------- 1 | Writers and novels 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n,t=0; 8 | cin>>n; 9 | vector ar(n); 10 | for(int i=0;i>ar[i]; 12 | t+=ar[i]; 13 | } 14 | int p=t*4; 15 | int b=p/100; 16 | if(p%100 != 0){ 17 | b++; 18 | } 19 | cout< 2 | using namespace std; 3 | int main() { 4 | int n; 5 | cin>>n; 6 | vector nums(n); 7 | for(int i=0;i>nums[i]; 9 | } 10 | vector res; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=0;i>s; 10 | vector val(s); 11 | for(int j=0;j>val[j]; 13 | } 14 | int c=0; 15 | for(int j=0;j=1000){ 17 | c+=1; 18 | } 19 | else{ 20 | continue; 21 | } 22 | } 23 | cout< 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int countPairs(vector& hours) { 10 | unordered_map remainderCount; 11 | int pairCount = 0; 12 | 13 | for (int hour : hours) { 14 | int remainder = hour % 24; 15 | int complement = (24 - remainder) % 24; 16 | 17 | if (remainderCount.find(complement) != remainderCount.end()) { 18 | pairCount += remainderCount[complement]; 19 | } 20 | 21 | remainderCount[remainder]++; 22 | } 23 | 24 | return pairCount; 25 | } 26 | 27 | int main() { 28 | int n; 29 | cin >> n; 30 | vector hours(n); 31 | 32 | for (int i = 0; i < n; i++) { 33 | cin >> hours[i]; 34 | } 35 | 36 | cout << countPairs(hours) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Day 29: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string reverseWords(string s) { 7 | stringstream ss(s); 8 | string word; 9 | stack words; 10 | 11 | while(getline(ss, word, ' ')) { 12 | if (!word.empty()) { 13 | words.push(word); 14 | } 15 | } 16 | 17 | string res = ""; 18 | while(!words.empty()) { 19 | res += words.top(); 20 | words.pop(); 21 | if (!words.empty()) { 22 | res += " "; 23 | } 24 | } 25 | 26 | return res; 27 | } 28 | }; 29 | 30 | int main() { 31 | Solution s1; 32 | string s; 33 | getline(cin, s); // Read the entire input string with spaces 34 | string res = s1.reverseWords(s); 35 | cout << res; 36 | } 37 | -------------------------------------------------------------------------------- /Day 32(Linked list): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | }; 8 | 9 | //insert at the begining 10 | void insert(Node*& head, int val){ 11 | Node* newst=new Node(); 12 | newst->data=val; 13 | newst->next=head; 14 | head=newst; 15 | } 16 | 17 | //insert at specified position 18 | void insertpos(Node*& head,int pos, int val){ 19 | if(pos==1){ 20 | insert(head,val); 21 | } 22 | else{ 23 | Node* temp=head; 24 | for(int i=1;inext; 26 | } 27 | Node* newnd=new Node(); 28 | newnd->data=val; 29 | newnd->next=temp->next; 30 | temp->next=newnd; 31 | } 32 | } 33 | 34 | //delete at the begining; 35 | 36 | void deletee(Node*& head){ 37 | if(head!=nullptr){ 38 | Node* temp=head; 39 | head=head->next; 40 | delete temp; 41 | } 42 | else{ 43 | cout<<"Linked list in underflow"<next; 57 | } 58 | prev=temp->next; 59 | temp->next=prev->next; 60 | delete prev; 61 | } 62 | } 63 | 64 | //print the linkedlist; 65 | void printlist(Node* head){ 66 | while(head!=nullptr){ 67 | cout<<"->"<data; 68 | head=head->next; 69 | } 70 | cout<>n; 85 | switch(n){ 86 | case 1: 87 | { 88 | cout<<"Enter the data value : "<>c1; 91 | insert(head,c1); 92 | break; 93 | } 94 | case 2: 95 | { 96 | cout<<"Enter the position and data :"<>c21>>c22; 99 | insertpos(head,c21,c22); 100 | break; 101 | } 102 | case 3: 103 | { 104 | deletee(head); 105 | break; 106 | } 107 | case 4: 108 | { 109 | cout<<"Enter the position of a linked lsit : "<>c4; 112 | deletepos(head,c4); 113 | break; 114 | } 115 | case 5: 116 | { 117 | printlist(head); 118 | break; 119 | } 120 | case 6: 121 | { 122 | exit(0); 123 | } 124 | 125 | } 126 | } 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /Day 33(Middle element in linked list): -------------------------------------------------------------------------------- 1 | struct ListNode { 2 | int val; 3 | ListNode *next; 4 | ListNode() : val(0), next(nullptr) {} 5 | ListNode(int x) : val(x), next(nullptr) {} 6 | ListNode(int x, ListNode *next) : val(x), next(next) {} 7 | }; 8 | 9 | class Solution { 10 | public: 11 | ListNode* middleNode(ListNode* head) { 12 | int count =0; 13 | ListNode* sam=head; 14 | while(sam!=nullptr){ 15 | count ++; 16 | sam=sam->next; 17 | } 18 | int s=ceil(count/2); 19 | ListNode* temp=head; 20 | 21 | for(int i=1;inext; 23 | } 24 | return temp; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Day 34 (Rturning the frequent element in an array): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int majorityElement(vector& nums) { 6 | map map; 7 | for(int i=0;i res={1,1,2,3,2,1,1}; 31 | int ss=s.majorityElement(res); 32 | cout< mapp; 13 | while(head!=nullptr){ 14 | mapp[head->val]++; 15 | head=head->next; 16 | } 17 | int s=mapp.size(); 18 | ListNode* temp=nullptr; 19 | ListNode * prev=nullptr; 20 | for(const auto& x:mapp){ 21 | if(x.second == 1){ 22 | if(temp==nullptr){ 23 | temp=new ListNode(x.first); 24 | prev=temp; 25 | } 26 | else{ 27 | prev->next=new ListNode(x.first); 28 | prev=prev->next; 29 | } 30 | } 31 | } 32 | return temp; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Day(19): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int n; 5 | vector nums(n); 6 | for(int i=0;i>nums[i]; 8 | } 9 | vector odd; 10 | vector even; 11 | vector res; 12 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int main() { 5 | int n; 6 | cin>>n; 7 | vector arr(n); 8 | for(int i=0;i>arr[i]; 10 | } 11 | int a; 12 | cin>>a; 13 | int count=0; 14 | for(int i=1;i<=n;i++){ 15 | if((i%a)==0){ 16 | count = count + arr[i-1]; 17 | } 18 | else{ 19 | continue; 20 | } 21 | } 22 | cout< map = new HashMap<>(); 4 | List list = new ArrayList<>(); 5 | for(int x : nums1){ 6 | map.put(x,map.getOrDefault(x,0)+1); 7 | } 8 | for(int y : nums2){ 9 | list.add(y); 10 | } 11 | List list2 = new ArrayList<>(); 12 | 13 | for(HashMap.Entry x : map.entrySet()){ 14 | if(list.contains(x.getKey())){ 15 | list2.add(x.getKey()); 16 | } 17 | else{ 18 | continue; 19 | } 20 | } 21 | int s = list2.size(); 22 | int arr[] = new int[s]; 23 | for(int i=0;i result = new Solution().findDuplicates(arr); 18 | Collections.sort(result); 19 | // Printing the result in the required format 20 | if (result.isEmpty()) { 21 | System.out.println("[]"); 22 | } else { 23 | for (int i = 0; i < result.size(); i++) { 24 | if (i != 0) System.out.print(" "); 25 | System.out.print(result.get(i)); 26 | } 27 | System.out.println(); 28 | } 29 | System.out.println("~"); 30 | } 31 | } 32 | } 33 | 34 | // } Driver Code Ends 35 | 36 | 37 | class Solution { 38 | public List findDuplicates(int[] arr) { 39 | ArrayList list = new ArrayList(); 40 | HashMap map = new HashMap(); 41 | for(int x : arr){ 42 | map.put(x,map.getOrDefault(x,0)+1); 43 | } 44 | for(int x : map.keySet()){ 45 | if(map.get(x) > 1 ){ 46 | list.add(x); 47 | } 48 | } 49 | return list; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Encapsulation in OOPS(Java): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class encapsule{ 3 | private String name; 4 | private String roll; 5 | private long phone; 6 | 7 | public void setValue(String name, String roll , long phone){ 8 | this.name=name; 9 | this.roll=roll; 10 | this.phone=phone; 11 | } 12 | public void getValue(){ 13 | System.out.println("Name : "+name); 14 | System.out.println("Roll no : "+roll); 15 | System.out.println("Phone No : "+phone); 16 | } 17 | } 18 | public class Main 19 | { 20 | public static void main(String[] args) { 21 | encapsule e = new encapsule(); 22 | e.setValue("Muthukumar","927622BAL028",6300802); 23 | e.getValue(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Exception handling using java(User defined exception): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | class userdefinedException extends Exception{ 4 | public userdefinedException(String m){ 5 | super(m); 6 | } 7 | } 8 | public class Main{ 9 | public static int getvalue(int arr[] , int x) throws userdefinedException{ 10 | if(x<0 || x>arr.length){ 11 | throw new userdefinedException("The array index in exceed to the length of the array"); 12 | } 13 | else{ 14 | return arr[x]; 15 | } 16 | } 17 | public static void main(String[] args) { 18 | int []arr={1,2,3,4,5,6}; 19 | try { 20 | System.out.println(getvalue(arr,10)); 21 | } catch(userdefinedException e) { 22 | System.out.println(e); 23 | } 24 | finally{ 25 | System.out.println("Execution of the code complated"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Find Target Indices After Sorting Array(day 11): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector targetIndices(vector& nums, int target) { 4 | vector res; 5 | int s=nums.size(); 6 | int temp; 7 | for(int i=0;inums[j]){ 11 | temp=nums[i]; 12 | nums[i]=nums[j]; 13 | nums[j]=temp; 14 | } 15 | else{ 16 | continue; 17 | } 18 | } 19 | } 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int arr[5]={3,2,4,5,1}; 7 | int s=sizeof(arr)/4; 8 | int temp=0; 9 | for(int i=0;i searchRange(vector& nums, int target) { 4 | int st=-1; 5 | int et=-1; 6 | int s = nums.size(); 7 | for(int i=0;i=0;i--){ 17 | if(nums[i]==target){ 18 | et=i; 19 | Break; } 20 | else{ 21 | continue; 22 | } 23 | } 24 | return {st,et}; 25 |     } }; 26 | -------------------------------------------------------------------------------- /First unique character(Dat 27): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int firstUniqChar(string s) { 6 | int n=s.size(); 7 | for(int i=0;i>s; 38 | int res= s1.firstUniqChar(s); 39 | cout<> map = new HashMap<>(); 7 | 8 | //Method 1 9 | 10 | map.put(1.0f,new HashMap()); 11 | map.get(1.0f).put("1","one"); 12 | map.get(1.0f).put("2","Two"); 13 | map.put(2.0f, new HashMap()); 14 | map.get(2.0f).put("3","Three"); 15 | map.get(2.0f).put("4","Four"); 16 | 17 | //Method 2 18 | 19 | HashMap n = new HashMap<>(); 20 | n.put("5","Five"); 21 | n.put("6","Six"); 22 | 23 | 24 | map.put(3.0f,n); 25 | 26 | for(float x : map.keySet()){ 27 | System.out.println(x +" : "+map.get(x)); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Insertionsort: -------------------------------------------------------------------------------- 1 | public class Main 2 | { 3 | static int[] insertionsort(int[] arr){ 4 | int step=0; 5 | for(int i=1;i=0 && arr[j]>key){ 9 | arr[j+1]=arr[j]; 10 | j--; 11 | step++; 12 | } 13 | arr[j+1]=key; 14 | } 15 | System.out.println(step); 16 | return arr; 17 | } 18 | public static void main(String[] args) { 19 | 20 | int[] arr={2,3,5,1,4,9,8,7}; 21 | int[] res=insertionsort(arr); 22 | for(int i=0;i>& matrix, int target) { 4 | int n=matrix.size(); 5 | int m=matrix[0].size(); 6 | for(int i=0;i map = new HashMap<>(); 13 | for( int x:arr){ 14 | map.put(x,map.getOrDefault(x,0)+1); 15 | } 16 | int v=-1; 17 | for(HashMap.Entry entry : map.entrySet()){ 18 | if(entry.getValue() > 1){ 19 | v=entry.getKey(); 20 | } 21 | else{ 22 | continue; 23 | } 24 | } 25 | System.out.print(v); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Maximum sum of concecutive element in an array (Using Two pointer | sliding window): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | int tar=4; 6 | int[] arr={1,2,3,4,5,6,7}; 7 | int l=0,r=tar-1; 8 | int sum=0; 9 | int maxsum=0; 10 | for(int i=l;i<=r;i++){ 11 | sum+=arr[i]; 12 | } 13 | maxsum=sum; 14 | while(r& nums1, vector& nums2) { 4 | int n1=nums1.size(); 5 | int n2=nums2.size(); 6 | for(int x:nums2){ 7 | nums1.push_back(x); 8 | } 9 | sort(nums1.begin(),nums1.end()); 10 | if((nums1.size())%2 == 0){ 11 | int s=nums1.size()/2; 12 | double ans=(nums1[s]+nums1[s-1])/2.0; 13 | return ans; 14 | } 15 | else{ 16 | int s=floor(nums1.size()/2); 17 | double ans=nums1[s]; 18 | return ans; 19 | } 20 | return 0; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Method overloading (Complie time polymorphism) oops in java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class poly{ 3 | public void sample(int a,int b){ 4 | System.out.println(a+b); 5 | } 6 | public void sample(String a,String b){ 7 | System.out.println(a+b); 8 | } 9 | public void sample(float a,float b){ 10 | System.out.println(a+b); 11 | } 12 | public void sample(double a,double b){ 13 | System.out.println(a+b); 14 | } 15 | } 16 | public class Main 17 | { 18 | public static void main(String[] args) { 19 | poly p = new poly(); 20 | p.sample(1,2); 21 | p.sample("Muthu","Kumar"); 22 | p.sample(2.0,3.0); 23 | p.sample(3.30,5.56); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Minimum badness: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | string s="RRWBWBWRWR"; 5 | for(int i =0;i map = new HashMap<>(); 4 | int r=0; 5 | int n=grid.length*grid[0].length; 6 | for(int i=0;i1){ 13 | r=x; 14 | } 15 | } 16 | int l=(n*(n+1))/2; 17 | for(Integer x:map.keySet()){ 18 | l=l-x; 19 | } 20 | int[] k=new int[2]; 21 | k[0]=r; 22 | k[1]=l; 23 | return k; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Missing value in consecutive number(Day 1): -------------------------------------------------------------------------------- 1 | m=[1,2,3,4,5,6,7,9] 2 | n=9 3 | summ=(n*(n+1))//2 4 | i=0 5 | for x in m: 6 | i=i+x 7 | print("Missing number : ",summ-i) 8 | -------------------------------------------------------------------------------- /Move zero (leet code): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static void moveZeroes(int[] nums) { 3 | int sum=0; 4 | List list = new ArrayList<>(); 5 | for(int i=0;i& nums) { 4 | int s=nums.size(); 5 | int temp; 6 | if(s<=2){ 7 | return -1; 8 | } 9 | else{ 10 | for(int i=0;inums[j]){ 13 | temp=nums[i]; 14 | nums[i]=nums[j]; 15 | nums[j]=temp; 16 | } 17 | else{ 18 | continue; 19 | } 20 | } 21 | } 22 | return nums[1]; 23 | } 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Occurance of the number using the hashmap (Collections): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | int[] arr={10,20,30,10,10,40,50,10,60,10}; 6 | HashMap map = new HashMap<>(); 7 | for(int x : arr){ 8 | map.put(x,map.getOrDefault(x,0)+1); 9 | } 10 | for(int x:map.keySet()){ 11 | if(x == 10){ 12 | System.out.println(x+" - "+map.get(x)); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Quick sort usign java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void quicksort(int arr[],int low,int high){ 5 | if(low 2 | using namespace std; 3 | void recur(int n, string s){ 4 | if(n>10){ 5 | return; 6 | } 7 | else{ 8 | cout<arr[j]){ 15 | temp=arr[i]; 16 | arr[i]=arr[j]; 17 | arr[j]=temp; 18 | } 19 | else{ 20 | continue; 21 | } 22 | } 23 | 24 | } 25 | for(int i=0;i 0) { 14 | // First array input 15 | String[] str1 = br.readLine().trim().split( 16 | " "); // Read the first line and split by spaces 17 | int n = str1.length; 18 | int[] a = new int[n]; 19 | for (int i = 0; i < n; i++) { 20 | a[i] = Integer.parseInt(str1[i]); // Convert each element to an integer 21 | } 22 | 23 | // Second array input 24 | String[] str2 = br.readLine().trim().split( 25 | " "); // Read the second line and split by spaces 26 | int m = str2.length; 27 | int[] b = new int[m]; 28 | for (int i = 0; i < m; i++) { 29 | b[i] = Integer.parseInt(str2[i]); // Convert each element to an integer 30 | } 31 | 32 | Solution obj = new Solution(); 33 | ArrayList res = new ArrayList(); 34 | res = obj.findUnion(a, b); 35 | for (int i = 0; i < res.size(); i++) System.out.print(res.get(i) + " "); 36 | System.out.println(); 37 | 38 | System.out.println("~"); 39 | } 40 | } 41 | } 42 | 43 | // } Driver Code Ends 44 | class Solution { 45 | public static ArrayList findUnion(int a[], int b[]) { 46 | TreeSet t1 = new TreeSet<>(); 47 | for(int x : a){ 48 | t1.add(x); 49 | } 50 | for(int x : b){ 51 | t1.add(x); 52 | } 53 | ArrayList res = new ArrayList<>(); 54 | for(int x : t1){ 55 | res.add(x); 56 | } 57 | return res; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Stack (validating paranetheses) day 28): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool paran(string s){ 4 | stack st; 5 | 6 | if(s.size() %2==0){ 7 | for(char i : s){ 8 | if(i=='{' || i=='[' || i=='('){ 9 | st.push(i); 10 | } 11 | else{ 12 | if(st.size() ==0){ 13 | return false; 14 | } 15 | else{ 16 | char ss=st.top(); 17 | if((ss=='{' && i=='}') || (ss=='[' && i==']') || (ss=='(' && i==')')){ 18 | st.pop(); 19 | } 20 | else{ 21 | return false; 22 | } 23 | } 24 | } 25 | } 26 | return st.empty(); 27 | } 28 | else{ 29 | return false; 30 | } 31 | } 32 | int main() 33 | { 34 | string s; 35 | cin>>s; 36 | bool res=paran(s); 37 | if(res==0){ 38 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout<<"Enter the number of elements in an array : "<>a; 9 | int n[a]; 10 | int sum=0; 11 | cout<<"Enter the values of an array: "<>n[i]; 14 | } 15 | for(int i=0;i 30 | using namespace std; 31 | 32 | int main() 33 | { 34 | int arr[5]={3,2,4,5,1}; 35 | int s=sizeof(arr)/4; 36 | int temp=0; 37 | for(int i=0;i set = new TreeSet<>(); 6 | set.add(11); 7 | set.add(2); 8 | set.add(33); 9 | set.add(14); 10 | set.add(12); 11 | 12 | System.out.println(set.first()); 13 | System.out.println(set.last()); 14 | System.out.println(set.higher(14)); 15 | System.out.println(set.lower(14)); 16 | set.pollFirst(); 17 | set.pollLast(); 18 | System.out.println(set.contains(12)); 19 | 20 | Iterator it =set.iterator(); 21 | while(it.hasNext()){ 22 | System.out.println(it.next()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Triangle validator (Day 13): -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int a,b,c; 5 | cin>>a>>b>>c; 6 | if(a+b>c && a+c>b && b+c>a){ 7 | cout<<"Yes"; 8 | } 9 | 10 | else{ 11 | cout<<"No"; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Two pointer sum (day 32): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | int n=nums.size(); 5 | int l=0,r=n-1; 6 | sort(nums.begin(),nums.end()); 7 | vector val; 8 | for(int i=l;i=0;j++){ 10 | if(i!=j){ 11 | if((nums[i]+nums[j]) == target){ 12 | val.push_back(i); 13 | val.push_back(j); 14 | break; 15 | } 16 | else if((nums[i]+nums[j]) < target){ 17 | i++; 18 | } 19 | else{ 20 | j--; 21 | } 22 | } 23 | else{ 24 | continue; 25 | } 26 | } 27 | } 28 | return val; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Valid parentheses (G2G problem ) using stack: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.lang.*; 3 | import java.util.*; 4 | 5 | class Driverclass { 6 | public static void main(String args[]) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int t = sc.nextInt(); 10 | 11 | while (t-- > 0) { 12 | String st = sc.next(); 13 | 14 | if (new Solution().isParenthesisBalanced(st) == true) 15 | System.out.println("true"); 16 | else 17 | System.out.println("false"); 18 | 19 | System.out.println("~"); 20 | } 21 | } 22 | } 23 | 24 | class Solution { 25 | static boolean isParenthesisBalanced(String s) { 26 | Stack st = new Stack<>(); 27 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | 5 | vectorvec={2,7,9,10,14}; 6 | vec.push_back(100); 7 | vec.push_back(101); 8 | vec.pop_back(); 9 | vec.begin(); 10 | vec.end(); 11 | vec.empalce_back(1000); 12 | vec.erase(vec.begin()+1,vec.end()-1); 13 | vec.erase(vec.begin()); 14 | vec.erase(vec.begin(),vec.end()-1); 15 | vec.erase(vec.begin()+3,vec.end()-0); 16 | vec.erase(vec.begin()+1,vec.end()-0); 17 | for(int i:vec ){ 18 | cout< 0) 11 | { 12 | int N = Integer.parseInt(read.readLine()); 13 | Solution ob = new Solution(); 14 | System.out.println(ob.isBitSet(N)); 15 | 16 | System.out.println("~"); 17 | } 18 | } 19 | } 20 | // } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | class Solution{ 25 | static int isBitSet(int N){ 26 | String s=Integer.toBinaryString(N); 27 | char[] ch = s.toCharArray(); 28 | HashSet set = new HashSet<>(); 29 | for( char x : ch){ 30 | set.add(x); 31 | } 32 | if(set.size() == 1 && set.contains('1')){ 33 | return 1; 34 | } 35 | else{ 36 | return 0; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /day 20: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, k; 7 | cin >> n; 8 | vector arr(n); 9 | for (int i = 0; i < n; i++) { 10 | cin >> arr[i]; 11 | } 12 | cin >> k; 13 | int totalCalibre = 0; 14 | for (int i = 0; i < n; i++) { 15 | totalCalibre += arr[i]; 16 | } 17 | int learningSessions = (totalCalibre + k - 1) / k; 18 | int totalLearningTime = learningSessions * 10; 19 | int totalBreakTime = learningSessions * 3; 20 | int totalTime = totalLearningTime + totalBreakTime; 21 | cout << totalTime << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /day 21: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // Function to check if two arrays are equal or not. 4 | bool check(vector& arr1, vector& arr2) { 5 | // code here 6 | int n=arr1.size(); 7 | for(int i=0;iarr1[j]){ 11 | temp=arr1[i]; 12 | arr1[i]=arr1[j]; 13 | arr1[j]=temp; 14 | } 15 | } 16 | 17 | } 18 | for(int i=0;iarr2[j]){ 22 | tem=arr2[i]; 23 | arr2[i]=arr2[j]; 24 | arr2[j]=tem; 25 | } 26 | } 27 | 28 | } 29 | int s=0; 30 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, k; 7 | cin >> n; 8 | vector arr(n); 9 | for (int i = 0; i < n; i++) { 10 | cin >> arr[i]; 11 | } 12 | cin >> k; 13 | int totalCalibre = 0; 14 | for (int i = 0; i < n; i++) { 15 | totalCalibre += arr[i]; 16 | } 17 | int learningSessions = (totalCalibre + k - 1) / k; 18 | int totalLearningTime = learningSessions * 10; 19 | int totalBreakTime = learningSessions * 3; 20 | int totalTime = totalLearningTime + totalBreakTime; 21 | cout << totalTime << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /day 24: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=0;i>a; 10 | if(a>=1 && a<=10){ 11 | cout<<"lower double"<=11 && a<=15){ 14 | cout<<"Lower single"<=16 && a<=25){ 17 | cout<<"upper double"< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter the number of elements : "; 6 | cin>>n; 7 | vector num(n); 8 | for(int i=0;i>num[i]; 10 | } 11 | auto max=max_element(num.begin(),num.end()); 12 | cout<<"\n"<<"Max number : "<<*max; 13 | vector fre(*max); 14 | for(int i=0;i& nums, int target) { 4 | bool val=false; 5 | int s; 6 | int size=nums.size(); 7 | for(int i=0;inums[size-1]){ 19 | return size; 20 | } 21 | else{ 22 | for(int i=0;i& nums1, int m, vector& nums2, int n) { 4 | vector res=nums1; 5 | nums1.clear(); 6 | for(int i=0;i words; 7 | while(getline(ss,word,' ')){ 8 | if (!word.empty()) { 9 | words.push(word); 10 | } 11 | 12 | } 13 | string res=words.top(); 14 | int val=res.size(); 15 | return val; 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /day 33: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | Scanner scan=new Scanner(System.in); 6 | int s=0; 7 | int n; 8 | n=scan.nextInt(); 9 | int arr[]=new int[n]; 10 | for(int i=0;ia[j+1]){ 29 | int temp=a[j]; 30 | a[j]=a[j+1]; 31 | a[j+1]=temp; 32 | s++; 33 | } 34 | } 35 | } 36 | for(int i=0;i 2 | using namespace std; 3 | int main() { 4 | vector num={0,1,1}; 5 | int n; 6 | cin>>n; 7 | for(int i=3;i<=n;i++){ 8 | num.push_back(num[i-1]+num[i-2]+num[i-3]); 9 | } 10 | cout< 2 | using namespace std; 3 | int main(){ 4 | vector num={1,1,2,3,4,5,55,6}; 5 | map arr; 6 | for(auto x:num){ 7 | arr[x]++; 8 | } 9 | int a=0; 10 | int aa=num[0]; 11 | for(auto x:arr){ 12 | if(x.second>a){ 13 | aa=x.first; 14 | a=x.second; 15 | } 16 | } 17 | cout< map){ 5 | for(char x : map.keySet()){ 6 | if(map.get(x)>1){ 7 | return x; 8 | } 9 | } 10 | return '.'; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner scan = new Scanner(System.in); 15 | int n=scan.nextInt(); 16 | scan.nextLine(); 17 | while(n>0){ 18 | LinkedHashMap map = new LinkedHashMap<>(); 19 | 20 | String s=scan.nextLine(); 21 | 22 | char[] c=s.toCharArray(); 23 | for(char ch : c){ 24 | map.put(ch,map.getOrDefault(ch,0)+1); 25 | } 26 | System.out.println(duplicate(map)); 27 | n--; 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /light management application using oops: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class light{ 3 | int led; 4 | String color; 5 | String type; 6 | boolean status; 7 | 8 | light(int l, String c,String t, boolean s){ 9 | this.led=l; 10 | this.color=c; 11 | this.type = t; 12 | this.status=s; 13 | } 14 | public void switchOn(){ 15 | System.out.println("Switched on!"); 16 | } 17 | public void switchOff(){ 18 | System.out.println("Switched off!"); 19 | } 20 | public void getValue(){ 21 | System.out.println("LED : " +led); 22 | System.out.println("COLOR : "+color); 23 | System.out.println("TYPE : "+type); 24 | System.out.println("STSTUS : "+status); 25 | System.out.println("_ _".repeat(10)); 26 | 27 | } 28 | } 29 | 30 | public class Main 31 | { 32 | public static void main(String[] args) { 33 | light l1= new light(5,"red","strip",false); 34 | l1.switchOn(); 35 | l1.switchOff(); 36 | l1.getValue(); 37 | light l2= new light(10,"Blue","Ring light",true); 38 | l2.getValue(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /maximum length of concecutive subelement in an array(Brute force): -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | int[] arr={1,2,3,4,5,6,7}; 6 | int tar=14; 7 | int sum=0; 8 | int maxnum =0; 9 | for(int i=0;itar){ 13 | sum-=arr[l]; 14 | l++; 15 | } 16 | if(sum<=tar){ 17 | maxnum=Math.max(maxnum,r-l+1); 18 | r++; 19 | } 20 | } 21 | System.out.print(maxnum); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /occuracne of the number in the array: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int arr[7]={4,3,5,5,3,7,4}; 5 | map map; 6 | for(int i=0;i<(sizeof(arr)/sizeof(arr[0]));i++){ 7 | map[arr[i]]++; 8 | } 9 | int m=0; 10 | for (const auto &pair : map) { 11 | if(pair.second == 1){ 12 | cout< 0) 11 | { 12 | int N = Integer.parseInt(read.readLine()); 13 | 14 | Solution ob = new Solution(); 15 | System.out.println(ob.onesComplement(N)); 16 | 17 | System.out.println("~"); 18 | } 19 | } 20 | } 21 | // } Driver Code Ends 22 | 23 | 24 | //User function Template for Java 25 | class Solution{ 26 | static int onesComplement(int N){ 27 | String n=Integer.toBinaryString(N); 28 | char[] ch = n.toCharArray(); 29 | for (int i = 0; i < ch.length; i++) { 30 | if (ch[i] == '0') { 31 | ch[i] = '1'; 32 | } else { 33 | ch[i] = '0'; 34 | } 35 | } 36 | String res = new String(ch); 37 | 38 | int re=Integer.parseInt(res,2); 39 | return re; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pretty number: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int n=scan.nextInt(); 7 | for(int i=0;i 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int arr[]={4,-2,6,-1,7,8}; 9 | int min1=arr[0]; 10 | int s=sizeof(arr)/4; 11 | for(int i=0;iarr[i] && arr[i]>min1){ 22 | min2=arr[i]; 23 | } 24 | } 25 | 26 | cout< 34 | using namespace std; 35 | 36 | int main() 37 | { 38 | int arr[]={3,0,1,2,4,5}; 39 | int s=sizeof(arr)/4; 40 | int max1=0; 41 | for(int i=0;imax1){ 43 | max1=arr[i]; 44 | } 45 | else{ 46 | continue; 47 | } 48 | } 49 | int max2=0; 50 | for(int i=0;i st=new Stack<>(); 7 | int n; 8 | n=scan.nextInt(); 9 | int arr[]=new int[n]; 10 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | vector num={1,2,3,4,5,6,7,7,8}; 6 | int s=num.size(); 7 | int n; 8 | cin>>n; 9 | vector res; 10 | int flag=0; 11 | int sum=0; 12 | int k=floor(s/n); 13 | if(k==0){ 14 | cout<<"flase"; 15 | } 16 | else{ 17 | for(int i=0;i