24 Game in C++


Suppose we have four cards; these cards are holding some number from 1 to 9. We have to check whether they could operate through some operators like +, -, *, /, to get 24. So if we have some numbers like [4,9,2,6], then we can get 24 by (4 * 9) – (2 * 6), answer will be true.

To solve this, we will follow these steps −

  • epsilon := 10.0^-5
  • Define a function solve(), this will take an array v,
  • if size of v is same as 1, then −
    • return true when |v[0] - 24.0| <= epsilon
  • for initialize i := 0, when i < size of v, update (increase i by 1), do −
    • for initialize j := 0, when j < size of v, update (increase j by 1), do −
      • if i is same as j, then: Ignore following part, skip to the next iteration
      • Define an array res
      • for initialize k := 0, when k < size of v, update (increase k by 1), do −
        • if i is not equal to k and j is not equal to k, then −
          • insert v[k] at the end of res
      • for initialize k := 0, when k < 4, update (increase k by 1), do −
        • if operators[k] is same as '+', then −
          • insert v[i] + v[j] at the end of res
        • otherwise when operators[k] is same as '-', then −
          • insert v[i] - v[j] at the end of res
        • otherwise when operators[k] is same as '*', then −
          • insert v[i] * v[j] at the end of res
        • Otherwise
          • insert v[i] / v[j] at the end of res
        • if solve(res), then −
          • return true
        • delete last element from res
  • return false
  • From the main method do following steps −
  • Define an array v
  • for initialize i := 0, when i < size of nums, update (increase i by 1), do −
    • insert nums[i] at the end of v
  • return solve(v)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   char operators[4] = {'+', '-', '/', '*'};
   double epsilon = pow(10.0, -5);
   bool judgePoint24(vector<int>& nums) {
      vector <double> v;
      for(int i = 0; i < nums.size(); i++){
         v.push_back(nums[i]);
      }
      return solve(v);
   }
   bool solve(vector <double> v){
      if(v.size() == 1){
         return abs(v[0] - 24.0) <= epsilon;
      }
      for(int i = 0; i < v.size(); i++){
         for(int j = 0; j < v.size(); j++){
            if(i == j) continue;
               vector <double> res;
            for(int k = 0; k < v.size(); k++){
               if(i != k && j != k){
                  res.push_back(v[k]);
               }
            }
            for(int k = 0; k < 4; k++){
               if(operators[k] == '+'){
                  res.push_back(v[i] + v[j]);
               }else if(operators[k] == '-'){
                  res.push_back(v[i] - v[j]);
               }else if(operators[k] == '*'){
                  res.push_back(v[i] * v[j]);
               }else{
                  res.push_back(v[i] / v[j]);
               }
               if(solve(res)) return true;
                  res.pop_back();
            }
         }
      }
      return false;
   }
};
main(){
   Solution ob;
   vector<int> v = {4,9,2,6};
   cout << (ob.judgePoint24(v));
}

Input

{4,9,2,6}

Output

1

Updated on: 01-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements