Topics :

  1. Analysis of Algorithms
  2. Searching and Sorting
  3. Greedy Algorithms
  4. Dynamic Programming
  5. Pattern Searching
  6. Other String Algorithms
  7. Backtracking
  8. Divide and Conquer
  9. Geometric Algorithms
  10. Mathematical Algorithms
  11. Bit Algorithms
  12. Graph Algorithms
  13. Randomized Algorithms
  14. Branch and Bound
  15. Quizzes on Algorithms
  16. Misc

Analysis of Algorithms:

  1. Asymptotic Analysis
  2. Worst, Average and Best Cases
  3. Asymptotic Notations
  4. Analysis of Loops
  5. Solving Recurrences
  6. Amortized Analysis
  7. What does ‘Space Complexity’ mean?
  8. Pseudo-polynomial Algorithms
  9. NP-Completeness Introduction
  10. Polynomial Time Approximation Scheme
  11. A Time Complexity Question
  12. Time Complexity of building a heap
  13. Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
  14. Time Complexity of Loop with Powers
  15. Performance of loops (A caching question)

Recent Articles on Analysis of Algorithms

Quiz on Analysis of Algorithms

Quiz on Recurrences

Searching and Sorting:

  1. Binary Search
  2. Selection Sort
  3. Bubble Sort
  4. Insertion Sort
  5. Merge Sort
  6. Heap Sort
  7. QuickSort
  8. Radix Sort
  9. Counting Sort
  10. Bucket Sort
  11. ShellSort
  12. Comb Sort
  13. Pigeonhole Sort
  14. Interpolation search vs Binary search
  15. Stability in sorting algorithms
  16. When does the worst case of Quicksort occur?
  17. Lower bound for comparison based sorting algorithms
  18. Which sorting algorithm makes minimum number of memory writes?
  19. Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
  20. Merge Sort for Linked Lists
  21. Sort a nearly sorted (or K sorted) array
  22. Iterative Quick Sort
  23. QuickSort on Singly Linked List
  24. QuickSort on Doubly Linked List
  25. Find k closest elements to a given value
  26. Sort n numbers in range from 0 to n^2 – 1 in linear time
  27. A Problem in Many Binary Search Implementations
  28. Search in an almost sorted array
  29. Sort an array in wave form
  30. Why is Binary Search preferred over Ternary Search?
  31. K’th Smallest/Largest Element in Unsorted Array
  32. K’th Smallest/Largest Element in Unsorted Array in Expected Linear Time
  33. K’th Smallest/Largest Element in Unsorted Array in Worst Case Linear Time
  34. Find the closest pair from two sorted arrays
  35. Find common elements in three sorted arrays
  36. Given a sorted array and a number x, find the pair in array whose sum is closest to x
  37. Count 1’s in a sorted binary array
  38. Binary Insertion Sort
  39. Insertion Sort for Singly Linked List
  40. Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
  41. Merge Sort for Doubly Linked List

Recent Articles on Searching

Recent Articles on Sorting

Quiz on Sorting

Quiz on Searching

Greedy Algorithms:

  1. Activity Selection Problem
  2. Kruskal’s Minimum Spanning Tree Algorithm
  3. Huffman Coding
  4. Efficient Huffman Coding for Sorted Input
  5. Prim’s Minimum Spanning Tree Algorithm
  6. Prim’s MST for Adjacency List Representation
  7. Dijkstra’s Shortest Path Algorithm
  8. Dijkstra’s Algorithm for Adjacency List Representation
  9. Job Sequencing Problem
  10. Quiz on Greedy Algorithms
  11. Greedy Algorithm to find Minimum number of Coins
  12. K Centers Problem

Recent Articles on Greedy Algorithms

Quiz on Greedy Algorithms

Dynamic Programming:

  1. Overlapping Subproblems Property
  2. Optimal Substructure Property
  3. Longest Increasing Subsequence
  4. Longest Common Subsequence
  5. Edit Distance
  6. Min Cost Path
  7. Coin Change
  8. Matrix Chain Multiplication
  9. Binomial Coefficient
  10. 0-1 Knapsack Problem
  11. Egg Dropping Puzzle
  12. Longest Palindromic Subsequence
  13. Cutting a Rod
  14. Maximum Sum Increasing Subsequence
  15. Longest Bitonic Subsequence
  16. Floyd Warshall Algorithm
  17. Palindrome Partitioning
  18. Partition problem
  19. Word Wrap Problem
  20. Maximum Length Chain of Pairs
  21. Variations of LIS
  22. Box Stacking Problem
  23. Program for Fibonacci numbers
  24. Minimum number of jumps to reach end
  25. Maximum size square sub-matrix with all 1s
  26. Ugly Numbers
  27. Largest Sum Contiguous Subarray
  28. Longest Palindromic Substring
  29. Bellman–Ford Algorithm for Shortest Paths
  30. Optimal Binary Search Tree
  31. Largest Independent Set Problem
  32. Subset Sum Problem
  33. Maximum sum rectangle in a 2D matrix
  34. Count number of binary strings without consecutive 1?s
  35. Boolean Parenthesization Problem
  36. Count ways to reach the n’th stair
  37. Minimum Cost Polygon Triangulation
  38. Mobile Numeric Keypad Problem
  39. Count of n digit numbers whose sum of digits equals to given sum
  40. Minimum Initial Points to Reach Destination
  41. Total number of non-decreasing numbers with n digits
  42. Find length of the longest consecutive path from a given starting character
  43. Tiling Problem
  44. Minimum number of squares whose sum equals to given number n
  45. Find minimum number of coins that make a given value
  46. Collect maximum points in a grid using two traversals
  47. Shortest Common Supersequence
  48. Compute sum of digits in all numbers from 1 to n
  49. Count possible ways to construct buildings
  50. Maximum profit by buying and selling a share at most twice
  51. How to print maximum number of A’s using given four keys
  52. Find the minimum cost to reach destination using a train
  53. Vertex Cover Problem | Set 2 (Dynamic Programming Solution for Tree)
  54. Count number of ways to reach a given score in a game
  55. Weighted Job Scheduling
  56. Longest Even Length Substring such that Sum of First and Second Half is same

Recent Articles on Dynamic Programming

Quiz on Dynamic Programming

Pattern Searching:

  1. Naive Pattern Searching
  2. KMP Algorithm
  3. Rabin-Karp Algorithm
  4. A Naive Pattern Searching Question
  5. Finite Automata
  6. Efficient Construction of Finite Automata
  7. Boyer Moore Algorithm – Bad Character Heuristic
  8. Suffix Array
  9. Anagram Substring Search (Or Search for all permutations)
  10. Pattern Searching using a Trie of all Suffixes
  11. Aho-Corasick Algorithm for Pattern Searching
  12. kasai’s Algorithm for Construction of LCP array from Suffix Array
  13. Z algorithm (Linear time pattern searching Algorithm)

Recent Articles on Pattern Searching

Other String Algorithms:

  1. Manacher’s Algorithm – Linear Time Longest Palindromic Substring – Part 1, Part 2, Part 3, Part 4
  2. Longest Even Length Substring such that Sum of First and Second Half is same
  3. Print all possible strings that can be made by placing spaces

Recent Articles on Strings

Backtracking:

  1. Print all permutations of a given string
  2. The Knight’s tour problem
  3. Rat in a Maze
  4. N Queen Problem
  5. Subset Sum
  6. m Coloring Problem
  7. Hamiltonian Cycle
  8. Sudoku
  9. Tug of War
  10. Solving Cryptarithmetic Puzzles

Recent Articles on Backtracking

Divide and Conquer:

  1. Introduction
  2. Write your own pow(x, n) to calculate x*n
  3. Median of two sorted arrays
  4. Count Inversions
  5. Closest Pair of Points
  6. Strassen’s Matrix Multiplication

Recent Articles on Divide and Conquer

Quiz on Divide and Conquer

Geometric Algorithms:

  1. Closest Pair of Points | O(nlogn) Implementation
  2. How to check if two given line segments intersect?
  3. How to check if a given point lies inside or outside a polygon?
  4. Convex Hull | Set 1 (Jarvis’s Algorithm or Wrapping)
  5. Convex Hull | Set 2 (Graham Scan)
  6. Given n line segments, find if any two segments intersect
  7. Check whether a given point lies inside a triangle or not
  8. How to check if given four points form a square

Recent Articles on Geometric Algorithms

Mathematical Algorithms:

  1. Write an Efficient Method to Check if a Number is Multiple of 3
  2. Efficient way to multiply with 7
  3. Write a C program to print all permutations of a given string
  4. Lucky Numbers
  5. Write a program to add two numbers in base 14
  6. Babylonian method for square root
  7. Multiply two integers without using multiplication, division and bitwise operators, and no loops
  8. Print all combinations of points that can compose a given number
  9. Write you own Power without using multiplication(*) and division(/) operators
  10. Program for Fibonacci numbers
  11. Average of a stream of numbers
  12. Count numbers that don’t contain 3
  13. MagicSquare
  14. Sieve of Eratosthenes
  15. Find day of the week for a given date
  16. DFA based division
  17. Generate integer from 1 to 7 with equal probability
  18. Given a number, find the next smallest palindrome
  19. Make a fair coin from a biased coin
  20. Check divisibility by 7
  21. Find the largest multiple of 3
  22. Lexicographic rank of a string
  23. Print all permutations in sorted (lexicographic) order
  24. Shuffle a given array
  25. Space and time efficient Binomial Coefficient
  26. Reservoir Sampling
  27. Pascal’s Triangle
  28. Select a random number from stream, with O(1) space
  29. Find the largest multiple of 2, 3 and 5
  30. Efficient program to calculate e^x
  31. Measure one litre using two vessels and infinite water supply
  32. Efficient program to print all prime factors of a given number
  33. Print all possible combinations of r elements in a given array of size n
  34. Random number generator in arbitrary probability distribution fashion
  35. How to check if a given number is Fibonacci number?
  36. Russian Peasant Multiplication
  37. Count all possible groups of size 2 or 3 that have sum as multiple of 3
  38. Tower of Hanoi
  39. Horner’s Method for Polynomial Evaluation
  40. Count trailing zeroes in factorial of a number
  41. Program for nth Catalan Number
  42. Generate one of 3 numbers according to given probabilities
  43. Find Excel column name from a given column number
  44. Find next greater number with same set of digits
  45. Count Possible Decodings of a given Digit Sequence
  46. Calculate the angle between hour hand and minute hand
  47. Count number of binary strings without consecutive 1?s
  48. Find the smallest number whose digits multiply to a given number n
  49. Draw a circle without floating point arithmetic
  50. How to check if an instance of 8 puzzle is solvable?
  51. Birthday Paradox
  52. Multiply two polynomials
  53. Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
  54. Count ways to reach the n’th stair
  55. Replace all ‘0’ with ‘5’ in an input Integer
  56. Program to add two polynomials
  57. Print first k digits of 1/n where n is a positive integer
  58. Given a number as a string, find the number of contiguous subsequences which recursively add up to 9

Recent Articles on Mathematical Algorithms

Bit Algorithms:

  1. Find the element that appears once
  2. Detect opposite signs
  3. Set bits in all numbers from 1 to n
  4. Swap bits
  5. Add two numbers
  6. Smallest of three
  7. A Boolean Array Puzzle
  8. Set bits in an (big) array
  9. Next higher number with same number of set bits
  10. Optimization Technique (Modulus)
  11. Add 1 to a number
  12. Multiply with 3.5
  13. Turn off the rightmost set bit
  14. Check for Power of 4
  15. Absolute value (abs) without branching
  16. Modulus division by a power-of-2-number
  17. Minimum or Maximum of two integers
  18. Rotate bits
  19. Find the two non-repeating elements in an array
  20. Number Occurring Odd Number of Times
  21. Check for Integer Overflow
  22. Little and Big Endian
  23. Reverse Bits of a Number
  24. Count set bits in an integer
  25. Number of bits to be flipped to convert A to B
  26. Next Power of 2
  27. Check if a Number is Multiple of 3
  28. Find parity
  29. Multiply with 7
  30. Find whether a no is power of two
  31. Position of rightmost set bit
  32. Binary representation of a given number
  33. Swap all odd and even bits
  34. Find position of the only set bit
  35. Karatsuba algorithm for fast multiplication
  36. How to swap two numbers without using a temporary variable?
  37. Check if a number is multiple of 9 using bitwise operators
  38. Swap two nibbles in a byte
  39. How to turn off a particular bit in a number?
  40. Check if binary representation of a number is palindrome

Recent Articles on Bit Algorithms

Quiz on Bit Algorithms

Graph Algorithms:

Introduction, DFS and BFS:

  1. Graph and its representations
  2. Breadth First Traversal for a Graph
  3. Depth First Traversal for a Graph
  4. Applications of Depth First Search
  5. Detect Cycle in a Directed Graph
  6. Detect Cycle in a an Undirected Graph
  7. Detect cycle in an undirected graph
  8. Longest Path in a Directed Acyclic Graph
  9. Topological Sorting
  10. Check whether a given graph is Bipartite or not
  11. Snake and Ladder Problem
  12. Biconnected Components
  13. Check if a given graph is tree or not

Minimum Spanning Tree:

  1. Prim’s Minimum Spanning Tree (MST))
  2. Applications of Minimum Spanning Tree Problem
  3. Prim’s MST for Adjacency List Representation
  4. Kruskal’s Minimum Spanning Tree Algorithm
  5. Boruvka’s algorithm for Minimum Spanning Tree

Shortest Paths:

  1. Dijkstra’s shortest path algorithm
  2. Dijkstra’s Algorithm for Adjacency List Representation
  3. Bellman–Ford Algorithm
  4. Floyd Warshall Algorithm
  5. Johnson’s algorithm for All-pairs shortest paths
  6. Shortest Path in Directed Acyclic Graph
  7. Some interesting shortest path questions
  8. Shortest path with exactly k edges in a directed and weighted graph

Connectivity:

  1. Find if there is a path between two vertices in a directed graph
  2. Connectivity in a directed graph
  3. Articulation Points (or Cut Vertices) in a Graph
  4. Biconnected graph
  5. Bridges in a graph
  6. Eulerian path and circuit
  7. Fleury’s Algorithm for printing Eulerian Path or Circuit
  8. Strongly Connected Components
  9. Transitive closure of a graph
  10. Find the number of islands
  11. Count all possible walks from a source to a destination with exactly k edges
  12. Euler Circuit in a Directed Graph
  13. Biconnected Components
  14. Tarjan’s Algorithm to find Strongly Connected Components

Hard Problems:

  1. Graph Coloring (Introduction and Applications)
  2. Greedy Algorithm for Graph Coloring
  3. Travelling Salesman Problem (Naive and Dynamic Programming)
  4. Travelling Salesman Problem (Approximate using MST)
  5. Hamiltonian Cycle
  6. Vertex Cover Problem (Introduction and Approximate Algorithm)
  7. K Centers Problem (Greedy Approximate Algorithm)

Maximum Flow:

  1. Ford-Fulkerson Algorithm for Maximum Flow Problem
  2. Find maximum number of edge disjoint paths between two vertices
  3. Find minimum s-t cut in a flow network
  4. Maximum Bipartite Matching
  5. Channel Assignment Problem

Misc:

  1. Find if the strings can be chained to form a circle
  2. Given a sorted dictionary of an alien language, find order of characters
  3. Karger’s algorithm for Minimum Cut
  4. Karger’s algorithm for Minimum Cut | Set 2 (Analysis and Applications)
  5. Hopcroft–Karp Algorithm for Maximum Matching | Set 1 (Introduction)
  6. Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
  7. Length of shortest chain to reach a target word
  8. Find same contacts in a list of contacts

All Algorithms on Graph

Quiz on Graph
Quiz on Graph Traversals
Quiz on Graph Shortest Paths
Quiz on Graph Minimum Spanning Tree

Randomized Algorithms:

  1. Linearity of Expectation
  2. Expected Number of Trials until Success
  3. Randomized Algorithms | Set 0 (Mathematical Background)
  4. Randomized Algorithms | Set 1 (Introduction and Analysis)
  5. Randomized Algorithms | Set 2 (Classification and Applications)
  6. Randomized Algorithms | Set 3 (1/2 Approximate Median)
  7. Karger’s algorithm for Minimum Cut
  8. K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
  9. Reservoir Sampling
  10. Shuffle a given array
  11. Select a Random Node from a Singly Linked List

Recent Articles on Randomized Algorithms

Branch and Bound:

  1. Branch and Bound | Set 1 (Introduction with 0/1 Knapsack)
  2. Branch and Bound | Set 2 (Implementation of 0/1 Knapsack)
  3. Branch and Bound | Set 3 (8 puzzle Problem)
  4. Branch And Bound | Set 4 (Job Assignment Problem)

 

Quizzes on Algorithms:

  1. Analysis of Algorithms
  2. Sorting
  3. Divide and Conquer
  4. Greedy Algorithms
  5. Dynamic Programming
  6. Backtracking
  7. Misc
  8. NP Complete
  9. Searching
  10. Analysis of Algorithms (Recurrences)
  11. Recursion
  12. Bit Algorithms
  13. Graph Traversals
  14. Graph Shortest Paths
  15. Graph Minimum Spanning Tree

Misc:

  1. Commonly Asked Algorithm Interview Questions | Set 1
  2. Given a matrix of ‘O’ and ‘X’, find the largest subsquare surrounded by ‘X’
  3. Nuts & Bolts Problem (Lock & Key problem)
  4. Flood fill Algorithm – how to implement fill() in paint?
  5. Given n appointments, find all conflicting appointments
  6. Check a given sentence for a given set of simple grammer rules
  7. Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
  8. How to check if two given sets are disjoint?
  9. Minimum Number of Platforms Required for a Railway/Bus Station
  10. Length of the largest subarray with contiguous elements | Set 1
  11. Length of the largest subarray with contiguous elements | Set 2
  12. Print all increasing sequences of length k from first n natural numbers
  13. Given two strings, find if first string is a subsequence of second
  14. Snake and Ladder Problem
  15. Write a function that returns 2 for input 1 and returns 1 for 2
  16. Connect n ropes with minimum cost
  17. Find the number of valid parentheses expressions of given length
  18. Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
  19. Generate all binary permutations such that there are more 1’s than 0’s at every point in all permutations
  20. Lexicographically minimum string rotation
  21. Construct an array from its pair-sum array
  22. Program to evaluate simple expressions
  23. Check if characters of a given string can be rearranged to form a palindrome
  24. Print all pairs of anagrams in a given array of strings

Please see Data Structures and Advanced Data Structures for Graph, Binary Tree, BST and Linked List based algorithms.

We will be adding more categories and posts to this page soon.

You can create a new Algorithm topic and discuss it with other geeks using Geeksforgeeks Q&A page. See already discussed Algorithm questions on forum.



Company Wise Coding Practice    Topic Wise Coding Practice

  • ina

    can enyone help me?
    I have this exercise:

    count the leafs of a binary tree thet are divisible by 10

    • Himanshu Dagar

      One way is traverse a tree in any order you want . While traversing check that (node->left==NULL&&node->right==NULL&&node->value%10==0)
      then increment your count variable
      Time complexity:- O(n)
      n-number of nodes in binary tree

  • ARGHA

    JEST QUESTION 2014

    Given an array of M integers .Up to index N the array is filled with positive integers and beyond N all the array elements are negative.M is much larger than N.
    Design an algorithm to find the index N with complexity of log(N).

    • Jeff

      binary search

      • mukund pb

        I think binary search will be log(M) and not log(N) as asked in the question. This may not be the best approach

    • Vikas Varshney

      First find the location of FIRST negative number using step size of power of 2.this will take constant time as power of 2 grows exponentially.O(1)

      Then use the binary search between positive and negative no. in log(N) time.

  • swarnim

    There are 8 cubicles in a room. In each cubicle, there are 8 computers. There are a total of 60 members. Constraint#1 – A team can contain a minimum of 2 members and a maximum of 6 members. Constraint#2 – In every cubicle, not more than 1 computer can be unallocated. Constraint#3 – All team members of a team must be in the same cubicle..anyone plz answer how to write algo on this??

  • swarnim

    Return the longest possible word from the English dictionary by combining symbols of the Periodic Table. Constraint – Symbols containing 2 characters cannot be broken & used independently. E.g. – Sodium’s symbol Na cannot be broken into N & a, you have to use Na together,how to write algo on this??

  • v kumar

    i need fast reply

  • v kumar

    i want step wise algorithm for pascal triangle using nested for loops in the form of
    1
    1 2 1
    1 3 3 1
    1 4 6 4 1

    • Raj Kishor

      //A program to display the Pascal’s Triangle.

      //Created by RAJ.

      #include

      using namespace std;

      main()

      {

      unsigned int term,t;

      int i,j,k,n;

      cout<>n;

      for(i=0;i0;j–)

      cout<<" ";

      term=1;

      for(k=0;k<=i;k++)

      {

      t=term;

      while(t)

      {

      cout<<"b";

      t/=10;

      }

      cout<<term<<" ";

      term=term*(i-k)/(k+1);

      }

      cout<<endl;

      }

      return 0;

      }

    • Raj Kishor

      //A program to display the Pascal's Triangle.

      //Created by RAJ.

      #include

      using namespace std;

      main()

      {

      unsigned int term,t;

      int i,j,k,n;

      cout<>n;

      for(i=0;i0;j--)

      cout<<" ";

      term=1;

      for(k=0;k<=i;k++)

      {

      t=term;

      while(t)

      {

      cout<<"b";

      t/=10;

      }

      cout<<term<<" ";

      term=term*(i-k)/(k+1);

      }

      cout<<endl;

      }

      return 0;

      }

    • Raj Kishor

      //A program to display the Pascal’s Triangle.
      //Created by RAJ.
      #include
      using namespace std;
      main()
      {
      unsigned int term,t;
      int i,j,k,n;
      cout<>n;
      for(i=0;i0;j–)
      cout<<" ";
      term=1;
      for(k=0;k<=i;k++)
      {
      t=term;
      while(t)
      {
      cout<<"b";
      t/=10;
      }
      cout<<term<<" ";
      term=term*(i-k)/(k+1);
      }
      cout<<endl;
      }
      return 0;
      }

  • v kumar

    i want step wise algorithm for pascal triangle using nested for loops in the form of
    1
    1 2 1
    1 3 3 1
    1 4 6 4 1

  • sukoon sharma

    i want some new problems on transfer and conquer algorithm not the ones used in textbooks

  • Yo


    #include

    using namsespace std;

    int geeksforgeeks(int n){

    if(n==1) cout<<"GEEKS FOREVER"<<endl;

    else geeksforgeeks(n-1);

    }

    int main(){#include

    using namespace std;

    int geeksforgeeks(int n){

    if(n==1) return 0;

    geeksforgeeks(n-1);

    cout<<"GEEKS FOREVER"<<endl;

    return 0;

    }

    int main(){

    geeksforgeeks(5);

    }

    geeksforgeeks(20);

    }

  • gwpark

    So incredible job, guy. I’m so impressed. I’ve just read about dynamic programming, and I think the code you inserted is very helpful for me to understand more deeply about dynamic programming. Actually, I was finding some tutorial about algorithm in my native language, but I just decided to get around here to study algorithms (also study English). Thank you.

  • Haad Ali

    Hello….Can any one write an algorithm like find Fibonacci no from 1-20??
    I need dat…plx reply me as soon as possible……..

    • piyush bansal

      a = 0
      b = 1
      c=a+b
      a = b
      b = c
      again loop from start

      play with this algo as per your requirement

    • Chaithanya Kanumolu

      We have a D.P solution for fibonacci series.

      it looks like:
      a[0]=1;
      a[1]=1;
      int i=2;
      for(;a[i]<=20;i++)
      a[i]=a[i-1]+a[i-2];
      for(int j=1;j<=i;j++)
      sop(a[j]);
      Intentionally I have avoided a[0] in sop since 1 gets repeated twice.
      I hope this helps.

    • ARUN

      don’t ask for homework questions here , this platform is for quality discussion .

  • mysticPrince

    Nice List of Algorithms! Good Work geeks!

  • samg

    Hello,

    Thank you for helping me with this. I need to design an algorithm for the following.

    From the table below, I want to find out a set of 7 names that gives the largest sum of marks possible with keeping the total fees less than 4000.

    Just to be clear, here is the problem in different words.

    Desired output:
    Set of 7 names (Name1,Name2,Name3,Name4,Name5,Name6,Name7)that gives the largest sum of Marks
    (Marks1+Marks2+Marks3+Marks4+Marks5+Marks6+Marks7) should be the largest, satisfying the given condition.

    Conditions:
    Sum of Fees of 7 names is less than 4000.
    Fees1+Fees2+Fees3+Fees4+Fees5+Fees6+Fees7 < 4000

    Name Marks Fees
    A 87 345
    B 4 897
    C 87 432
    D 34 435
    E 6 345
    F 3 789
    G 9 78
    H 8 345
    I 7 879
    J 8 324
    K 87 789
    L 43 4536
    M 65 234
    N 87 897
    O 65 978
    P 23 324
    Q 89 342
    R 9 789
    S 87 34
    T 76 234
    U 654 897
    V 76 23
    W 879 435
    X 0 897
    Y 435 54
    Z 56 23

    I really appreciate your response.

    Regards!

    • Chaithanya Kanumolu

      I think this is similar to Knapsack problem. DP should help you, I guess.

    • Desmond

      Knapsack has very less runtime efficiency. . .this takes O(n^2). . .Or better if u replace selection sort with merge sort. . .

      #include

      #include

      main()

      {

      FILE *fp;

      char buff[255];

      int i;

      struct xxx

      {

      char name;

      float marks;

      float fees;

      float avg;

      };

      struct xxx a[26] = {{‘A’, 87 ,345 },

      {‘B’, 4 ,897 },

      {‘C’, 87 ,432 },

      {‘D’, 34 ,435 },

      {‘E’, 6 ,345 },

      {‘F’, 3 ,789 },

      {‘G’, 9 ,78 },

      {‘H’, 8 ,345 },

      {‘I’, 7 ,879 },

      {‘J’, 8 ,324 },

      {‘K’, 87 ,789 },

      {‘L’, 43 ,4536},

      {‘M’, 65 ,234 },

      {‘N’, 87 ,897 },

      {‘O’, 65 ,978 },

      {‘P’, 23 ,324 },

      {‘Q’, 89 ,342 },

      {‘R’, 9 ,789 },

      {‘S’, 87 ,34 },

      {‘T’, 76 ,234 },

      {‘U’, 654 ,897 },

      {‘V’, 76 ,23 },

      {‘W’, 879 ,435 },

      {‘X’, 0 ,897 },

      {‘Y’, 435 ,54 },

      {‘Z’, 56 ,23 }};

      for (i=0;i<26;i++)

      {

      a[i].avg = (4000/a[i].fees) * a[i].marks;

      printf("%c:%fn",a[i].name,a[i].avg);

      }

      printf("nnn");

      for(i=0;i<25;i++)

      {

      int pos=i;

      int j;

      for(j=i+1;ja[pos].avg)

      {

      pos = j;

      }

      }

      if(pos!=i)

      {

      float temp = a[i].avg;

      a[i].avg = a[pos].avg;

      a[pos].avg =temp;

      temp = a[i].fees;

      a[i].fees = a[pos].fees;

      a[pos].fees =temp;

      temp = a[i].marks;

      a[i].marks = a[pos].marks;

      a[pos].marks =temp;

      char c =a[i].name;

      a[i].name=a[pos].name;

      a[pos].name=c;

      }

      }

      for (i=0;i<26;i++)

      {

      printf("%c:t%ft%.0ft%.0fn",a[i].name,a[i].avg,a[i].marks,a[i].fees);

      }

      for(i=6;i<26;i++)

      {

      if((a[i].fees + a[i-1].fees + a[i-2].fees+a[i-3].fees+a[i-4].fees+a[i-5].fees+a[i-6].fees)<=4000 )

      {

      break;

      }

      }

      int j;

      printf("Optimal solution is : n");

      for(j=0;j<7;j++)

      {

      printf ("%c ",a[i–]);

      }

      }

  • mano

    hi! i just want to know some problems that could be solved using graph data structure based on vertex or edge coloring…

    • manu

      kunju paiya. sootha moodu

  • Anoop S S

    Excellent work

  • Hari priya

    It’s really helpful!! thank u GeekfforGeeks

  • anoop shukla

    i got a new way to learn………..i like your data that are being provided

  • Chetan

    Wow loving it all ..Happy studying.. Good work geeksforgeeks :)

  • Ganesh

    Very helpful…. Few more Datastructure and algorithms resources shared here http://www.indiageeks.in/top-5-online-resources-to-learn-data-structures-algorithms/

  • vsethuooo

    This is great! I can find more algos in the misc page! Why don’t you transfer them here, for easier access!!

  • savan

    thank you guys

  • harish.pendela

    Excellent work geeksforgeeks team…!!

  • ultimate_coder

    excellent

  • ultimate_coder

    excellent

  • Procastinator

    Respect!!

  • mahesh

    You guys are awesome … respect !!!

  • Akshay

    Whenever I am confused about what to do…I come to geeksforgeeks…its a boon for passionate individuals who loves coding and dreams of bagging a job at software giants like google,microsoft..

  • Naaz
     
    Great work....thank u guys !!!!!!!!!!
    
     
  • subho

    Fantastic job being done by geeksforgeeks guys. Keep it up. really useful…..

  • Loving it….

  • Nauman

    Guys u rock!! 1 of d best website ever!!

  • azee

    Awesome work guys. Best place to understand code level implementation of different algorithms. Very Very helpful. :)

  • George

    Thanks

  • Chandana

    This is till now the best website for all the knowledge on programming till now, I have come across. Awesome work people. Bless All

  • PG

    Excellent work guys..keep it up!!

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Ashish

    Exactly the kind of website I was looking for. Alive, informative and to the point. Applause for your efforts.

  • gosi
     
    indeed , worth a web Portal ...
     
  • Shailesh

    Great Work,thank you so much…

  • Sharat

    Thank you for this great work… I love this site!

     
    /* Paste your code here (You may delete these lines if not writing code) */